001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: /* @generated Thu Sep 27 15:20:43 EDT 2007 from /u01/builds/cougaar/B12_4/B12_4/070927151721/src/planning/src/org/cougaar/planning/ldm/measure/measures.def - DO NOT HAND EDIT */
028: /** Immutable implementation of Duration.
029: **/package org.cougaar.planning.ldm.measure;
030:
031: import java.io.*;
032:
033: public final class Duration extends Scalar implements Externalizable {
034: // Conversion factor constants
035: public static final double SECONDS_PER_MINUTES = 60;
036: public static final double MINUTES_PER_SECONDS = (1.0d / 60);
037: public static final double SECONDS_PER_HOURS = 3600;
038: public static final double HOURS_PER_SECONDS = (1.0d / 3600);
039: public static final double SECONDS_PER_DAYS = 86400;
040: public static final double DAYS_PER_SECONDS = (1.0d / 86400);
041: public static final double SECONDS_PER_WEEKS = 604800;
042: public static final double WEEKS_PER_SECONDS = (1.0d / 604800);
043: public static final double SECONDS_PER_MILLISECONDS = (1.0d / 1000);
044: public static final double MILLISECONDS_PER_SECONDS = 1000;
045: public static final double SECONDS_PER_KILOSECONDS = 1000;
046: public static final double KILOSECONDS_PER_SECONDS = (1.0d / 1000);
047: public static final double SECONDS_PER_MONTHS = 2629743.8;
048: public static final double MONTHS_PER_SECONDS = (1.0d / 2629743.8);
049: public static final double SECONDS_PER_YEARS = 31556926;
050: public static final double YEARS_PER_SECONDS = (1.0d / 31556926);
051: public static final double SECONDS_PER_FORTNIGHTS = 1209600;
052: public static final double FORTNIGHTS_PER_SECONDS = (1.0d / 1209600);
053:
054: // the value is stored as seconds
055: private double theValue;
056:
057: /** No-arg constructor is only for use by serialization **/
058: public Duration() {
059: }
060:
061: // private constructor
062: private Duration(double v) {
063: theValue = v;
064: }
065:
066: /** parameterized constructor **/
067: public Duration(double v, int unit) {
068: if (unit >= 0 && unit <= MAXUNIT)
069: theValue = v * getConvFactor(unit);
070: else
071: throw new UnknownUnitException();
072: }
073:
074: /** takes strings of the form "Number unit" **/
075: public Duration(String s) {
076: int i = indexOfType(s);
077: if (i < 0)
078: throw new UnknownUnitException();
079: double n = Double.valueOf(s.substring(0, i).trim())
080: .doubleValue();
081: String u = s.substring(i).trim().toLowerCase();
082: if (u.equals("seconds"))
083: theValue = n;
084: else if (u.equals("minutes"))
085: theValue = n * SECONDS_PER_MINUTES;
086: else if (u.equals("hours"))
087: theValue = n * SECONDS_PER_HOURS;
088: else if (u.equals("days"))
089: theValue = n * SECONDS_PER_DAYS;
090: else if (u.equals("weeks"))
091: theValue = n * SECONDS_PER_WEEKS;
092: else if (u.equals("milliseconds"))
093: theValue = n * SECONDS_PER_MILLISECONDS;
094: else if (u.equals("kiloseconds"))
095: theValue = n * SECONDS_PER_KILOSECONDS;
096: else if (u.equals("months"))
097: theValue = n * SECONDS_PER_MONTHS;
098: else if (u.equals("years"))
099: theValue = n * SECONDS_PER_YEARS;
100: else if (u.equals("fortnights"))
101: theValue = n * SECONDS_PER_FORTNIGHTS;
102: else
103: throw new UnknownUnitException();
104: }
105:
106: // TypeNamed factory methods
107: public static final Duration newSeconds(double v) {
108: return new Duration(v);
109: }
110:
111: public static final Duration newSeconds(String s) {
112: return new Duration((Double.valueOf(s).doubleValue()));
113: }
114:
115: public static final Duration newMinutes(double v) {
116: return new Duration(v * SECONDS_PER_MINUTES);
117: }
118:
119: public static final Duration newMinutes(String s) {
120: return new Duration((Double.valueOf(s).doubleValue())
121: * SECONDS_PER_MINUTES);
122: }
123:
124: public static final Duration newHours(double v) {
125: return new Duration(v * SECONDS_PER_HOURS);
126: }
127:
128: public static final Duration newHours(String s) {
129: return new Duration((Double.valueOf(s).doubleValue())
130: * SECONDS_PER_HOURS);
131: }
132:
133: public static final Duration newDays(double v) {
134: return new Duration(v * SECONDS_PER_DAYS);
135: }
136:
137: public static final Duration newDays(String s) {
138: return new Duration((Double.valueOf(s).doubleValue())
139: * SECONDS_PER_DAYS);
140: }
141:
142: public static final Duration newWeeks(double v) {
143: return new Duration(v * SECONDS_PER_WEEKS);
144: }
145:
146: public static final Duration newWeeks(String s) {
147: return new Duration((Double.valueOf(s).doubleValue())
148: * SECONDS_PER_WEEKS);
149: }
150:
151: public static final Duration newMilliseconds(double v) {
152: return new Duration(v * SECONDS_PER_MILLISECONDS);
153: }
154:
155: public static final Duration newMilliseconds(String s) {
156: return new Duration((Double.valueOf(s).doubleValue())
157: * SECONDS_PER_MILLISECONDS);
158: }
159:
160: public static final Duration newKiloseconds(double v) {
161: return new Duration(v * SECONDS_PER_KILOSECONDS);
162: }
163:
164: public static final Duration newKiloseconds(String s) {
165: return new Duration((Double.valueOf(s).doubleValue())
166: * SECONDS_PER_KILOSECONDS);
167: }
168:
169: public static final Duration newMonths(double v) {
170: return new Duration(v * SECONDS_PER_MONTHS);
171: }
172:
173: public static final Duration newMonths(String s) {
174: return new Duration((Double.valueOf(s).doubleValue())
175: * SECONDS_PER_MONTHS);
176: }
177:
178: public static final Duration newYears(double v) {
179: return new Duration(v * SECONDS_PER_YEARS);
180: }
181:
182: public static final Duration newYears(String s) {
183: return new Duration((Double.valueOf(s).doubleValue())
184: * SECONDS_PER_YEARS);
185: }
186:
187: public static final Duration newFortnights(double v) {
188: return new Duration(v * SECONDS_PER_FORTNIGHTS);
189: }
190:
191: public static final Duration newFortnights(String s) {
192: return new Duration((Double.valueOf(s).doubleValue())
193: * SECONDS_PER_FORTNIGHTS);
194: }
195:
196: public int getCommonUnit() {
197: return HOURS;
198: }
199:
200: public int getMaxUnit() {
201: return MAXUNIT;
202: }
203:
204: // unit names for getUnitName
205: private static final String unitNames[] = { "seconds", "minutes",
206: "hours", "days", "weeks", "milliseconds", "kiloseconds",
207: "months", "years", "fortnights", };
208:
209: public String getUnitName(int unit) {
210: return unitNames[unit];
211: }
212:
213: // Index Typed factory methods
214: static final double convFactor[] = { 1.0, SECONDS_PER_MINUTES,
215: SECONDS_PER_HOURS, SECONDS_PER_DAYS, SECONDS_PER_WEEKS,
216: SECONDS_PER_MILLISECONDS, SECONDS_PER_KILOSECONDS,
217: SECONDS_PER_MONTHS, SECONDS_PER_YEARS,
218: SECONDS_PER_FORTNIGHTS, };
219:
220: public static final double getConvFactor(int i) {
221: return convFactor[i];
222: }
223:
224: // indexes into factor array
225: public static final int SECONDS = 0;
226: public static final int MINUTES = 1;
227: public static final int HOURS = 2;
228: public static final int DAYS = 3;
229: public static final int WEEKS = 4;
230: public static final int MILLISECONDS = 5;
231: public static final int KILOSECONDS = 6;
232: public static final int MONTHS = 7;
233: public static final int YEARS = 8;
234: public static final int FORTNIGHTS = 9;
235: public static final int MAXUNIT = 9;
236:
237: // Index Typed factory methods
238: public static final Duration newDuration(double v, int unit) {
239: if (unit >= 0 && unit <= MAXUNIT)
240: return new Duration(v * getConvFactor(unit));
241: else
242: throw new UnknownUnitException();
243: }
244:
245: public static final Duration newDuration(String s, int unit) {
246: if (unit >= 0 && unit <= MAXUNIT)
247: return new Duration((Double.valueOf(s).doubleValue())
248: * getConvFactor(unit));
249: else
250: throw new UnknownUnitException();
251: }
252:
253: // Support for AbstractMeasure-level constructor
254: public static final AbstractMeasure newMeasure(String s, int unit) {
255: return newDuration(s, unit);
256: }
257:
258: public static final AbstractMeasure newMeasure(double v, int unit) {
259: return newDuration(v, unit);
260: }
261:
262: // simple math : addition and subtraction
263: public final Measure add(Measure toAdd) {
264: if (!(toAdd instanceof Duration))
265: throw new IllegalArgumentException();
266: return new Duration(theValue + toAdd.getNativeValue());
267: }
268:
269: public final Measure subtract(Measure toSubtract) {
270: if (!(toSubtract instanceof Duration))
271: throw new IllegalArgumentException();
272: return new Duration(theValue - toSubtract.getNativeValue());
273: }
274:
275: public final Measure scale(double scale) {
276: return new Duration(theValue * scale, 0);
277: }
278:
279: public final Measure negate() {
280: return newDuration(-1 * theValue, 0);
281: }
282:
283: public final Measure floor(int unit) {
284: return newDuration(Math.floor(getValue(unit)), 0);
285: }
286:
287: public final Measure valueOf(double value) {
288: return new Duration(value);
289: }
290:
291: public final Measure valueOf(double value, int unit) {
292: return new Duration(value, unit);
293: }
294:
295: public final double getNativeValue() {
296: return theValue;
297: }
298:
299: public final int getNativeUnit() {
300: return 0;
301: }
302:
303: public final Duration divide(Rate toRate) {
304: Measure canonicalNumerator = toRate.getCanonicalNumerator();
305: if (!(toRate.getCanonicalNumerator() instanceof Duration)) {
306: throw new IllegalArgumentException(
307: "Expecting a Duration/Duration");
308: }
309: int durationNativeUnit = toRate.getCanonicalDenominator()
310: .getNativeUnit(); // seconds
311: double value = toRate.getValue(canonicalNumerator
312: .getNativeUnit(), durationNativeUnit); // ?/seconds
313: return new Duration(theValue / value, durationNativeUnit); // ?/?/second = seconds
314: }
315:
316: // Unit-based Reader methods
317: public double getSeconds() {
318: return (theValue);
319: }
320:
321: public double getMinutes() {
322: return (theValue / SECONDS_PER_MINUTES);
323: }
324:
325: public double getHours() {
326: return (theValue / SECONDS_PER_HOURS);
327: }
328:
329: public double getDays() {
330: return (theValue / SECONDS_PER_DAYS);
331: }
332:
333: public double getWeeks() {
334: return (theValue / SECONDS_PER_WEEKS);
335: }
336:
337: public double getMilliseconds() {
338: return (theValue / SECONDS_PER_MILLISECONDS);
339: }
340:
341: public double getKiloseconds() {
342: return (theValue / SECONDS_PER_KILOSECONDS);
343: }
344:
345: public double getMonths() {
346: return (theValue / SECONDS_PER_MONTHS);
347: }
348:
349: public double getYears() {
350: return (theValue / SECONDS_PER_YEARS);
351: }
352:
353: public double getFortnights() {
354: return (theValue / SECONDS_PER_FORTNIGHTS);
355: }
356:
357: public double getValue(int unit) {
358: if (unit >= 0 && unit <= MAXUNIT)
359: return (theValue / getConvFactor(unit));
360: else
361: throw new UnknownUnitException();
362: }
363:
364: public boolean equals(Object o) {
365: return (o instanceof Duration && theValue == ((Duration) o).theValue);
366: }
367:
368: public String toString() {
369: return Double.toString(theValue) + "s";
370: }
371:
372: public int hashCode() {
373: return (new Double(theValue)).hashCode();
374: }
375:
376: // serialization
377: public void writeExternal(ObjectOutput out) throws IOException {
378: out.writeDouble(theValue);
379: }
380:
381: public void readExternal(ObjectInput in) throws IOException {
382: theValue = in.readDouble();
383: }
384: }
|