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: https://sites.google.com/site/cmzmasek/home/software/forester
26 package org.forester.phylogeny.data;
28 import java.io.IOException;
29 import java.io.Writer;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.SortedMap;
33 import java.util.TreeMap;
35 import org.forester.util.ForesterUtil;
37 public class PropertiesMap implements PhylogenyData {
39 private final SortedMap<String, Property> _properties;
41 public PropertiesMap() {
42 _properties = new TreeMap<String, Property>();
46 return _properties.size();
49 public void addProperty( final Property property ) throws IllegalArgumentException {
50 if ( getProperties().containsKey( property.getRef() ) ) {
51 throw new IllegalArgumentException( "ref [" + property.getRef() + "] is already present" );
53 getProperties().put( property.getRef(), property );
57 public StringBuffer asSimpleText() {
58 final StringBuffer sb = new StringBuffer();
60 for( final String ref : getPropertyRefs() ) {
67 sb.append( getProperty( ref ).asText() );
73 public StringBuffer asText() {
74 return asSimpleText();
78 public PhylogenyData copy() {
79 final PropertiesMap new_one = new PropertiesMap();
80 for( final String r : getProperties().keySet() ) {
81 new_one.addProperty( getProperties().get( r ) );
86 public SortedMap<String, Property> getProperties() {
90 public Property[] getPropertiesArray() {
91 final Property[] a = new Property[ getProperties().size() ];
93 for( final String ref : getProperties().keySet() ) {
94 a[ i++ ] = getProperties().get( ref );
99 public List<Property> getPropertiesWithGivenReferencePrefix( final String ref_prefix )
100 throws IllegalArgumentException {
101 if ( ForesterUtil.isEmpty( ref_prefix ) ) {
102 throw new IllegalArgumentException( "reference prefix is null or empty" );
104 final String my_ref_prefix = new String( ref_prefix.trim() );
105 final List<Property> props = new ArrayList<Property>();
106 for( final String ref : getProperties().keySet() ) {
107 if ( ref.startsWith( my_ref_prefix ) ) {
108 props.add( getProperty( ref ) );
114 public Property getProperty( final String ref ) throws IllegalArgumentException {
115 if ( getProperties().containsKey( ref ) ) {
116 return getProperties().get( ref );
119 throw new IllegalArgumentException( "reference [" + ref + "] is not present" );
124 * Returns all property refs of this PhylogenyNode as String array.
126 public String[] getPropertyRefs() {
127 if ( getProperties() == null ) {
128 return new String[ 0 ];
130 final Property[] properties = getPropertiesArray();
131 final String[] refs = new String[ properties.length ];
132 for( int i = 0; i < properties.length; ++i ) {
133 refs[ i ] = properties[ i ].getRef();
139 public boolean isEqual( final PhylogenyData data ) {
140 throw new UnsupportedOperationException();
143 public boolean refExists( final String ref ) {
144 if ( getProperties() != null ) {
145 for( final String r : getProperties().keySet() ) {
146 if ( r.equalsIgnoreCase( ref ) ) {
154 public Property removeProperty( final String ref ) throws IllegalArgumentException {
155 if ( getProperties().containsKey( ref ) ) {
156 return getProperties().remove( ref );
159 throw new IllegalArgumentException( "reference [" + ref + "] is not present" );
163 public List<String> removePropertiesWithGivenReferencePrefix( final String ref_prefix )
164 throws IllegalArgumentException {
165 if ( ForesterUtil.isEmpty( ref_prefix ) ) {
166 throw new IllegalArgumentException( "reference prefix is null or empty" );
168 final String my_ref_prefix = new String( ref_prefix.trim() );
169 final List<String> to_remove = new ArrayList<String>();
170 for( final String ref : getProperties().keySet() ) {
171 if ( ref.startsWith( my_ref_prefix ) ) {
172 to_remove.add( ref );
175 for( final String ref : to_remove ) {
176 getProperties().remove( ref );
182 public StringBuffer toNHX() {
183 final StringBuffer sb = new StringBuffer();
184 if ( getProperties() != null ) {
185 for( final String ref : getProperties().keySet() ) {
186 sb.append( getProperties().get( ref ).toNHX() );
193 public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
194 if ( getProperties() != null ) {
195 for( final String ref : getProperties().keySet() ) {
196 getProperties().get( ref ).toPhyloXML( writer, level, indentation );
202 public String toString() {
203 return asSimpleText().toString();