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