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