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 AbstractRate.
029: **/package org.cougaar.planning.ldm.measure;
030:
031: import java.io.*;
032:
033: /** @deprecated Use a real Rate Measure instead **/
034: public final class AbstractRate extends AbstractMeasure implements
035: Externalizable {
036: // Conversion factor constants
037: public static final double PER_SECOND_PER_PER_MINUTE = (1.0d / 60);
038: public static final double PER_MINUTE_PER_PER_SECOND = 60;
039: public static final double PER_SECOND_PER_PER_HOUR = (1.0d / 3600);
040: public static final double PER_HOUR_PER_PER_SECOND = 3600;
041: public static final double PER_SECOND_PER_PER_DAY = (1.0d / 86400);
042: public static final double PER_DAY_PER_PER_SECOND = 86400;
043: public static final double PER_SECOND_PER_PER_WEEK = (1.0d / 604800);
044: public static final double PER_WEEK_PER_PER_SECOND = 604800;
045: public static final double PER_SECOND_PER_PER_MILLISECOND = 1000;
046: public static final double PER_MILLISECOND_PER_PER_SECOND = (1.0d / 1000);
047: public static final double PER_SECOND_PER_PER_KILOSECOND = (1.0d / 1000);
048: public static final double PER_KILOSECOND_PER_PER_SECOND = 1000;
049:
050: // the value is stored as per_second
051: private double theValue;
052:
053: /** No-arg constructor is only for use by serialization **/
054: public AbstractRate() {
055: }
056:
057: // private constructor
058: private AbstractRate(double v) {
059: theValue = v;
060: }
061:
062: /** parameterized constructor **/
063: public AbstractRate(double v, int unit) {
064: if (unit >= 0 && unit <= MAXUNIT)
065: theValue = v * getConvFactor(unit);
066: else
067: throw new UnknownUnitException();
068: }
069:
070: /** takes strings of the form "Number unit" **/
071: public AbstractRate(String s) {
072: int i = indexOfType(s);
073: if (i < 0)
074: throw new UnknownUnitException();
075: double n = Double.valueOf(s.substring(0, i).trim())
076: .doubleValue();
077: String u = s.substring(i).trim().toLowerCase();
078: if (u.equals("persecond"))
079: theValue = n;
080: else if (u.equals("perminute"))
081: theValue = n * PER_SECOND_PER_PER_MINUTE;
082: else if (u.equals("perhour"))
083: theValue = n * PER_SECOND_PER_PER_HOUR;
084: else if (u.equals("perday"))
085: theValue = n * PER_SECOND_PER_PER_DAY;
086: else if (u.equals("perweek"))
087: theValue = n * PER_SECOND_PER_PER_WEEK;
088: else if (u.equals("permillisecond"))
089: theValue = n * PER_SECOND_PER_PER_MILLISECOND;
090: else if (u.equals("perkilosecond"))
091: theValue = n * PER_SECOND_PER_PER_KILOSECOND;
092: else
093: throw new UnknownUnitException();
094: }
095:
096: // TypeNamed factory methods
097: public static final AbstractRate newPerSecond(double v) {
098: return new AbstractRate(v);
099: }
100:
101: public static final AbstractRate newPerSecond(String s) {
102: return new AbstractRate((Double.valueOf(s).doubleValue()));
103: }
104:
105: public static final AbstractRate newPerMinute(double v) {
106: return new AbstractRate(v * PER_SECOND_PER_PER_MINUTE);
107: }
108:
109: public static final AbstractRate newPerMinute(String s) {
110: return new AbstractRate((Double.valueOf(s).doubleValue())
111: * PER_SECOND_PER_PER_MINUTE);
112: }
113:
114: public static final AbstractRate newPerHour(double v) {
115: return new AbstractRate(v * PER_SECOND_PER_PER_HOUR);
116: }
117:
118: public static final AbstractRate newPerHour(String s) {
119: return new AbstractRate((Double.valueOf(s).doubleValue())
120: * PER_SECOND_PER_PER_HOUR);
121: }
122:
123: public static final AbstractRate newPerDay(double v) {
124: return new AbstractRate(v * PER_SECOND_PER_PER_DAY);
125: }
126:
127: public static final AbstractRate newPerDay(String s) {
128: return new AbstractRate((Double.valueOf(s).doubleValue())
129: * PER_SECOND_PER_PER_DAY);
130: }
131:
132: public static final AbstractRate newPerWeek(double v) {
133: return new AbstractRate(v * PER_SECOND_PER_PER_WEEK);
134: }
135:
136: public static final AbstractRate newPerWeek(String s) {
137: return new AbstractRate((Double.valueOf(s).doubleValue())
138: * PER_SECOND_PER_PER_WEEK);
139: }
140:
141: public static final AbstractRate newPerMillisecond(double v) {
142: return new AbstractRate(v * PER_SECOND_PER_PER_MILLISECOND);
143: }
144:
145: public static final AbstractRate newPerMillisecond(String s) {
146: return new AbstractRate((Double.valueOf(s).doubleValue())
147: * PER_SECOND_PER_PER_MILLISECOND);
148: }
149:
150: public static final AbstractRate newPerKilosecond(double v) {
151: return new AbstractRate(v * PER_SECOND_PER_PER_KILOSECOND);
152: }
153:
154: public static final AbstractRate newPerKilosecond(String s) {
155: return new AbstractRate((Double.valueOf(s).doubleValue())
156: * PER_SECOND_PER_PER_KILOSECOND);
157: }
158:
159: public int getCommonUnit() {
160: return PER_HOUR;
161: }
162:
163: public int getMaxUnit() {
164: return MAXUNIT;
165: }
166:
167: // unit names for getUnitName
168: private static final String unitNames[] = { "per_second",
169: "per_minute", "per_hour", "per_day", "per_week",
170: "per_millisecond", "per_kilosecond", };
171:
172: public String getUnitName(int unit) {
173: return unitNames[unit];
174: }
175:
176: // Index Typed factory methods
177: static final double convFactor[] = { 1.0,
178: PER_SECOND_PER_PER_MINUTE, PER_SECOND_PER_PER_HOUR,
179: PER_SECOND_PER_PER_DAY, PER_SECOND_PER_PER_WEEK,
180: PER_SECOND_PER_PER_MILLISECOND,
181: PER_SECOND_PER_PER_KILOSECOND, };
182:
183: public static final double getConvFactor(int i) {
184: return convFactor[i];
185: }
186:
187: // indexes into factor array
188: public static final int PER_SECOND = 0;
189: public static final int PER_MINUTE = 1;
190: public static final int PER_HOUR = 2;
191: public static final int PER_DAY = 3;
192: public static final int PER_WEEK = 4;
193: public static final int PER_MILLISECOND = 5;
194: public static final int PER_KILOSECOND = 6;
195: public static final int MAXUNIT = 6;
196:
197: // Index Typed factory methods
198: public static final AbstractRate newAbstractRate(double v, int unit) {
199: if (unit >= 0 && unit <= MAXUNIT)
200: return new AbstractRate(v * getConvFactor(unit));
201: else
202: throw new UnknownUnitException();
203: }
204:
205: public static final AbstractRate newAbstractRate(String s, int unit) {
206: if (unit >= 0 && unit <= MAXUNIT)
207: return new AbstractRate((Double.valueOf(s).doubleValue())
208: * getConvFactor(unit));
209: else
210: throw new UnknownUnitException();
211: }
212:
213: // Support for AbstractMeasure-level constructor
214: public static final AbstractMeasure newMeasure(String s, int unit) {
215: return newAbstractRate(s, unit);
216: }
217:
218: public static final AbstractMeasure newMeasure(double v, int unit) {
219: return newAbstractRate(v, unit);
220: }
221:
222: // simple math : addition and subtraction
223: public final Measure add(Measure toAdd) {
224: if (!(toAdd instanceof AbstractRate))
225: throw new IllegalArgumentException();
226: return new AbstractRate(theValue + toAdd.getNativeValue());
227: }
228:
229: public final Measure subtract(Measure toSubtract) {
230: if (!(toSubtract instanceof AbstractRate))
231: throw new IllegalArgumentException();
232: return new AbstractRate(theValue - toSubtract.getNativeValue());
233: }
234:
235: public final Measure scale(double scale) {
236: return new AbstractRate(theValue * scale, 0);
237: }
238:
239: public final Measure negate() {
240: return newAbstractRate(-1 * theValue, 0);
241: }
242:
243: public final Measure floor(int unit) {
244: return newAbstractRate(Math.floor(getValue(unit)), 0);
245: }
246:
247: public final Measure valueOf(double value) {
248: return new AbstractRate(value);
249: }
250:
251: public final Measure valueOf(double value, int unit) {
252: return new AbstractRate(value, unit);
253: }
254:
255: public final double getNativeValue() {
256: return theValue;
257: }
258:
259: public final int getNativeUnit() {
260: return 0;
261: }
262:
263: public final Duration divide(Rate toRate) {
264: Measure canonicalNumerator = toRate.getCanonicalNumerator();
265: if (!(toRate.getCanonicalNumerator() instanceof AbstractRate)) {
266: throw new IllegalArgumentException(
267: "Expecting a AbstractRate/Duration");
268: }
269: int durationNativeUnit = toRate.getCanonicalDenominator()
270: .getNativeUnit(); // seconds
271: double value = toRate.getValue(canonicalNumerator
272: .getNativeUnit(), durationNativeUnit); // ?/seconds
273: return new Duration(theValue / value, durationNativeUnit); // ?/?/second = seconds
274: }
275:
276: // Unit-based Reader methods
277: public double getPerSecond() {
278: return (theValue);
279: }
280:
281: public double getPerMinute() {
282: return (theValue / PER_SECOND_PER_PER_MINUTE);
283: }
284:
285: public double getPerHour() {
286: return (theValue / PER_SECOND_PER_PER_HOUR);
287: }
288:
289: public double getPerDay() {
290: return (theValue / PER_SECOND_PER_PER_DAY);
291: }
292:
293: public double getPerWeek() {
294: return (theValue / PER_SECOND_PER_PER_WEEK);
295: }
296:
297: public double getPerMillisecond() {
298: return (theValue / PER_SECOND_PER_PER_MILLISECOND);
299: }
300:
301: public double getPerKilosecond() {
302: return (theValue / PER_SECOND_PER_PER_KILOSECOND);
303: }
304:
305: public double getValue(int unit) {
306: if (unit >= 0 && unit <= MAXUNIT)
307: return (theValue / getConvFactor(unit));
308: else
309: throw new UnknownUnitException();
310: }
311:
312: public boolean equals(Object o) {
313: return (o instanceof AbstractRate && theValue == ((AbstractRate) o).theValue);
314: }
315:
316: public String toString() {
317: return Double.toString(theValue) + "1/s";
318: }
319:
320: public int hashCode() {
321: return (new Double(theValue)).hashCode();
322: }
323:
324: // serialization
325: public void writeExternal(ObjectOutput out) throws IOException {
326: out.writeDouble(theValue);
327: }
328:
329: public void readExternal(ObjectInput in) throws IOException {
330: theValue = in.readDouble();
331: }
332: }
|