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: www.phylosoft.org/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 return ForesterUtil.isEmpty( _desc ) && ForesterUtil.isEmpty( _points ) && ForesterUtil.isEmpty( _polygons );
68 public StringBuffer asSimpleText() {
69 final StringBuffer sb = new StringBuffer();
73 sb.append( "Distribution: " );
74 if ( !ForesterUtil.isEmpty( getDesc() ) ) {
75 sb.append( ForesterUtil.LINE_SEPARATOR );
76 sb.append( " Description: " );
77 sb.append( getDesc() );
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() );
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() );
106 public StringBuffer asText() {
107 return asSimpleText();
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() );
120 if ( getPolygons() != null ) {
121 new_polygons = new ArrayList<Polygon>();
122 for( final Polygon polygon : getPolygons() ) {
123 new_polygons.add( ( Polygon ) polygon.copy() );
126 return new Distribution( getDesc(), new_points, new_polygons );
129 public String getDesc() {
133 public List<Point> getPoints() {
137 public List<Polygon> getPolygons() {
142 public boolean isEqual( final PhylogenyData data ) {
143 throw new UnsupportedOperationException();
147 public StringBuffer toNHX() {
148 throw new UnsupportedOperationException();
152 public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
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 );
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 );
168 if ( getPolygons() != null ) {
169 for( final Polygon polygon : getPolygons() ) {
170 polygon.toPhyloXML( writer, level, ind );
173 writer.write( ForesterUtil.LINE_SEPARATOR );
174 writer.write( indentation );
175 PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.DISTRIBUTION );
179 public String toString() {
180 return asSimpleText().toString();