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 Volume.
029: **/package org.cougaar.planning.ldm.measure;
030:
031: import java.io.*;
032:
033: public final class Volume extends Scalar implements Externalizable {
034: // Conversion factor constants
035: public static final double LITERS_PER_OUNCES = (1.0d / 33.814023);
036: public static final double OUNCES_PER_LITERS = 33.814023;
037: public static final double LITERS_PER_GALLONS = 3.785412;
038: public static final double GALLONS_PER_LITERS = (1.0d / 3.785412);
039: public static final double LITERS_PER_IMPERIAL_GALLONS = 4.546090;
040: public static final double IMPERIAL_GALLONS_PER_LITERS = (1.0d / 4.546090);
041: public static final double LITERS_PER_CUBIC_FEET = 28.316847;
042: public static final double CUBIC_FEET_PER_LITERS = (1.0d / 28.316847);
043: public static final double LITERS_PER_CUBIC_YARDS = 764.55486;
044: public static final double CUBIC_YARDS_PER_LITERS = (1.0d / 764.55486);
045: public static final double LITERS_PER_MTONS = 1132.67388;
046: public static final double MTONS_PER_LITERS = (1.0d / 1132.67388);
047: public static final double LITERS_PER_CUBIC_CENTIMETERS = (1.0d / 1000);
048: public static final double CUBIC_CENTIMETERS_PER_LITERS = 1000;
049: public static final double LITERS_PER_CUBIC_METERS = 1000;
050: public static final double CUBIC_METERS_PER_LITERS = (1.0d / 1000);
051: public static final double LITERS_PER_BARRELS = 158.98729;
052: public static final double BARRELS_PER_LITERS = (1.0d / 158.98729);
053:
054: // the value is stored as liters
055: private double theValue;
056:
057: /** No-arg constructor is only for use by serialization **/
058: public Volume() {
059: }
060:
061: // private constructor
062: private Volume(double v) {
063: theValue = v;
064: }
065:
066: /** parameterized constructor **/
067: public Volume(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 Volume(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("liters"))
083: theValue = n;
084: else if (u.equals("ounces"))
085: theValue = n * LITERS_PER_OUNCES;
086: else if (u.equals("gallons"))
087: theValue = n * LITERS_PER_GALLONS;
088: else if (u.equals("imperialgallons"))
089: theValue = n * LITERS_PER_IMPERIAL_GALLONS;
090: else if (u.equals("cubicfeet"))
091: theValue = n * LITERS_PER_CUBIC_FEET;
092: else if (u.equals("cubicyards"))
093: theValue = n * LITERS_PER_CUBIC_YARDS;
094: else if (u.equals("mtons"))
095: theValue = n * LITERS_PER_MTONS;
096: else if (u.equals("cubiccentimeters"))
097: theValue = n * LITERS_PER_CUBIC_CENTIMETERS;
098: else if (u.equals("cubicmeters"))
099: theValue = n * LITERS_PER_CUBIC_METERS;
100: else if (u.equals("barrels"))
101: theValue = n * LITERS_PER_BARRELS;
102: else
103: throw new UnknownUnitException();
104: }
105:
106: // TypeNamed factory methods
107: public static final Volume newLiters(double v) {
108: return new Volume(v);
109: }
110:
111: public static final Volume newLiters(String s) {
112: return new Volume((Double.valueOf(s).doubleValue()));
113: }
114:
115: public static final Volume newOunces(double v) {
116: return new Volume(v * LITERS_PER_OUNCES);
117: }
118:
119: public static final Volume newOunces(String s) {
120: return new Volume((Double.valueOf(s).doubleValue())
121: * LITERS_PER_OUNCES);
122: }
123:
124: public static final Volume newGallons(double v) {
125: return new Volume(v * LITERS_PER_GALLONS);
126: }
127:
128: public static final Volume newGallons(String s) {
129: return new Volume((Double.valueOf(s).doubleValue())
130: * LITERS_PER_GALLONS);
131: }
132:
133: public static final Volume newImperialGallons(double v) {
134: return new Volume(v * LITERS_PER_IMPERIAL_GALLONS);
135: }
136:
137: public static final Volume newImperialGallons(String s) {
138: return new Volume((Double.valueOf(s).doubleValue())
139: * LITERS_PER_IMPERIAL_GALLONS);
140: }
141:
142: public static final Volume newCubicFeet(double v) {
143: return new Volume(v * LITERS_PER_CUBIC_FEET);
144: }
145:
146: public static final Volume newCubicFeet(String s) {
147: return new Volume((Double.valueOf(s).doubleValue())
148: * LITERS_PER_CUBIC_FEET);
149: }
150:
151: public static final Volume newCubicYards(double v) {
152: return new Volume(v * LITERS_PER_CUBIC_YARDS);
153: }
154:
155: public static final Volume newCubicYards(String s) {
156: return new Volume((Double.valueOf(s).doubleValue())
157: * LITERS_PER_CUBIC_YARDS);
158: }
159:
160: public static final Volume newMtons(double v) {
161: return new Volume(v * LITERS_PER_MTONS);
162: }
163:
164: public static final Volume newMtons(String s) {
165: return new Volume((Double.valueOf(s).doubleValue())
166: * LITERS_PER_MTONS);
167: }
168:
169: public static final Volume newCubicCentimeters(double v) {
170: return new Volume(v * LITERS_PER_CUBIC_CENTIMETERS);
171: }
172:
173: public static final Volume newCubicCentimeters(String s) {
174: return new Volume((Double.valueOf(s).doubleValue())
175: * LITERS_PER_CUBIC_CENTIMETERS);
176: }
177:
178: public static final Volume newCubicMeters(double v) {
179: return new Volume(v * LITERS_PER_CUBIC_METERS);
180: }
181:
182: public static final Volume newCubicMeters(String s) {
183: return new Volume((Double.valueOf(s).doubleValue())
184: * LITERS_PER_CUBIC_METERS);
185: }
186:
187: public static final Volume newBarrels(double v) {
188: return new Volume(v * LITERS_PER_BARRELS);
189: }
190:
191: public static final Volume newBarrels(String s) {
192: return new Volume((Double.valueOf(s).doubleValue())
193: * LITERS_PER_BARRELS);
194: }
195:
196: public int getCommonUnit() {
197: return GALLONS;
198: }
199:
200: public int getMaxUnit() {
201: return MAXUNIT;
202: }
203:
204: // unit names for getUnitName
205: private static final String unitNames[] = { "liters", "ounces",
206: "gallons", "imperial_gallons", "cubic_feet", "cubic_yards",
207: "mtons", "cubic_centimeters", "cubic_meters", "barrels", };
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, LITERS_PER_OUNCES,
215: LITERS_PER_GALLONS, LITERS_PER_IMPERIAL_GALLONS,
216: LITERS_PER_CUBIC_FEET, LITERS_PER_CUBIC_YARDS,
217: LITERS_PER_MTONS, LITERS_PER_CUBIC_CENTIMETERS,
218: LITERS_PER_CUBIC_METERS, LITERS_PER_BARRELS, };
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 LITERS = 0;
226: public static final int OUNCES = 1;
227: public static final int GALLONS = 2;
228: public static final int IMPERIAL_GALLONS = 3;
229: public static final int CUBIC_FEET = 4;
230: public static final int CUBIC_YARDS = 5;
231: public static final int MTONS = 6;
232: public static final int CUBIC_CENTIMETERS = 7;
233: public static final int CUBIC_METERS = 8;
234: public static final int BARRELS = 9;
235: public static final int MAXUNIT = 9;
236:
237: // Index Typed factory methods
238: public static final Volume newVolume(double v, int unit) {
239: if (unit >= 0 && unit <= MAXUNIT)
240: return new Volume(v * getConvFactor(unit));
241: else
242: throw new UnknownUnitException();
243: }
244:
245: public static final Volume newVolume(String s, int unit) {
246: if (unit >= 0 && unit <= MAXUNIT)
247: return new Volume((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 newVolume(s, unit);
256: }
257:
258: public static final AbstractMeasure newMeasure(double v, int unit) {
259: return newVolume(v, unit);
260: }
261:
262: // simple math : addition and subtraction
263: public final Measure add(Measure toAdd) {
264: if (!(toAdd instanceof Volume))
265: throw new IllegalArgumentException();
266: return new Volume(theValue + toAdd.getNativeValue());
267: }
268:
269: public final Measure subtract(Measure toSubtract) {
270: if (!(toSubtract instanceof Volume))
271: throw new IllegalArgumentException();
272: return new Volume(theValue - toSubtract.getNativeValue());
273: }
274:
275: public final Measure scale(double scale) {
276: return new Volume(theValue * scale, 0);
277: }
278:
279: public final Measure negate() {
280: return newVolume(-1 * theValue, 0);
281: }
282:
283: public final Measure floor(int unit) {
284: return newVolume(Math.floor(getValue(unit)), 0);
285: }
286:
287: public final Measure valueOf(double value) {
288: return new Volume(value);
289: }
290:
291: public final Measure valueOf(double value, int unit) {
292: return new Volume(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 Volume)) {
306: throw new IllegalArgumentException(
307: "Expecting a Volume/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 getLiters() {
318: return (theValue);
319: }
320:
321: public double getOunces() {
322: return (theValue / LITERS_PER_OUNCES);
323: }
324:
325: public double getGallons() {
326: return (theValue / LITERS_PER_GALLONS);
327: }
328:
329: public double getImperialGallons() {
330: return (theValue / LITERS_PER_IMPERIAL_GALLONS);
331: }
332:
333: public double getCubicFeet() {
334: return (theValue / LITERS_PER_CUBIC_FEET);
335: }
336:
337: public double getCubicYards() {
338: return (theValue / LITERS_PER_CUBIC_YARDS);
339: }
340:
341: public double getMtons() {
342: return (theValue / LITERS_PER_MTONS);
343: }
344:
345: public double getCubicCentimeters() {
346: return (theValue / LITERS_PER_CUBIC_CENTIMETERS);
347: }
348:
349: public double getCubicMeters() {
350: return (theValue / LITERS_PER_CUBIC_METERS);
351: }
352:
353: public double getBarrels() {
354: return (theValue / LITERS_PER_BARRELS);
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 Volume && theValue == ((Volume) o).theValue);
366: }
367:
368: public String toString() {
369: return Double.toString(theValue) + "l";
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: }
|