blast
[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 final class Identifier implements PhylogenyData {
36
37     final public static String NCBI   = "ncbi";
38     final public static String REFSEQ = "refseq";
39     final public static String SP     = "sp";
40     final private String       _value;
41     final private String       _provider;
42     final private String       _value_provider;
43
44     public Identifier() {
45         _value = "";
46         _provider = "";
47         _value_provider = "";
48     }
49
50     public Identifier( final String value ) {
51         _value = value;
52         _provider = "";
53         _value_provider = value;
54     }
55
56     public Identifier( final String value, final String provider ) {
57         _value = value;
58         _provider = provider;
59         if ( provider != null ) {
60             _value_provider = value + provider;
61         }
62         else {
63             _value_provider = value;
64         }
65     }
66
67     @Override
68     public StringBuffer asSimpleText() {
69         return new StringBuffer( getValue() );
70     }
71
72     @Override
73     public StringBuffer asText() {
74         final StringBuffer sb = new StringBuffer();
75         if ( !ForesterUtil.isEmpty( getProvider() ) ) {
76             sb.append( "[" );
77             sb.append( getProvider() );
78             sb.append( "] " );
79         }
80         sb.append( getValue() );
81         return sb;
82     }
83
84     public String getValuePlusProvider() {
85         return _value_provider;
86     }
87
88     @Override
89     public PhylogenyData copy() {
90         return new Identifier( getValue(), getProvider() );
91     }
92
93     @Override
94     public boolean equals( final Object o ) {
95         if ( this == o ) {
96             return true;
97         }
98         else if ( o == null ) {
99             return false;
100         }
101         else if ( o.getClass() != this.getClass() ) {
102             throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
103                     + o.getClass() + "]" );
104         }
105         else {
106             return isEqual( ( Identifier ) o );
107         }
108     }
109
110     public String getProvider() {
111         return _provider;
112     }
113
114     public String getValue() {
115         return _value;
116     }
117
118     @Override
119     public int hashCode() {
120         return _value_provider.hashCode();
121     }
122
123     @Override
124     public boolean isEqual( final PhylogenyData data ) {
125         if ( this == data ) {
126             return true;
127         }
128         if ( ( data == null ) || ( getValue() == null ) ) {
129             return false;
130         }
131         final Identifier a = ( Identifier ) data;
132         if ( ( getProvider() != null ) && ( a.getProvider() != null ) ) {
133             return ( a.getValue().equals( getValue() ) && a.getProvider().equals( getProvider() ) );
134         }
135         return ( a.getValue().equals( getValue() ) );
136     }
137
138     @Override
139     public StringBuffer toNHX() {
140         final StringBuffer sb = new StringBuffer();
141         sb.append( ":" );
142         sb.append( NHXtags.NODE_IDENTIFIER );
143         sb.append( ForesterUtil.replaceIllegalNhxCharacters( getValue() ) );
144         return sb;
145     }
146
147     @Override
148     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
149         if ( !org.forester.util.ForesterUtil.isEmpty( getProvider() ) ) {
150             PhylogenyDataUtil.appendElement( writer,
151                                              PhyloXmlMapping.IDENTIFIER,
152                                              getValue(),
153                                              PhyloXmlMapping.IDENTIFIER_PROVIDER_ATTR,
154                                              getProvider(),
155                                              indentation );
156         }
157         else {
158             PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.IDENTIFIER, getValue(), indentation );
159         }
160     }
161
162     @Override
163     public String toString() {
164         return asText().toString();
165     }
166 }