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