Debian Codegraph @

src/dr/evolution/util/TimeScale.java

raw symbols SCIP
1 /*
2 * TimeScale.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evolution.util;
27
28 /**
29 * Class for defining and converting between time scales
30 *
31 * @version $Id: TimeScale.java,v 1.11 2005/05/24 20:25:57 rambaut Exp $
32 *
33 * @author Alexei Drummond
34 * @author Andrew Rambaut
35 */
36 public class TimeScale implements Units {
37
38 /**
39 * Constructor for a timescale that starts at Jan 1st 1970. This constructor
40 * can be used if we are not interested in absolute time.
41 * param units The units
42 * param backwards True if the timescale goes backwards in time
43 */
44 public TimeScale(Type units, boolean backwards) {
45 this(units, backwards, 0.0);
46 }
47
48 /**
49 * Constructor for a timescale that starts at origin
50 * param units The units
51 * param backwards True if the timescale goes backwards in time
52 * param origin The origin specified relative to 1970 in same units
53 */
54 public TimeScale(Type units, boolean backwards, double origin) {
55 this.units = units;
56 this.backwards = backwards;
57 this.origin = origin;
58 }
59
60 /**
61 * Constructor for a timescale that starts at origin
62 * param units The units
63 * param backwards True if the timescale goes backwards in time
64 * param origin The origin specified as a date
65 */
66 public TimeScale(Type units, boolean backwards, java.util.Date origin) {
67 this.units = units;
68 this.backwards = backwards;
69
70 long millisAhead = origin.getTime();
71
72 double daysAhead = ((double)millisAhead)/MILLIS_PER_DAY;
73
74 switch (units) {
75 case DAYS: this.origin = daysAhead; break;
76 case MONTHS: this.origin = daysAhead / DAYS_PER_MONTH; break;
77 case YEARS: this.origin = daysAhead / DAYS_PER_YEAR; break;
78 default: throw new IllegalArgumentException();
79 }
80
81 }
82
83 /**
84 * @return the units of this timescale
85 */
86 public Type getUnits() { return units; }
87
88 /**
89 * Sets the units for this timescale.
90 */
91 public void setUnits(Type units) { this.units = units; }
92
93 /**
94 * @return true if larger numbers represent older dates in this time scale.
95 */
96 public boolean isBackwards() { return backwards; }
97
98 /**
99 * @return the date corresponding to the value zero in this time scale.
100 */
101 public double getOrigin() { return origin; }
102
103 /**
104 * @return a time given in timescale scale as a time in this timescale
105 */
106 public double convertTime(double time, TimeScale timeScale) {
107
108 // make it forwards
109 if (timeScale.isBackwards()) time = -time;
110
111 // make it absolute
112 time += timeScale.getOrigin();
113
114 // convert to the new timescale units
115 double newTime = convertTimeUnits(time, getUnits(), timeScale.getUnits());
116
117 // make it relative
118 newTime -= origin;
119
120 // make it backwards if required
121 if (backwards) newTime = -newTime;
122
123 return newTime;
124 }
125
126 public String toString() {
127
128 StringBuffer buffer = new StringBuffer("timescale(");
129 buffer.append(unitString(0.0));
130 if (backwards) {
131 buffer.append(", backwards");
132 } else {
133 buffer.append(", forewards");
134 }
135 buffer.append(" from " + origin + ")");
136
137 return buffer.toString();
138 }
139
140 public String unitString(double time) {
141 String unitString = null;
142 switch (units) {
143 case DAYS: unitString = "day"; break;
144 case MONTHS: unitString = "month"; break;
145 case YEARS: unitString = "year"; break;
146 default: throw new IllegalArgumentException();
147 }
148 if (time == 1.0) {
149 return unitString;
150 } else return unitString + "s";
151 }
152
153 public static void main(String[] args) {
154
155 TimeScale timeScale1 = new TimeScale(Units.Type.DAYS, true);
156 TimeScale timeScale2 = new TimeScale(Units.Type.YEARS, true);
157
158 System.out.println(timeScale1);
159 System.out.println(timeScale2);
160
161 double testTime = 100.0;
162 System.out.println("Test time = " + testTime);
163
164 System.out.println("timeScale1.convertTime(" + testTime + ", timeScale2)=" + timeScale1.convertTime(testTime, timeScale2));
165 System.out.println("timeScale2.convertTime(" + testTime + ", timeScale1)=" + timeScale2.convertTime(testTime, timeScale1));
166 }
167
168
169 //*************************************************************************
170 // STATIC STUFF
171 //*************************************************************************
172
173 /**
174 * @return time in currentUnits as newUnits.
175 */
176 public static double convertTimeUnits(double time, Type currentUnits, Type newUnits) {
177
178 return time * getScale(currentUnits, newUnits);
179 }
180
181 /**
182 * @return the scaling factor for converting currentUnits into newUnits.
183 */
184 public static double getScale(Type currentUnits, Type newUnits) {
185 if (currentUnits == newUnits) return 1.0;
186
187 switch (currentUnits) {
188 case DAYS:
189 switch (newUnits) {
190 case MONTHS: return 1.0/DAYS_PER_MONTH;
191 case YEARS: return 1.0/DAYS_PER_YEAR;
192 default: throw new IllegalArgumentException();
193 }
194 case MONTHS:
195 switch (newUnits) {
196 case DAYS: return DAYS_PER_MONTH;
197 case YEARS: return 1.0/MONTHS_PER_YEAR;
198 default: throw new IllegalArgumentException();
199 }
200 case YEARS:
201 switch (newUnits) {
202 case DAYS: return DAYS_PER_YEAR;
203 case MONTHS: return MONTHS_PER_YEAR;
204 default: throw new IllegalArgumentException();
205 }
206 default: throw new IllegalArgumentException();
207 }
208 }
209
210 //*************************************************************************
211 // PRIVATE STUFF
212 //*************************************************************************
213
214 // The origin is specified in days relative to 1st January 1970
215 protected double origin = 720035.0;
216 protected Type units;
217 protected boolean backwards;
218
219 protected static double MILLIS_PER_DAY = 86400000.0;
220 protected static double DAYS_PER_YEAR = 365.25;
221 protected static double MONTHS_PER_YEAR = 12.0;
222 protected static double DAYS_PER_MONTH = DAYS_PER_YEAR / MONTHS_PER_YEAR;
223 }