2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
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.
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.
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
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org
26 package org.forester.phylogeny.data;
28 import java.io.IOException;
29 import java.io.Writer;
31 import org.forester.io.parsers.nhx.NHXtags;
32 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
33 import org.forester.util.ForesterUtil;
35 public final class Accession implements PhylogenyData, Comparable<Accession> {
37 final private String _comment;
38 final private String _source;
39 final private String _source_value;
40 final private String _value;
42 public Accession( final String value, final String source ) {
46 if ( source != null ) {
47 _source_value = source + value;
50 _source_value = value;
54 public Accession( final String value, final String source, final String comment ) {
58 if ( source != null ) {
59 _source_value = source + value;
62 _source_value = value;
67 public StringBuffer asSimpleText() {
68 return new StringBuffer( getValue() );
72 public StringBuffer asText() {
73 final StringBuffer sb = new StringBuffer();
74 if ( !ForesterUtil.isEmpty( getSource() ) ) {
75 sb.append( getSource() );
78 sb.append( getValue() );
79 if ( !ForesterUtil.isEmpty( getComment() ) ) {
81 sb.append( getComment() );
88 public int compareTo( final Accession o ) {
92 return _source_value.compareTo( o._source_value );
96 public PhylogenyData copy() {
97 return new Accession( getValue(), getSource() );
101 public boolean equals( final Object o ) {
105 else if ( o == null ) {
108 else if ( o.getClass() != this.getClass() ) {
109 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
110 + o.getClass() + "]" );
113 return isEqual( ( Accession ) o );
117 public String getComment() {
121 public String getSource() {
125 public String getValue() {
130 public int hashCode() {
131 return _source_value.hashCode();
135 public boolean isEqual( final PhylogenyData data ) {
136 if ( this == data ) {
139 if ( ( data == null ) || ( getValue() == null ) ) {
142 final Accession a = ( Accession ) data;
143 if ( ( getSource() != null ) && ( a.getSource() != null ) ) {
144 return ( a.getValue().equals( getValue() ) && a.getSource().equals( getSource() ) );
146 return ( a.getValue().equals( getValue() ) );
150 public StringBuffer toNHX() {
151 final StringBuffer sb = new StringBuffer();
153 sb.append( NHXtags.SEQUENCE_ACCESSION );
154 sb.append( ForesterUtil.replaceIllegalNhxCharacters( getValue() ) );
159 public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
160 if ( ForesterUtil.isEmpty( getSource() ) ) {
161 if ( ForesterUtil.isEmpty( getComment() ) ) {
162 PhylogenyDataUtil.appendElement( writer,
163 PhyloXmlMapping.ACCESSION,
165 PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
170 PhylogenyDataUtil.appendElement( writer,
171 PhyloXmlMapping.ACCESSION,
173 PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
175 PhyloXmlMapping.ACCESSION_COMMENT_ATTR,
181 if ( ForesterUtil.isEmpty( getComment() ) ) {
182 PhylogenyDataUtil.appendElement( writer,
183 PhyloXmlMapping.ACCESSION,
185 PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
190 PhylogenyDataUtil.appendElement( writer,
191 PhyloXmlMapping.ACCESSION,
193 PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
195 PhyloXmlMapping.ACCESSION_COMMENT_ATTR,
203 public String toString() {
204 return asText().toString();