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 Distance.
029: **/package org.cougaar.planning.ldm.measure;
030:
031: import java.io.*;
032:
033: public final class Distance extends Scalar implements Externalizable {
034: // Conversion factor constants
035: public static final double METERS_PER_MILES = 1609.344;
036: public static final double MILES_PER_METERS = (1.0d / 1609.344);
037: public static final double METERS_PER_NAUTICAL_MILES = 1852.0;
038: public static final double NAUTICAL_MILES_PER_METERS = (1.0d / 1852.0);
039: public static final double METERS_PER_YARDS = 0.9414;
040: public static final double YARDS_PER_METERS = (1.0d / 0.9414);
041: public static final double METERS_PER_FEET = 0.3048;
042: public static final double FEET_PER_METERS = (1.0d / 0.3048);
043: public static final double METERS_PER_INCHES = 0.0254;
044: public static final double INCHES_PER_METERS = (1.0d / 0.0254);
045: public static final double METERS_PER_KILOMETERS = 1000.0;
046: public static final double KILOMETERS_PER_METERS = (1.0d / 1000.0);
047: public static final double METERS_PER_CENTIMETERS = (1.0d / 100);
048: public static final double CENTIMETERS_PER_METERS = 100;
049: public static final double METERS_PER_MILLIMETERS = (1.0d / 1000);
050: public static final double MILLIMETERS_PER_METERS = 1000;
051: public static final double METERS_PER_FURLONGS = 201.168;
052: public static final double FURLONGS_PER_METERS = (1.0d / 201.168);
053:
054: // the value is stored as meters
055: private double theValue;
056:
057: /** No-arg constructor is only for use by serialization **/
058: public Distance() {
059: }
060:
061: // private constructor
062: private Distance(double v) {
063: theValue = v;
064: }
065:
066: /** parameterized constructor **/
067: public Distance(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 Distance(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("meters"))
083: theValue = n;
084: else if (u.equals("miles"))
085: theValue = n * METERS_PER_MILES;
086: else if (u.equals("nauticalmiles"))
087: theValue = n * METERS_PER_NAUTICAL_MILES;
088: else if (u.equals("yards"))
089: theValue = n * METERS_PER_YARDS;
090: else if (u.equals("feet"))
091: theValue = n * METERS_PER_FEET;
092: else if (u.equals("inches"))
093: theValue = n * METERS_PER_INCHES;
094: else if (u.equals("kilometers"))
095: theValue = n * METERS_PER_KILOMETERS;
096: else if (u.equals("centimeters"))
097: theValue = n * METERS_PER_CENTIMETERS;
098: else if (u.equals("millimeters"))
099: theValue = n * METERS_PER_MILLIMETERS;
100: else if (u.equals("furlongs"))
101: theValue = n * METERS_PER_FURLONGS;
102: else
103: throw new UnknownUnitException();
104: }
105:
106: // TypeNamed factory methods
107: public static final Distance newMeters(double v) {
108: return new Distance(v);
109: }
110:
111: public static final Distance newMeters(String s) {
112: return new Distance((Double.valueOf(s).doubleValue()));
113: }
114:
115: public static final Distance newMiles(double v) {
116: return new Distance(v * METERS_PER_MILES);
117: }
118:
119: public static final Distance newMiles(String s) {
120: return new Distance((Double.valueOf(s).doubleValue())
121: * METERS_PER_MILES);
122: }
123:
124: public static final Distance newNauticalMiles(double v) {
125: return new Distance(v * METERS_PER_NAUTICAL_MILES);
126: }
127:
128: public static final Distance newNauticalMiles(String s) {
129: return new Distance((Double.valueOf(s).doubleValue())
130: * METERS_PER_NAUTICAL_MILES);
131: }
132:
133: public static final Distance newYards(double v) {
134: return new Distance(v * METERS_PER_YARDS);
135: }
136:
137: public static final Distance newYards(String s) {
138: return new Distance((Double.valueOf(s).doubleValue())
139: * METERS_PER_YARDS);
140: }
141:
142: public static final Distance newFeet(double v) {
143: return new Distance(v * METERS_PER_FEET);
144: }
145:
146: public static final Distance newFeet(String s) {
147: return new Distance((Double.valueOf(s).doubleValue())
148: * METERS_PER_FEET);
149: }
150:
151: public static final Distance newInches(double v) {
152: return new Distance(v * METERS_PER_INCHES);
153: }
154:
155: public static final Distance newInches(String s) {
156: return new Distance((Double.valueOf(s).doubleValue())
157: * METERS_PER_INCHES);
158: }
159:
160: public static final Distance newKilometers(double v) {
161: return new Distance(v * METERS_PER_KILOMETERS);
162: }
163:
164: public static final Distance newKilometers(String s) {
165: return new Distance((Double.valueOf(s).doubleValue())
166: * METERS_PER_KILOMETERS);
167: }
168:
169: public static final Distance newCentimeters(double v) {
170: return new Distance(v * METERS_PER_CENTIMETERS);
171: }
172:
173: public static final Distance newCentimeters(String s) {
174: return new Distance((Double.valueOf(s).doubleValue())
175: * METERS_PER_CENTIMETERS);
176: }
177:
178: public static final Distance newMillimeters(double v) {
179: return new Distance(v * METERS_PER_MILLIMETERS);
180: }
181:
182: public static final Distance newMillimeters(String s) {
183: return new Distance((Double.valueOf(s).doubleValue())
184: * METERS_PER_MILLIMETERS);
185: }
186:
187: public static final Distance newFurlongs(double v) {
188: return new Distance(v * METERS_PER_FURLONGS);
189: }
190:
191: public static final Distance newFurlongs(String s) {
192: return new Distance((Double.valueOf(s).doubleValue())
193: * METERS_PER_FURLONGS);
194: }
195:
196: public int getCommonUnit() {
197: return METERS;
198: }
199:
200: public int getMaxUnit() {
201: return MAXUNIT;
202: }
203:
204: // unit names for getUnitName
205: private static final String unitNames[] = { "meters", "miles",
206: "nautical_miles", "yards", "feet", "inches", "kilometers",
207: "centimeters", "millimeters", "furlongs", };
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, METERS_PER_MILES,
215: METERS_PER_NAUTICAL_MILES, METERS_PER_YARDS,
216: METERS_PER_FEET, METERS_PER_INCHES, METERS_PER_KILOMETERS,
217: METERS_PER_CENTIMETERS, METERS_PER_MILLIMETERS,
218: METERS_PER_FURLONGS, };
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 METERS = 0;
226: public static final int MILES = 1;
227: public static final int NAUTICAL_MILES = 2;
228: public static final int YARDS = 3;
229: public static final int FEET = 4;
230: public static final int INCHES = 5;
231: public static final int KILOMETERS = 6;
232: public static final int CENTIMETERS = 7;
233: public static final int MILLIMETERS = 8;
234: public static final int FURLONGS = 9;
235: public static final int MAXUNIT = 9;
236:
237: // Index Typed factory methods
238: public static final Distance newDistance(double v, int unit) {
239: if (unit >= 0 && unit <= MAXUNIT)
240: return new Distance(v * getConvFactor(unit));
241: else
242: throw new UnknownUnitException();
243: }
244:
245: public static final Distance newDistance(String s, int unit) {
246: if (unit >= 0 && unit <= MAXUNIT)
247: return new Distance((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 newDistance(s, unit);
256: }
257:
258: public static final AbstractMeasure newMeasure(double v, int unit) {
259: return newDistance(v, unit);
260: }
261:
262: // simple math : addition and subtraction
263: public final Measure add(Measure toAdd) {
264: if (!(toAdd instanceof Distance))
265: throw new IllegalArgumentException();
266: return new Distance(theValue + toAdd.getNativeValue());
267: }
268:
269: public final Measure subtract(Measure toSubtract) {
270: if (!(toSubtract instanceof Distance))
271: throw new IllegalArgumentException();
272: return new Distance(theValue - toSubtract.getNativeValue());
273: }
274:
275: public final Measure scale(double scale) {
276: return new Distance(theValue * scale, 0);
277: }
278:
279: public final Measure negate() {
280: return newDistance(-1 * theValue, 0);
281: }
282:
283: public final Measure floor(int unit) {
284: return newDistance(Math.floor(getValue(unit)), 0);
285: }
286:
287: public final Measure valueOf(double value) {
288: return new Distance(value);
289: }
290:
291: public final Measure valueOf(double value, int unit) {
292: return new Distance(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 Distance)) {
306: throw new IllegalArgumentException(
307: "Expecting a Distance/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 getMeters() {
318: return (theValue);
319: }
320:
321: public double getMiles() {
322: return (theValue / METERS_PER_MILES);
323: }
324:
325: public double getNauticalMiles() {
326: return (theValue / METERS_PER_NAUTICAL_MILES);
327: }
328:
329: public double getYards() {
330: return (theValue / METERS_PER_YARDS);
331: }
332:
333: public double getFeet() {
334: return (theValue / METERS_PER_FEET);
335: }
336:
337: public double getInches() {
338: return (theValue / METERS_PER_INCHES);
339: }
340:
341: public double getKilometers() {
342: return (theValue / METERS_PER_KILOMETERS);
343: }
344:
345: public double getCentimeters() {
346: return (theValue / METERS_PER_CENTIMETERS);
347: }
348:
349: public double getMillimeters() {
350: return (theValue / METERS_PER_MILLIMETERS);
351: }
352:
353: public double getFurlongs() {
354: return (theValue / METERS_PER_FURLONGS);
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 Distance && theValue == ((Distance) o).theValue);
366: }
367:
368: public String toString() {
369: return Double.toString(theValue) + "m";
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: }
|