in progress
[jalview.git] / forester / java / src / org / forester / phylogeny / data / Distribution.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 // Copyright (C) 2000-2001 Washington University School of Medicine
8 // and Howard Hughes Medical Institute
9 // All rights reserved
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
15 //
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License along with this library; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 //
25 // Contact: phylosoft @ gmail . com
26 // WWW: www.phylosoft.org/forester
27
28 package org.forester.phylogeny.data;
29
30 import java.io.IOException;
31 import java.io.Writer;
32 import java.util.ArrayList;
33 import java.util.List;
34
35 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
36 import org.forester.io.writers.PhylogenyWriter;
37 import org.forester.util.ForesterUtil;
38
39 public class Distribution implements PhylogenyData {
40
41     private final String        _desc;
42     private final List<Point>   _points;
43     private final List<Polygon> _polygons;
44
45     public Distribution( final String desc ) {
46         _desc = desc;
47         _points = null;
48         _polygons = null;
49     }
50
51     public Distribution( final String desc, final List<Point> points ) {
52         _desc = null;
53         _points = points;
54         _polygons = null;
55     }
56
57     public Distribution( final String desc, final List<Point> points, final List<Polygon> polygons ) {
58         _desc = desc;
59         _points = points;
60         _polygons = polygons;
61     }
62
63     public boolean isEmpty() {
64         return ForesterUtil.isEmpty( _desc ) && ForesterUtil.isEmpty( _points ) && ForesterUtil.isEmpty( _polygons );
65     }
66
67     @Override
68     public StringBuffer asSimpleText() {
69         final StringBuffer sb = new StringBuffer();
70         if ( isEmpty() ) {
71             return sb;
72         }
73         sb.append( "Distribution: " );
74         if ( !ForesterUtil.isEmpty( getDesc() ) ) {
75             sb.append( ForesterUtil.LINE_SEPARATOR );
76             sb.append( " Description: " );
77             sb.append( getDesc() );
78         }
79         int i = 0;
80         if ( getPoints() != null ) {
81             for( final Point point : getPoints() ) {
82                 if ( point != null ) {
83                     sb.append( ForesterUtil.LINE_SEPARATOR );
84                     sb.append( " Point " + i + ": " );
85                     sb.append( point.asSimpleText() );
86                     i++;
87                 }
88             }
89         }
90         i = 0;
91         if ( getPolygons() != null ) {
92             for( final Polygon polygon : getPolygons() ) {
93                 if ( polygon != null ) {
94                     sb.append( ForesterUtil.LINE_SEPARATOR );
95                     sb.append( " Polygon " + i + ":" );
96                     sb.append( ForesterUtil.LINE_SEPARATOR );
97                     sb.append( polygon.asSimpleText() );
98                     i++;
99                 }
100             }
101         }
102         return sb;
103     }
104
105     @Override
106     public StringBuffer asText() {
107         return asSimpleText();
108     }
109
110     @Override
111     public PhylogenyData copy() {
112         List<Point> new_points = null;
113         List<Polygon> new_polygons = null;
114         if ( getPoints() != null ) {
115             new_points = new ArrayList<Point>();
116             for( final Point point : getPoints() ) {
117                 new_points.add( ( Point ) point.copy() );
118             }
119         }
120         if ( getPolygons() != null ) {
121             new_polygons = new ArrayList<Polygon>();
122             for( final Polygon polygon : getPolygons() ) {
123                 new_polygons.add( ( Polygon ) polygon.copy() );
124             }
125         }
126         return new Distribution( getDesc(), new_points, new_polygons );
127     }
128
129     public String getDesc() {
130         return _desc;
131     }
132
133     public List<Point> getPoints() {
134         return _points;
135     }
136
137     public List<Polygon> getPolygons() {
138         return _polygons;
139     }
140
141     @Override
142     public boolean isEqual( final PhylogenyData data ) {
143         throw new UnsupportedOperationException();
144     }
145
146     @Override
147     public StringBuffer toNHX() {
148         throw new UnsupportedOperationException();
149     }
150
151     @Override
152     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
153         if ( isEmpty() ) {
154             return;
155         }
156         writer.write( ForesterUtil.LINE_SEPARATOR );
157         writer.write( indentation );
158         PhylogenyDataUtil.appendOpen( writer, PhyloXmlMapping.DISTRIBUTION );
159         if ( !ForesterUtil.isEmpty( getDesc() ) ) {
160             PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.DISTRIBUTION_DESC, getDesc(), indentation );
161         }
162         final String ind = indentation + PhylogenyWriter.PHYLO_XML_INTENDATION_BASE;
163         if ( getPoints() != null ) {
164             for( final Point point : getPoints() ) {
165                 point.toPhyloXML( writer, level, ind );
166             }
167         }
168         if ( getPolygons() != null ) {
169             for( final Polygon polygon : getPolygons() ) {
170                 polygon.toPhyloXML( writer, level, ind );
171             }
172         }
173         writer.write( ForesterUtil.LINE_SEPARATOR );
174         writer.write( indentation );
175         PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.DISTRIBUTION );
176     }
177
178     @Override
179     public String toString() {
180         return asSimpleText().toString();
181     }
182 }