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
7 // Copyright (C) 2000-2001 Washington University School of Medicine
8 // and Howard Hughes Medical Institute
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.
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.
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
25 // Contact: phylosoft @ gmail . com
26 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
28 package org.forester.phylogeny.data;
30 import java.io.IOException;
31 import java.io.Writer;
32 import java.util.ArrayList;
33 import java.util.List;
35 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
36 import org.forester.io.writers.PhylogenyWriter;
37 import org.forester.util.ForesterUtil;
39 public class Distribution implements PhylogenyData {
41 private final String _desc;
42 private final List<Point> _points;
43 private final List<Polygon> _polygons;
45 public Distribution( final String desc ) {
51 public Distribution( final String desc, final List<Point> points ) {
57 public Distribution( final String desc, final List<Point> points, final List<Polygon> polygons ) {
63 public boolean isEmpty() {
64 if ( ForesterUtil.isEmpty( _desc ) && ( ( getPoints() != null ) && ( getPoints().size() == 1 ) )
65 && ForesterUtil.isEmpty( _polygons ) ) {
66 if ( Point.isSeemsEmpty( getPoints().get( 0 ) ) ) {
70 return ForesterUtil.isEmpty( _desc ) && ForesterUtil.isEmpty( _points ) && ForesterUtil.isEmpty( _polygons );
74 public StringBuffer asSimpleText() {
75 final StringBuffer sb = new StringBuffer();
79 sb.append( "Distribution: " );
80 if ( !ForesterUtil.isEmpty( getDesc() ) ) {
81 sb.append( ForesterUtil.LINE_SEPARATOR );
82 sb.append( " Description: " );
83 sb.append( getDesc() );
86 if ( getPoints() != null ) {
87 for( final Point point : getPoints() ) {
88 if ( ( point != null ) && !Point.isSeemsEmpty( point ) ) {
89 sb.append( ForesterUtil.LINE_SEPARATOR );
90 sb.append( " Point " + i + ": " );
91 sb.append( point.asSimpleText() );
97 if ( getPolygons() != null ) {
98 for( final Polygon polygon : getPolygons() ) {
99 if ( polygon != null ) {
100 sb.append( ForesterUtil.LINE_SEPARATOR );
101 sb.append( " Polygon " + i + ":" );
102 sb.append( ForesterUtil.LINE_SEPARATOR );
103 sb.append( polygon.asSimpleText() );
112 public StringBuffer asText() {
113 return asSimpleText();
117 public PhylogenyData copy() {
118 List<Point> new_points = null;
119 List<Polygon> new_polygons = null;
120 if ( getPoints() != null ) {
121 new_points = new ArrayList<Point>();
122 for( final Point point : getPoints() ) {
123 new_points.add( ( Point ) point.copy() );
126 if ( getPolygons() != null ) {
127 new_polygons = new ArrayList<Polygon>();
128 for( final Polygon polygon : getPolygons() ) {
129 new_polygons.add( ( Polygon ) polygon.copy() );
132 return new Distribution( getDesc(), new_points, new_polygons );
135 public String getDesc() {
139 public List<Point> getPoints() {
143 public List<Polygon> getPolygons() {
148 public boolean isEqual( final PhylogenyData data ) {
149 throw new UnsupportedOperationException();
153 public StringBuffer toNHX() {
154 throw new UnsupportedOperationException();
158 public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
162 writer.write( ForesterUtil.LINE_SEPARATOR );
163 writer.write( indentation );
164 PhylogenyDataUtil.appendOpen( writer, PhyloXmlMapping.DISTRIBUTION );
165 if ( !ForesterUtil.isEmpty( getDesc() ) ) {
166 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.DISTRIBUTION_DESC, getDesc(), indentation );
168 final String ind = indentation + PhylogenyWriter.PHYLO_XML_INTENDATION_BASE;
169 if ( getPoints() != null ) {
170 for( final Point point : getPoints() ) {
171 if ( ( point != null ) && !Point.isSeemsEmpty( point ) ) {
172 point.toPhyloXML( writer, level, ind );
176 if ( getPolygons() != null ) {
177 for( final Polygon polygon : getPolygons() ) {
178 if ( polygon != null ) {
179 polygon.toPhyloXML( writer, level, ind );
183 writer.write( ForesterUtil.LINE_SEPARATOR );
184 writer.write( indentation );
185 PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.DISTRIBUTION );
189 public String toString() {
190 return asSimpleText().toString();