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