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