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