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