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 CostRate.
029: **/package org.cougaar.planning.ldm.measure;
030:
031: import java.io.*;
032:
033: public final class CostRate extends AbstractMeasure implements
034: Externalizable, Derivative, Rate {
035: // the value is stored as dollars/second
036: private double theValue;
037:
038: /** No-arg constructor is only for use by serialization **/
039: public CostRate() {
040: }
041:
042: // private constructor
043: private CostRate(double v) {
044: theValue = v;
045: }
046:
047: /** @param unit One of the constant units of CostRate **/
048: public CostRate(double v, int unit) {
049: if (unit >= 0 && unit <= MAXUNIT)
050: theValue = v / getConvFactor(unit);
051: else
052: throw new UnknownUnitException();
053: }
054:
055: /** @param unit1 One of the constant units of Cost
056: * @param unit2 One of the constant units of Duration
057: **/
058: public CostRate(double v, int unit1, int unit2) {
059: if (unit1 >= 0 && unit1 <= Cost.MAXUNIT && unit2 >= 0
060: && unit2 <= Duration.MAXUNIT)
061: theValue = v * Cost.getConvFactor(unit1)
062: / Duration.getConvFactor(unit2);
063: else
064: throw new UnknownUnitException();
065: }
066:
067: /** @param num An instance of Cost to use as numerator
068: * @param den An instance of Durationto use as denominator
069: **/
070: public CostRate(Cost num, Duration den) {
071: theValue = num.getValue(0) / den.getValue(0);
072: }
073:
074: /** takes strings of the form "Number unit" **/
075: public CostRate(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("dollarspersecond"))
083: theValue = n / (1.0d / 1.0d);
084: else if (u.equals("dollarsperminute"))
085: theValue = n / (1.0d / (1.0d / 60));
086: else if (u.equals("dollarsperhour"))
087: theValue = n / (1.0d / (1.0d / 3600));
088: else if (u.equals("dollarsperday"))
089: theValue = n / (1.0d / (1.0d / 86400));
090: else if (u.equals("dollarsperweek"))
091: theValue = n / (1.0d / (1.0d / 604800));
092: else if (u.equals("dollarspermillisecond"))
093: theValue = n / (1.0d / 1000);
094: else if (u.equals("dollarsperkilosecond"))
095: theValue = n / (1.0d / (1.0d / 1000));
096: else if (u.equals("dollarspermonth"))
097: theValue = n / (1.0d / (1.0d / 2629743.8));
098: else if (u.equals("dollarsperyear"))
099: theValue = n / (1.0d / (1.0d / 31556926));
100: else if (u.equals("dollarsperfortnight"))
101: theValue = n / (1.0d / (1.0d / 1209600));
102: else
103: throw new UnknownUnitException();
104: }
105:
106: // TypeNamed factory methods
107: public static final CostRate newDollarsPerSecond(double v) {
108: return new CostRate(v * (1.0d / (1.0d / 1.0d)));
109: }
110:
111: public static final CostRate newDollarsPerSecond(String s) {
112: return new CostRate((Double.valueOf(s).doubleValue())
113: * (1.0d / (1.0d / 1.0d)));
114: }
115:
116: public static final CostRate newDollarsPerMinute(double v) {
117: return new CostRate(v * (1.0d / (1.0d / (1.0d / 60))));
118: }
119:
120: public static final CostRate newDollarsPerMinute(String s) {
121: return new CostRate((Double.valueOf(s).doubleValue())
122: * (1.0d / (1.0d / (1.0d / 60))));
123: }
124:
125: public static final CostRate newDollarsPerHour(double v) {
126: return new CostRate(v * (1.0d / (1.0d / (1.0d / 3600))));
127: }
128:
129: public static final CostRate newDollarsPerHour(String s) {
130: return new CostRate((Double.valueOf(s).doubleValue())
131: * (1.0d / (1.0d / (1.0d / 3600))));
132: }
133:
134: public static final CostRate newDollarsPerDay(double v) {
135: return new CostRate(v * (1.0d / (1.0d / (1.0d / 86400))));
136: }
137:
138: public static final CostRate newDollarsPerDay(String s) {
139: return new CostRate((Double.valueOf(s).doubleValue())
140: * (1.0d / (1.0d / (1.0d / 86400))));
141: }
142:
143: public static final CostRate newDollarsPerWeek(double v) {
144: return new CostRate(v * (1.0d / (1.0d / (1.0d / 604800))));
145: }
146:
147: public static final CostRate newDollarsPerWeek(String s) {
148: return new CostRate((Double.valueOf(s).doubleValue())
149: * (1.0d / (1.0d / (1.0d / 604800))));
150: }
151:
152: public static final CostRate newDollarsPerMillisecond(double v) {
153: return new CostRate(v * (1.0d / (1.0d / 1000)));
154: }
155:
156: public static final CostRate newDollarsPerMillisecond(String s) {
157: return new CostRate((Double.valueOf(s).doubleValue())
158: * (1.0d / (1.0d / 1000)));
159: }
160:
161: public static final CostRate newDollarsPerKilosecond(double v) {
162: return new CostRate(v * (1.0d / (1.0d / (1.0d / 1000))));
163: }
164:
165: public static final CostRate newDollarsPerKilosecond(String s) {
166: return new CostRate((Double.valueOf(s).doubleValue())
167: * (1.0d / (1.0d / (1.0d / 1000))));
168: }
169:
170: public static final CostRate newDollarsPerMonth(double v) {
171: return new CostRate(v * (1.0d / (1.0d / (1.0d / 2629743.8))));
172: }
173:
174: public static final CostRate newDollarsPerMonth(String s) {
175: return new CostRate((Double.valueOf(s).doubleValue())
176: * (1.0d / (1.0d / (1.0d / 2629743.8))));
177: }
178:
179: public static final CostRate newDollarsPerYear(double v) {
180: return new CostRate(v * (1.0d / (1.0d / (1.0d / 31556926))));
181: }
182:
183: public static final CostRate newDollarsPerYear(String s) {
184: return new CostRate((Double.valueOf(s).doubleValue())
185: * (1.0d / (1.0d / (1.0d / 31556926))));
186: }
187:
188: public static final CostRate newDollarsPerFortnight(double v) {
189: return new CostRate(v * (1.0d / (1.0d / (1.0d / 1209600))));
190: }
191:
192: public static final CostRate newDollarsPerFortnight(String s) {
193: return new CostRate((Double.valueOf(s).doubleValue())
194: * (1.0d / (1.0d / (1.0d / 1209600))));
195: }
196:
197: public int getCommonUnit() {
198: return 0;
199: }
200:
201: public int getMaxUnit() {
202: return MAXUNIT;
203: }
204:
205: // unit names for getUnitName
206: private static final String unitNames[] = { "dollars/second",
207: "dollars/minute", "dollars/hour", "dollars/day",
208: "dollars/week", "dollars/millisecond",
209: "dollars/kilosecond", "dollars/month", "dollars/year",
210: "dollars/fortnight", };
211:
212: /** @param unit One of the constant units of CostRate **/
213: public final String getUnitName(int unit) {
214: return unitNames[unit];
215: }
216:
217: // Index Typed factory methods
218: static final double convFactor[] = { (1.0d / 1.0d),
219: (1.0d / (1.0d / 60)), (1.0d / (1.0d / 3600)),
220: (1.0d / (1.0d / 86400)), (1.0d / (1.0d / 604800)),
221: (1.0d / 1000), (1.0d / (1.0d / 1000)),
222: (1.0d / (1.0d / 2629743.8)), (1.0d / (1.0d / 31556926)),
223: (1.0d / (1.0d / 1209600)), };
224:
225: public static final double getConvFactor(int i) {
226: return convFactor[i];
227: }
228:
229: // indexes into factor array
230: public static final int DOLLARS_PER_SECOND = 0;
231: public static final int DOLLARS_PER_MINUTE = 1;
232: public static final int DOLLARS_PER_HOUR = 2;
233: public static final int DOLLARS_PER_DAY = 3;
234: public static final int DOLLARS_PER_WEEK = 4;
235: public static final int DOLLARS_PER_MILLISECOND = 5;
236: public static final int DOLLARS_PER_KILOSECOND = 6;
237: public static final int DOLLARS_PER_MONTH = 7;
238: public static final int DOLLARS_PER_YEAR = 8;
239: public static final int DOLLARS_PER_FORTNIGHT = 9;
240: static final int MAXUNIT = 9;
241:
242: // Index Typed factory methods
243: /** @param unit One of the constant units of CostRate **/
244: public static final CostRate newCostRate(double v, int unit) {
245: if (unit >= 0 && unit <= MAXUNIT)
246: return new CostRate(v * getConvFactor(unit));
247: else
248: throw new UnknownUnitException();
249: }
250:
251: /** @param unit One of the constant units of CostRate **/
252: public static final CostRate newCostRate(String s, int unit) {
253: if (unit >= 0 && unit <= MAXUNIT)
254: return new CostRate((Double.valueOf(s).doubleValue())
255: * getConvFactor(unit));
256: else
257: throw new UnknownUnitException();
258: }
259:
260: // Index Typed factory methods
261: /** @param unit1 One of the constant units of Cost
262: * @param unit2 One of the constant units of Duration
263: **/
264: public static final CostRate newCostRate(double v, int unit1,
265: int unit2) {
266: if (unit1 >= 0 && unit1 <= Cost.MAXUNIT && unit2 >= 0
267: && unit2 <= Duration.MAXUNIT)
268: return new CostRate(v * Cost.getConvFactor(unit1)
269: / Duration.getConvFactor(unit2));
270: else
271: throw new UnknownUnitException();
272: }
273:
274: /** @param num An instance of Cost to use as numerator
275: * @param den An instance of Durationto use as denominator
276: **/
277: public static final CostRate newCostRate(Cost num, Duration den) {
278: return new CostRate(num.getValue(0) / den.getValue(0));
279: }
280:
281: /** @param unit1 One of the constant units of Cost
282: * @param unit2 One of the constant units of Duration
283: **/
284: public static final CostRate newCostRate(String s, int unit1,
285: int unit2) {
286: if (unit1 >= 0 && unit1 <= Cost.MAXUNIT && unit2 >= 0
287: && unit2 <= Duration.MAXUNIT)
288: return new CostRate((Double.valueOf(s).doubleValue())
289: * Cost.getConvFactor(unit1)
290: / Duration.getConvFactor(unit2));
291: else
292: throw new UnknownUnitException();
293: }
294:
295: // Support for AbstractMeasure-level constructor
296: public static final AbstractMeasure newMeasure(String s, int unit) {
297: return newCostRate(s, unit);
298: }
299:
300: public static final AbstractMeasure newMeasure(double v, int unit) {
301: return newCostRate(v, unit);
302: }
303:
304: // simple math : addition and subtraction
305: public final Measure add(Measure toAdd) {
306: if (!(toAdd instanceof CostRate))
307: throw new IllegalArgumentException();
308: return new CostRate(theValue + toAdd.getNativeValue());
309: }
310:
311: public final Measure subtract(Measure toSubtract) {
312: if (!(toSubtract instanceof CostRate))
313: throw new IllegalArgumentException();
314: return new CostRate(theValue - toSubtract.getNativeValue());
315: }
316:
317: public final Measure scale(double scale) {
318: return new CostRate(theValue * scale, 0);
319: }
320:
321: public final Measure negate() {
322: return newCostRate(-1 * theValue, 0);
323: }
324:
325: public final Measure floor(int unit) {
326: return newCostRate(Math.floor(getValue(unit)), 0);
327: }
328:
329: public final Measure valueOf(double value) {
330: return new CostRate(value);
331: }
332:
333: public final Measure valueOf(double value, int unit) {
334: return new CostRate(value, unit);
335: }
336:
337: public final double getNativeValue() {
338: return theValue;
339: }
340:
341: public final int getNativeUnit() {
342: return 0;
343: }
344:
345: public final Duration divide(Rate toRate) {
346: throw new IllegalArgumentException(
347: "Call divideRate instead to divide one Rate by another.");
348: }
349:
350: public final double divideRate(Rate toRate) {
351: if (toRate.getCanonicalNumerator().getClass() != getCanonicalNumerator()
352: .getClass()
353: || toRate.getCanonicalDenominator().getClass() != getCanonicalDenominator()
354: .getClass()) {
355: throw new IllegalArgumentException("Expecting a CostRate"
356: + ", got a "
357: + toRate.getCanonicalNumerator().getClass() + "/"
358: + toRate.getCanonicalDenominator().getClass());
359: }
360: return theValue / toRate.getNativeValue();
361: }
362:
363: // Unit-based Reader methods
364: public double getDollarsPerSecond() {
365: return (theValue * (1.0d / 1.0d));
366: }
367:
368: public double getDollarsPerMinute() {
369: return (theValue * (1.0d / (1.0d / 60)));
370: }
371:
372: public double getDollarsPerHour() {
373: return (theValue * (1.0d / (1.0d / 3600)));
374: }
375:
376: public double getDollarsPerDay() {
377: return (theValue * (1.0d / (1.0d / 86400)));
378: }
379:
380: public double getDollarsPerWeek() {
381: return (theValue * (1.0d / (1.0d / 604800)));
382: }
383:
384: public double getDollarsPerMillisecond() {
385: return (theValue * (1.0d / 1000));
386: }
387:
388: public double getDollarsPerKilosecond() {
389: return (theValue * (1.0d / (1.0d / 1000)));
390: }
391:
392: public double getDollarsPerMonth() {
393: return (theValue * (1.0d / (1.0d / 2629743.8)));
394: }
395:
396: public double getDollarsPerYear() {
397: return (theValue * (1.0d / (1.0d / 31556926)));
398: }
399:
400: public double getDollarsPerFortnight() {
401: return (theValue * (1.0d / (1.0d / 1209600)));
402: }
403:
404: /** @param unit One of the constant units of CostRate **/
405: public double getValue(int unit) {
406: if (unit >= 0 && unit <= MAXUNIT)
407: return (theValue * getConvFactor(unit));
408: else
409: throw new UnknownUnitException();
410: }
411:
412: /** @param unit1 One of the constant units of Cost
413: * @param unit2 One of the constant units of Duration
414: **/
415: public double getValue(int unit1, int unit2) {
416: if (unit1 >= 0 && unit1 <= Cost.MAXUNIT && unit2 >= 0
417: && unit2 <= Duration.MAXUNIT)
418: return (theValue * Duration.getConvFactor(unit2) / Cost
419: .getConvFactor(unit1));
420: else
421: throw new UnknownUnitException();
422: }
423:
424: public boolean equals(Object o) {
425: return (o instanceof CostRate && theValue == ((CostRate) o).theValue);
426: }
427:
428: public String toString() {
429: return Double.toString(theValue) + "usd/s";
430: }
431:
432: public int hashCode() {
433: return (new Double(theValue)).hashCode();
434: }
435:
436: // Derivative
437: public final Class getNumeratorClass() {
438: return Cost.class;
439: }
440:
441: public final Class getDenominatorClass() {
442: return Duration.class;
443: }
444:
445: private final static Cost can_num = new Cost(0.0, 0);
446:
447: public final Measure getCanonicalNumerator() {
448: return can_num;
449: }
450:
451: private final static Duration can_den = new Duration(0.0, 0);
452:
453: public final Measure getCanonicalDenominator() {
454: return can_den;
455: }
456:
457: public final Measure computeNumerator(Measure den) {
458: if (!(den instanceof Duration))
459: throw new IllegalArgumentException();
460: return new Cost(theValue * den.getValue(0), 0);
461: }
462:
463: public final Measure computeDenominator(Measure num) {
464: if (!(num instanceof Cost))
465: throw new IllegalArgumentException();
466: return new Duration(num.getValue(0) / theValue, 0);
467: }
468:
469: // serialization
470: public void writeExternal(ObjectOutput out) throws IOException {
471: out.writeDouble(theValue);
472: }
473:
474: public void readExternal(ObjectInput in) throws IOException {
475: theValue = in.readDouble();
476: }
477: }
|