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