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/forester
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 Identifier implements PhylogenyData {
37 final public static String NCBI = "ncbi";
38 final public static String REFSEQ = "refseq";
39 final private String _value;
40 final private String _provider;
41 final private String _value_provider;
49 public Identifier( final String value ) {
52 _value_provider = value;
55 public Identifier( final String value, final String provider ) {
58 if ( provider != null ) {
59 _value_provider = value + provider;
62 _value_provider = value;
67 public StringBuffer asSimpleText() {
68 return new StringBuffer( getValue() );
72 public StringBuffer asText() {
73 final StringBuffer sb = new StringBuffer();
74 if ( !ForesterUtil.isEmpty( getProvider() ) ) {
76 sb.append( getProvider() );
79 sb.append( getValue() );
83 public String getValuePlusProvider() {
84 return _value_provider;
88 public PhylogenyData copy() {
89 return new Identifier( getValue(), getProvider() );
93 public boolean equals( final Object o ) {
97 else if ( o == null ) {
100 else if ( o.getClass() != this.getClass() ) {
101 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
102 + o.getClass() + "]" );
105 return isEqual( ( Identifier ) o );
109 public String getProvider() {
113 public String getValue() {
118 public int hashCode() {
119 return _value_provider.hashCode();
123 public boolean isEqual( final PhylogenyData data ) {
124 if ( this == data ) {
127 if ( ( data == null ) || ( getValue() == null ) ) {
130 final Identifier a = ( Identifier ) data;
131 if ( ( getProvider() != null ) && ( a.getProvider() != null ) ) {
132 return ( a.getValue().equals( getValue() ) && a.getProvider().equals( getProvider() ) );
134 return ( a.getValue().equals( getValue() ) );
138 public StringBuffer toNHX() {
139 final StringBuffer sb = new StringBuffer();
141 sb.append( NHXtags.NODE_IDENTIFIER );
142 sb.append( ForesterUtil.replaceIllegalNhxCharacters( getValue() ) );
147 public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
148 if ( !org.forester.util.ForesterUtil.isEmpty( getProvider() ) ) {
149 PhylogenyDataUtil.appendElement( writer,
150 PhyloXmlMapping.IDENTIFIER,
152 PhyloXmlMapping.IDENTIFIER_PROVIDER_ATTR,
157 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.IDENTIFIER, getValue(), indentation );
162 public String toString() {
163 return asText().toString();