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