Added getValues and write to DistanceMatrix interface for broader use
[jalview.git] / forester / java / src / org / forester / phylogeny / data / Date.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: https://sites.google.com/site/cmzmasek/home/software/forester
27
28 package org.forester.phylogeny.data;
29
30 import java.io.IOException;
31 import java.io.Writer;
32 import java.math.BigDecimal;
33
34 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
35 import org.forester.util.ForesterUtil;
36
37 public class Date implements PhylogenyData {
38
39     private String     _desc;
40     private BigDecimal _value;
41     private BigDecimal _min;
42     private BigDecimal _max;
43     private String     _unit;
44
45     public Date() {
46         _desc = "";
47         _value = null;
48         _min = null;
49         _max = null;
50         _unit = "";
51     }
52
53     public Date( final String desc ) {
54         if ( desc == null ) {
55             throw new IllegalArgumentException( "illegaly empty of null fields in constructor" );
56         }
57         _desc = desc;
58         _value = null;
59         _min = null;
60         _max = null;
61         _unit = "";
62     }
63
64     public Date( final String desc,
65                  final BigDecimal value,
66                  final BigDecimal min,
67                  final BigDecimal max,
68                  final String unit ) {
69         if ( ( desc == null ) || ( unit == null ) ) {
70             throw new IllegalArgumentException( "illegaly empty of null fields in constructor" );
71         }
72         _desc = desc;
73         _value = value;
74         _min = min;
75         _max = max;
76         _unit = unit;
77     }
78
79     @Override
80     public StringBuffer asSimpleText() {
81         if ( getValue() != null ) {
82             return new StringBuffer( getDesc() + " [" + getValue().toPlainString() + " " + getUnit() + "]" );
83         }
84         else {
85             return new StringBuffer( getDesc() );
86         }
87     }
88
89     @Override
90     public StringBuffer asText() {
91         return asSimpleText();
92     }
93
94     @Override
95     public PhylogenyData copy() {
96         return new Date( getDesc(),
97                          getValue() == null ? null : new BigDecimal( getValue().toPlainString() ),
98                                  getMin() == null ? null : new BigDecimal( getMin().toPlainString() ),
99                                          getMax() == null ? null : new BigDecimal( getMax().toPlainString() ),
100                                                  getUnit() );
101     }
102
103     public String getDesc() {
104         return _desc;
105     }
106
107     public BigDecimal getMax() {
108         return _max;
109     }
110
111     public BigDecimal getMin() {
112         return _min;
113     }
114
115     public String getUnit() {
116         return _unit;
117     }
118
119     public BigDecimal getValue() {
120         return _value;
121     }
122
123     @Override
124     public boolean isEqual( final PhylogenyData data ) {
125         throw new UnsupportedOperationException();
126     }
127
128     public void setDesc( final String desc ) {
129         _desc = desc;
130     }
131
132     public void setMax( final BigDecimal max ) {
133         _max = max;
134     }
135
136     public void setMin( final BigDecimal min ) {
137         _min = min;
138     }
139
140     public void setUnit( final String unit ) {
141         _unit = unit;
142     }
143
144     public void setValue( final BigDecimal value ) {
145         _value = value;
146     }
147
148     @Override
149     public StringBuffer toNHX() {
150         throw new UnsupportedOperationException();
151     }
152
153     @Override
154     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
155         writer.write( ForesterUtil.LINE_SEPARATOR );
156         writer.write( indentation );
157         PhylogenyDataUtil.appendOpen( writer, PhyloXmlMapping.CLADE_DATE, PhyloXmlMapping.CLADE_DATE_UNIT, getUnit() );
158         if ( !ForesterUtil.isEmpty( getDesc() ) ) {
159             PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.CLADE_DATE_DESC, getDesc(), indentation );
160         }
161         if ( getValue() != null ) {
162             PhylogenyDataUtil.appendElement( writer,
163                                              PhyloXmlMapping.CLADE_DATE_VALUE,
164                                              getValue().toPlainString(),
165                                              indentation );
166         }
167         if ( getMin() != null ) {
168             PhylogenyDataUtil.appendElement( writer,
169                                              PhyloXmlMapping.CLADE_DATE_MIN,
170                                              getMin().toPlainString(),
171                                              indentation );
172         }
173         if ( getMax() != null ) {
174             PhylogenyDataUtil.appendElement( writer,
175                                              PhyloXmlMapping.CLADE_DATE_MAX,
176                                              getMax().toPlainString(),
177                                              indentation );
178         }
179         writer.write( ForesterUtil.LINE_SEPARATOR );
180         writer.write( indentation );
181         PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.CLADE_DATE );
182     }
183
184     @Override
185     public String toString() {
186         return asSimpleText().toString();
187     }
188 }