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 class Identifier implements PhylogenyData {
38 final String _provider;
45 public Identifier( final String value ) {
50 public Identifier( final String value, final String provider ) {
55 public StringBuffer asSimpleText() {
56 return new StringBuffer( getValue() );
59 public StringBuffer asText() {
60 final StringBuffer sb = new StringBuffer();
61 if ( !ForesterUtil.isEmpty( getProvider() ) ) {
63 sb.append( getProvider() );
66 sb.append( getValue() );
70 public PhylogenyData copy() {
71 return new Identifier( getValue(), getProvider() );
75 public boolean equals( final Object o ) {
79 else if ( o == null ) {
82 else if ( o.getClass() != this.getClass() ) {
83 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
84 + o.getClass() + "]" );
87 return isEqual( ( Identifier ) o );
91 public String getProvider() {
95 public String getValue() {
100 public int hashCode() {
101 if ( getProvider() != null ) {
102 return ( getProvider() + getValue() ).hashCode();
104 return getValue().hashCode();
107 public boolean isEqual( final PhylogenyData data ) {
108 if ( this == data ) {
111 if ( ( data == null ) || ( getValue() == null ) ) {
114 final Identifier a = ( Identifier ) data;
115 if ( ( getProvider() != null ) && ( a.getProvider() != null ) ) {
116 return ( a.getValue().equals( getValue() ) && a.getProvider().equals( getProvider() ) );
118 return ( a.getValue().equals( getValue() ) );
121 public StringBuffer toNHX() {
122 final StringBuffer sb = new StringBuffer();
124 sb.append( NHXtags.NODE_IDENTIFIER );
125 sb.append( ForesterUtil.replaceIllegalNhxCharacters( getValue() ) );
129 public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
130 if ( !org.forester.util.ForesterUtil.isEmpty( getProvider() ) ) {
131 PhylogenyDataUtil.appendElement( writer,
132 PhyloXmlMapping.IDENTIFIER,
134 PhyloXmlMapping.IDENTIFIER_PROVIDER_ATTR,
139 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.IDENTIFIER, getValue(), indentation );
144 public String toString() {
145 return asText().toString();