moved to: https://sites.google.com/site/cmzmasek/home/software/forester
[jalview.git] / forester / java / src / org / forester / phylogeny / data / Point.java
1
2 package org.forester.phylogeny.data;
3
4 import java.io.IOException;
5 import java.io.Writer;
6 import java.math.BigDecimal;
7
8 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
9 import org.forester.util.ForesterUtil;
10
11 public class Point implements PhylogenyData {
12
13     private final String       _geodetic_datum;
14     private final BigDecimal   _lat;
15     private final BigDecimal   _long;
16     private final BigDecimal   _alt;
17     private final String       _alt_unit;
18     public static final String UNKNOWN_GEODETIC_DATUM = "?";
19
20     public Point() {
21         this( UNKNOWN_GEODETIC_DATUM, null, null, null, "" );
22     }
23
24     public Point( final String geodetic_datum, final BigDecimal lat, final BigDecimal longitude ) {
25         this( geodetic_datum, lat, longitude, null, "" );
26     }
27
28     public boolean isEmpty() {
29         return ( _lat == null ) && ( _long == null ) && ( _alt == null );
30     }
31
32     public Point( final String geodetic_datum,
33                   final BigDecimal lat,
34                   final BigDecimal longitude,
35                   final BigDecimal alt,
36                   final String alt_unit ) {
37         if ( ForesterUtil.isEmpty( geodetic_datum ) ) {
38             throw new IllegalArgumentException( "illegal attempt to use empty geodetic datum" );
39         }
40         if ( ( alt != null ) && ForesterUtil.isEmpty( alt_unit ) ) {
41             throw new IllegalArgumentException( "altitude must hava a unit" );
42         }
43         _geodetic_datum = geodetic_datum;
44         _lat = lat;
45         _long = longitude;
46         _alt = alt;
47         _alt_unit = alt_unit;
48     }
49
50     @Override
51     public StringBuffer asSimpleText() {
52         if ( isEmpty() ) {
53             return new StringBuffer();
54         }
55         else if ( getAltitude() == null ) {
56             return new StringBuffer( "[" + getLatitude().toPlainString() + ", " + getLongitude() + "]" );
57         }
58         else {
59             return new StringBuffer( "[" + getLatitude().toPlainString() + ", " + getLongitude() + ", " + getAltitude()
60                     + getAltiudeUnit() + "]" );
61         }
62     }
63
64     @Override
65     public StringBuffer asText() {
66         return asSimpleText();
67     }
68
69     @Override
70     public PhylogenyData copy() {
71         return new Point( getGeodeticDatum(),
72                           getLatitude() == null ? null : new BigDecimal( getLatitude().toPlainString() ),
73                           getLongitude() == null ? null : new BigDecimal( getLongitude().toPlainString() ),
74                           getAltitude() == null ? null : new BigDecimal( getAltitude().toPlainString() ),
75                           getAltiudeUnit() );
76     }
77
78     public BigDecimal getAltitude() {
79         return _alt;
80     }
81
82     public String getAltiudeUnit() {
83         return _alt_unit;
84     }
85
86     public String getGeodeticDatum() {
87         return _geodetic_datum;
88     }
89
90     public BigDecimal getLatitude() {
91         return _lat;
92     }
93
94     public BigDecimal getLongitude() {
95         return _long;
96     }
97
98     @Override
99     public boolean isEqual( final PhylogenyData point ) {
100         throw new UnsupportedOperationException();
101     }
102
103     @Override
104     public StringBuffer toNHX() {
105         throw new UnsupportedOperationException();
106     }
107
108     @Override
109     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
110         if ( isEmpty() ) {
111             return;
112         }
113         writer.write( ForesterUtil.LINE_SEPARATOR );
114         writer.write( indentation );
115         if ( getAltitude() != null ) {
116             PhylogenyDataUtil.appendOpen( writer,
117                                           PhyloXmlMapping.POINT,
118                                           PhyloXmlMapping.POINT_GEODETIC_DATUM,
119                                           getGeodeticDatum(),
120                                           PhyloXmlMapping.POINT_ALTITUDE_UNIT_ATTR,
121                                           getAltiudeUnit() );
122         }
123         else {
124             PhylogenyDataUtil.appendOpen( writer,
125                                           PhyloXmlMapping.POINT,
126                                           PhyloXmlMapping.POINT_GEODETIC_DATUM,
127                                           getGeodeticDatum() );
128         }
129         PhylogenyDataUtil.appendElement( writer,
130                                          PhyloXmlMapping.POINT_LATITUDE,
131                                          getLatitude().toPlainString(),
132                                          indentation );
133         PhylogenyDataUtil.appendElement( writer,
134                                          PhyloXmlMapping.POINT_LONGITUDE,
135                                          getLongitude().toPlainString(),
136                                          indentation );
137         if ( getAltitude() != null ) {
138             PhylogenyDataUtil.appendElement( writer,
139                                              PhyloXmlMapping.POINT_ALTITUDE,
140                                              getAltitude().toPlainString(),
141                                              indentation );
142         }
143         writer.write( ForesterUtil.LINE_SEPARATOR );
144         writer.write( indentation );
145         PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.POINT );
146     }
147
148     @Override
149     public String toString() {
150         return asSimpleText().toString();
151     }
152
153     static public final boolean isSeemsEmpty( final Point p ) {
154         return ( ( ( p.getAltitude() == null ) || ( p.getAltitude().compareTo( BigDecimal.ZERO ) <= 0 ) )
155                 && ( ( p.getLongitude() == null ) || ( p.getLongitude().compareTo( BigDecimal.ZERO ) <= 0 ) )
156                 && ( ( p.getLatitude() == null ) || ( p.getLatitude().compareTo( BigDecimal.ZERO ) <= 0 ) )
157                 && ( ForesterUtil.isEmpty( p.getGeodeticDatum() ) || p.getGeodeticDatum()
158                         .equalsIgnoreCase( UNKNOWN_GEODETIC_DATUM ) ) && ( ForesterUtil.isEmpty( p.getAltiudeUnit() ) || p
159                 .getAltiudeUnit().equalsIgnoreCase( "?" ) ) );
160     }
161 }