577a532299193953a329c817422cbb21edc5e29f
[jalview.git] / forester / java / src / org / forester / phylogeny / data / Polygon.java
1 // $Id:
2 // forester -- software libraries and applications
3 // for genomics and evolutionary biology research.
4 //
5 // Copyright (C) 2010 Christian M Zmasek
6 // Copyright (C) 2010 Sanford-Burnham Medical Research Institute
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: www.phylosoft.org/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
33 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
34 import org.forester.io.writers.PhylogenyWriter;
35 import org.forester.util.ForesterUtil;
36
37 public class Polygon implements PhylogenyData {
38
39     private final List<Point> _points;
40
41     public Polygon( final List<Point> points ) {
42         _points = points;
43     }
44
45     @Override
46     public StringBuffer asSimpleText() {
47         final StringBuffer sb = new StringBuffer();
48         boolean first = true;
49         for( final Point point : getPoints() ) {
50             if ( first ) {
51                 first = false;
52             }
53             else {
54                 sb.append( ForesterUtil.LINE_SEPARATOR );
55             }
56             sb.append( point.asSimpleText() );
57         }
58         return sb;
59     }
60
61     @Override
62     public StringBuffer asText() {
63         return asSimpleText();
64     }
65
66     @Override
67     public PhylogenyData copy() {
68         final List<Point> new_points = new ArrayList<Point>();
69         for( final Point point : getPoints() ) {
70             new_points.add( ( Point ) point.copy() );
71         }
72         return new Polygon( new_points );
73     }
74
75     public List<Point> getPoints() {
76         return _points;
77     }
78
79     public boolean isEmpty() {
80         return ForesterUtil.isEmpty( _points );
81     }
82
83     @Override
84     public boolean isEqual( final PhylogenyData data ) {
85         throw new UnsupportedOperationException();
86     }
87
88     @Override
89     public StringBuffer toNHX() {
90         throw new UnsupportedOperationException();
91     }
92
93     @Override
94     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
95         if ( isEmpty() ) {
96             return;
97         }
98         writer.write( ForesterUtil.LINE_SEPARATOR );
99         writer.write( indentation );
100         PhylogenyDataUtil.appendOpen( writer, PhyloXmlMapping.POLYGON );
101         for( final Point point : getPoints() ) {
102             point.toPhyloXML( writer, level, indentation + PhylogenyWriter.PHYLO_XML_INTENDATION_BASE );
103             writer.write( indentation );
104         }
105         writer.write( ForesterUtil.LINE_SEPARATOR );
106         writer.write( indentation );
107         PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.POLYGON );
108     }
109 }