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 Cost.
029: **/package org.cougaar.planning.ldm.measure;
030:
031: import java.io.*;
032:
033: public final class Cost extends AbstractMeasure implements
034: Externalizable {
035: // Conversion factor constants
036:
037: // the value is stored as dollars
038: private double theValue;
039:
040: /** No-arg constructor is only for use by serialization **/
041: public Cost() {
042: }
043:
044: // private constructor
045: private Cost(double v) {
046: theValue = v;
047: }
048:
049: /** parameterized constructor **/
050: public Cost(double v, int unit) {
051: if (unit >= 0 && unit <= MAXUNIT)
052: theValue = v * getConvFactor(unit);
053: else
054: throw new UnknownUnitException();
055: }
056:
057: /** takes strings of the form "Number unit" **/
058: public Cost(String s) {
059: int i = indexOfType(s);
060: if (i < 0)
061: throw new UnknownUnitException();
062: double n = Double.valueOf(s.substring(0, i).trim())
063: .doubleValue();
064: String u = s.substring(i).trim().toLowerCase();
065: if (u.equals("dollars"))
066: theValue = n;
067: else
068: throw new UnknownUnitException();
069: }
070:
071: // TypeNamed factory methods
072: public static final Cost newDollars(double v) {
073: return new Cost(v);
074: }
075:
076: public static final Cost newDollars(String s) {
077: return new Cost((Double.valueOf(s).doubleValue()));
078: }
079:
080: public int getCommonUnit() {
081: return 0;
082: }
083:
084: public int getMaxUnit() {
085: return MAXUNIT;
086: }
087:
088: // unit names for getUnitName
089: private static final String unitNames[] = { "dollars", };
090:
091: public String getUnitName(int unit) {
092: return unitNames[unit];
093: }
094:
095: // Index Typed factory methods
096: static final double convFactor[] = { 1.0, };
097:
098: public static final double getConvFactor(int i) {
099: return convFactor[i];
100: }
101:
102: // indexes into factor array
103: public static final int DOLLARS = 0;
104: public static final int MAXUNIT = 0;
105:
106: // Index Typed factory methods
107: public static final Cost newCost(double v, int unit) {
108: if (unit >= 0 && unit <= MAXUNIT)
109: return new Cost(v * getConvFactor(unit));
110: else
111: throw new UnknownUnitException();
112: }
113:
114: public static final Cost newCost(String s, int unit) {
115: if (unit >= 0 && unit <= MAXUNIT)
116: return new Cost((Double.valueOf(s).doubleValue())
117: * getConvFactor(unit));
118: else
119: throw new UnknownUnitException();
120: }
121:
122: // Support for AbstractMeasure-level constructor
123: public static final AbstractMeasure newMeasure(String s, int unit) {
124: return newCost(s, unit);
125: }
126:
127: public static final AbstractMeasure newMeasure(double v, int unit) {
128: return newCost(v, unit);
129: }
130:
131: // simple math : addition and subtraction
132: public final Measure add(Measure toAdd) {
133: if (!(toAdd instanceof Cost))
134: throw new IllegalArgumentException();
135: return new Cost(theValue + toAdd.getNativeValue());
136: }
137:
138: public final Measure subtract(Measure toSubtract) {
139: if (!(toSubtract instanceof Cost))
140: throw new IllegalArgumentException();
141: return new Cost(theValue - toSubtract.getNativeValue());
142: }
143:
144: public final Measure scale(double scale) {
145: return new Cost(theValue * scale, 0);
146: }
147:
148: public final Measure negate() {
149: return newCost(-1 * theValue, 0);
150: }
151:
152: public final Measure floor(int unit) {
153: return newCost(Math.floor(getValue(unit)), 0);
154: }
155:
156: public final Measure valueOf(double value) {
157: return new Cost(value);
158: }
159:
160: public final Measure valueOf(double value, int unit) {
161: return new Cost(value, unit);
162: }
163:
164: public final double getNativeValue() {
165: return theValue;
166: }
167:
168: public final int getNativeUnit() {
169: return 0;
170: }
171:
172: public final Duration divide(Rate toRate) {
173: Measure canonicalNumerator = toRate.getCanonicalNumerator();
174: if (!(toRate.getCanonicalNumerator() instanceof Cost)) {
175: throw new IllegalArgumentException(
176: "Expecting a Cost/Duration");
177: }
178: int durationNativeUnit = toRate.getCanonicalDenominator()
179: .getNativeUnit(); // seconds
180: double value = toRate.getValue(canonicalNumerator
181: .getNativeUnit(), durationNativeUnit); // ?/seconds
182: return new Duration(theValue / value, durationNativeUnit); // ?/?/second = seconds
183: }
184:
185: // Unit-based Reader methods
186: public double getDollars() {
187: return (theValue);
188: }
189:
190: public double getValue(int unit) {
191: if (unit >= 0 && unit <= MAXUNIT)
192: return (theValue / getConvFactor(unit));
193: else
194: throw new UnknownUnitException();
195: }
196:
197: public boolean equals(Object o) {
198: return (o instanceof Cost && theValue == ((Cost) o).theValue);
199: }
200:
201: public String toString() {
202: return Double.toString(theValue) + "usd";
203: }
204:
205: public int hashCode() {
206: return (new Double(theValue)).hashCode();
207: }
208:
209: // serialization
210: public void writeExternal(ObjectOutput out) throws IOException {
211: out.writeDouble(theValue);
212: }
213:
214: public void readExternal(ObjectInput in) throws IOException {
215: theValue = in.readDouble();
216: }
217: }
|