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