in progress
[jalview.git] / forester / java / src / org / forester / phylogeny / data / PropertiesMap.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // All rights reserved
8 //
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.
13 //
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.
18 //
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
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.phylogeny.data;
27
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;
34
35 import org.forester.util.ForesterUtil;
36
37 public class PropertiesMap implements PhylogenyData {
38
39     private final SortedMap<String, Property> _properties;
40
41     public PropertiesMap() {
42         _properties = new TreeMap<String, Property>();
43     }
44
45     public int size() {
46         return _properties.size();
47     }
48
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" );
52         }
53         getProperties().put( property.getRef(), property );
54     }
55
56     @Override
57     public StringBuffer asSimpleText() {
58         final StringBuffer sb = new StringBuffer();
59         boolean first = true;
60         for( final String ref : getPropertyRefs() ) {
61             if ( first ) {
62                 first = false;
63             }
64             else {
65                 sb.append( " " );
66             }
67             sb.append( getProperty( ref ).asText() );
68         }
69         return sb;
70     }
71
72     @Override
73     public StringBuffer asText() {
74         return asSimpleText();
75     }
76
77     @Override
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 ) );
82         }
83         return new_one;
84     }
85
86     public SortedMap<String, Property> getProperties() {
87         return _properties;
88     }
89
90     public Property[] getPropertiesArray() {
91         final Property[] a = new Property[ getProperties().size() ];
92         int i = 0;
93         for( final String ref : getProperties().keySet() ) {
94             a[ i++ ] = getProperties().get( ref );
95         }
96         return a;
97     }
98
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" );
103         }
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 ) );
109             }
110         }
111         return props;
112     }
113
114     public Property getProperty( final String ref ) throws IllegalArgumentException {
115         if ( getProperties().containsKey( ref ) ) {
116             return getProperties().get( ref );
117         }
118         else {
119             throw new IllegalArgumentException( "reference [" + ref + "] is not present" );
120         }
121     }
122
123     /**
124      * Returns all property refs of this PhylogenyNode as String array.
125      */
126     public String[] getPropertyRefs() {
127         if ( getProperties() == null ) {
128             return new String[ 0 ];
129         }
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();
134         }
135         return refs;
136     }
137
138     @Override
139     public boolean isEqual( final PhylogenyData data ) {
140         throw new UnsupportedOperationException();
141     }
142
143     public boolean refExists( final String ref ) {
144         if ( getProperties() != null ) {
145             for( final String r : getProperties().keySet() ) {
146                 if ( r.equalsIgnoreCase( ref ) ) {
147                     return true;
148                 }
149             }
150         }
151         return false;
152     }
153
154     public Property removeProperty( final String ref ) throws IllegalArgumentException {
155         if ( getProperties().containsKey( ref ) ) {
156             return getProperties().remove( ref );
157         }
158         else {
159             throw new IllegalArgumentException( "reference [" + ref + "] is not present" );
160         }
161     }
162
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" );
167         }
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 );
173             }
174         }
175         for( final String ref : to_remove ) {
176             getProperties().remove( ref );
177         }
178         return to_remove;
179     }
180
181     @Override
182     public StringBuffer toNHX() {
183         throw new UnsupportedOperationException();
184     }
185
186     @Override
187     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
188         if ( getProperties() != null ) {
189             for( final String ref : getProperties().keySet() ) {
190                 getProperties().get( ref ).toPhyloXML( writer, level, indentation );
191             }
192         }
193     }
194
195     @Override
196     public String toString() {
197         return asSimpleText().toString();
198     }
199 }