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 class Accession implements PhylogenyData {
40 public Accession( final String value, final String source ) {
45 public StringBuffer asSimpleText() {
46 return new StringBuffer( getValue() );
49 public StringBuffer asText() {
50 final StringBuffer sb = new StringBuffer();
51 if ( !ForesterUtil.isEmpty( getSource() ) ) {
53 sb.append( getSource() );
56 sb.append( getValue() );
60 public PhylogenyData copy() {
61 return new Accession( new String( getValue() ), new String( getSource() ) );
65 public boolean equals( final Object o ) {
69 else if ( o == null ) {
72 else if ( o.getClass() != this.getClass() ) {
73 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
74 + o.getClass() + "]" );
77 return isEqual( ( Accession ) o );
81 public String getSource() {
85 public String getValue() {
90 public int hashCode() {
91 if ( getSource() != null ) {
92 return ( getSource() + getValue() ).hashCode();
94 return getValue().hashCode();
97 public boolean isEqual( final PhylogenyData data ) {
101 if ( ( data == null ) || ( getValue() == null ) ) {
104 final Accession a = ( Accession ) data;
105 if ( ( getSource() != null ) && ( a.getSource() != null ) ) {
106 return ( a.getValue().equals( getValue() ) && a.getSource().equals( getSource() ) );
108 return ( a.getValue().equals( getValue() ) );
111 public StringBuffer toNHX() {
112 final StringBuffer sb = new StringBuffer();
114 sb.append( NHXtags.SEQUENCE_ACCESSION );
115 sb.append( ForesterUtil.replaceIllegalNhxCharacters( getValue() ) );
119 public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
120 if ( ForesterUtil.isEmpty( getSource() ) ) {
121 PhylogenyDataUtil.appendElement( writer,
122 PhyloXmlMapping.ACCESSION,
124 PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
129 PhylogenyDataUtil.appendElement( writer,
130 PhyloXmlMapping.ACCESSION,
132 PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
139 public String toString() {
140 return asText().toString();