in progress...
[jalview.git] / forester / java / src / org / forester / phylogeny / data / PropertiesList.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.Collections;
32 import java.util.Comparator;
33 import java.util.List;
34
35 import org.forester.util.ForesterUtil;
36
37 public class PropertiesList implements PhylogenyData {
38
39     private final List<Property>       _properties;
40     private final Comparator<Property> comp = new Comparator<Property>() {
41
42                                                 @Override
43                                                 public int compare( final Property p1, final Property p2 ) {
44                                                     return p2.getRef().compareTo( p1.getRef() );
45                                                 }
46                                             };
47
48     public PropertiesList() {
49         _properties = new ArrayList<Property>();
50     }
51
52     public int size() {
53         return _properties.size();
54     }
55
56     public void addProperty( final Property property ) throws IllegalArgumentException {
57         _properties.add( property );
58         Collections.sort( _properties, comp );
59     }
60
61     @Override
62     public StringBuffer asSimpleText() {
63         final StringBuffer sb = new StringBuffer();
64         boolean first = true;
65         for( final Property p : getProperties() ) {
66             if ( first ) {
67                 first = false;
68             }
69             else {
70                 sb.append( "\n" );
71             }
72             sb.append( p.asText() );
73         }
74         return sb;
75     }
76
77     @Override
78     public StringBuffer asText() {
79         return asSimpleText();
80     }
81
82     @Override
83     public PhylogenyData copy() {
84         final PropertiesList new_one = new PropertiesList();
85         for( final Property r : getProperties() ) {
86             new_one.addProperty( r );
87         }
88         return new_one;
89     }
90
91     public List<Property> getProperties() {
92         return _properties;
93     }
94
95     public List<Property> getPropertiesWithGivenReferencePrefix( final String ref_prefix )
96             throws IllegalArgumentException {
97         if ( ForesterUtil.isEmpty( ref_prefix ) ) {
98             throw new IllegalArgumentException( "reference prefix is null or empty" );
99         }
100         final String my_ref_prefix = new String( ref_prefix.trim() );
101         final List<Property> props = new ArrayList<Property>();
102         for( final Property p : getProperties() ) {
103             if ( p.getRef().startsWith( my_ref_prefix ) ) {
104                 props.add( p );
105             }
106         }
107         return props;
108     }
109
110     public List<Property> getProperties( final String ref ) throws IllegalArgumentException {
111         final List<Property> props = new ArrayList<Property>();
112         for( final Property p : getProperties() ) {
113             if ( p.getRef().equals( ref ) ) {
114                 props.add( p );
115             }
116         }
117         return props;
118     }
119
120     @Override
121     public boolean isEqual( final PhylogenyData data ) {
122         throw new UnsupportedOperationException();
123     }
124
125     @Override
126     public StringBuffer toNHX() {
127         throw new UnsupportedOperationException();
128     }
129
130     @Override
131     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
132         if ( getProperties() != null ) {
133             for( final Property p : getProperties() ) {
134                 p.toPhyloXML( writer, level, indentation );
135             }
136         }
137     }
138
139     @Override
140     public String toString() {
141         return asSimpleText().toString();
142     }
143 }