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