in progress
[jalview.git] / forester / java / src / org / forester / phylogeny / data / Identifier.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
25
26 package org.forester.phylogeny.data;
27
28 import java.io.IOException;
29 import java.io.Writer;
30
31 import org.forester.io.parsers.nhx.NHXtags;
32 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
33 import org.forester.util.ForesterUtil;
34
35 public class Identifier implements PhylogenyData {
36
37     final String _value;
38     final String _provider;
39
40     public Identifier() {
41         _value = "";
42         _provider = "";
43     }
44
45     public Identifier( final String value ) {
46         _value = value;
47         _provider = "";
48     }
49
50     public Identifier( final String value, final String provider ) {
51         _value = value;
52         _provider = provider;
53     }
54
55     @Override
56     public StringBuffer asSimpleText() {
57         return new StringBuffer( getValue() );
58     }
59
60     @Override
61     public StringBuffer asText() {
62         final StringBuffer sb = new StringBuffer();
63         if ( !ForesterUtil.isEmpty( getProvider() ) ) {
64             sb.append( "[" );
65             sb.append( getProvider() );
66             sb.append( "] " );
67         }
68         sb.append( getValue() );
69         return sb;
70     }
71
72     @Override
73     public PhylogenyData copy() {
74         return new Identifier( getValue(), getProvider() );
75     }
76
77     @Override
78     public boolean equals( final Object o ) {
79         if ( this == o ) {
80             return true;
81         }
82         else if ( o == null ) {
83             return false;
84         }
85         else if ( o.getClass() != this.getClass() ) {
86             throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
87                     + o.getClass() + "]" );
88         }
89         else {
90             return isEqual( ( Identifier ) o );
91         }
92     }
93
94     public String getProvider() {
95         return _provider;
96     }
97
98     public String getValue() {
99         return _value;
100     }
101
102     @Override
103     public int hashCode() {
104         if ( getProvider() != null ) {
105             return ( getProvider() + getValue() ).hashCode();
106         }
107         return getValue().hashCode();
108     }
109
110     @Override
111     public boolean isEqual( final PhylogenyData data ) {
112         if ( this == data ) {
113             return true;
114         }
115         if ( ( data == null ) || ( getValue() == null ) ) {
116             return false;
117         }
118         final Identifier a = ( Identifier ) data;
119         if ( ( getProvider() != null ) && ( a.getProvider() != null ) ) {
120             return ( a.getValue().equals( getValue() ) && a.getProvider().equals( getProvider() ) );
121         }
122         return ( a.getValue().equals( getValue() ) );
123     }
124
125     @Override
126     public StringBuffer toNHX() {
127         final StringBuffer sb = new StringBuffer();
128         sb.append( ":" );
129         sb.append( NHXtags.NODE_IDENTIFIER );
130         sb.append( ForesterUtil.replaceIllegalNhxCharacters( getValue() ) );
131         return sb;
132     }
133
134     @Override
135     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
136         if ( !org.forester.util.ForesterUtil.isEmpty( getProvider() ) ) {
137             PhylogenyDataUtil.appendElement( writer,
138                                              PhyloXmlMapping.IDENTIFIER,
139                                              getValue(),
140                                              PhyloXmlMapping.IDENTIFIER_PROVIDER_ATTR,
141                                              getProvider(),
142                                              indentation );
143         }
144         else {
145             PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.IDENTIFIER, getValue(), indentation );
146         }
147     }
148
149     @Override
150     public String toString() {
151         return asText().toString();
152     }
153 }