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: /** Immutable implementation of Temperature.
027: **/package org.cougaar.planning.ldm.measure;
028:
029: public final class Temperature extends AbstractMeasure {
030: private static final Conversion CELSIUS_TO_CELSIUS = new Conversion() {
031: public double convert(double from) {
032: return from;
033: }
034: };
035:
036: private static final Conversion CELSIUS_TO_FAHRENHEIT = new Conversion() {
037: public double convert(double from) {
038: return (from * 1.8) + 32.0;
039: }
040: };
041: private static final Conversion FAHRENHEIT_TO_CELSIUS = new Conversion() {
042: public double convert(double from) {
043: return (from - 32.0) / 1.8;
044: }
045: };
046:
047: // basic unit is Celsius
048: private double theValue;
049:
050: // private constructor
051: private Temperature(double v) {
052: theValue = v;
053: }
054:
055: public Temperature(String s) {
056: int i = indexOfType(s);
057: if (i < 0)
058: throw new UnknownUnitException();
059: double n = Double.valueOf(s.substring(0, i).trim())
060: .doubleValue();
061: String u = s.substring(i).trim().toLowerCase();
062: if (u.equals("celsius"))
063: theValue = n;
064: else if (u.equals("fahrenheit"))
065: theValue = FAHRENHEIT_TO_CELSIUS.convert(n);
066: else
067: throw new UnknownUnitException();
068: }
069:
070: public int getCommonUnit() {
071: return FAHRENHEIT;
072: }
073:
074: public int getMaxUnit() {
075: return 1;
076: }
077:
078: public String getUnitName(int i) {
079: if (i == 0)
080: return "celcius";
081: else if (i == 1)
082: return "fahrenheit";
083: else
084: throw new IllegalArgumentException();
085: }
086:
087: // TypeNamed factory methods
088: public static Temperature newCelsius(double v) {
089: return new Temperature(v);
090: }
091:
092: public static Temperature newCelsius(String s) {
093: return new Temperature((Double.valueOf(s).doubleValue()));
094: }
095:
096: public static Temperature newFahrenheit(double v) {
097: return new Temperature(FAHRENHEIT_TO_CELSIUS.convert(v));
098: }
099:
100: public static Temperature newFahrenheit(String s) {
101: return new Temperature(FAHRENHEIT_TO_CELSIUS.convert(Double
102: .valueOf(s).doubleValue()));
103: }
104:
105: // Index Typed factory methods
106: private static final Conversion convFactor[] = {
107: // conversions to base units
108: CELSIUS_TO_CELSIUS, FAHRENHEIT_TO_CELSIUS,
109: // conversions from base units
110: CELSIUS_TO_CELSIUS, CELSIUS_TO_FAHRENHEIT };
111: // indexes into factor array
112: public static int CELSIUS = 0;
113: public static int FAHRENHEIT = 1;
114: private static int MAXUNIT = 1;
115:
116: // Index Typed factory methods
117: public static Temperature newTemperature(double v, int unit) {
118: if (unit >= 0 && unit <= MAXUNIT)
119: return new Temperature(convFactor[unit].convert(v));
120: else
121: throw new UnknownUnitException();
122: }
123:
124: public static Temperature newTemperature(String s, int unit) {
125: if (unit >= 0 && unit <= MAXUNIT)
126: return new Temperature(convFactor[unit].convert(Double
127: .valueOf(s).doubleValue()));
128: else
129: throw new UnknownUnitException();
130: }
131:
132: // Support for AbstractMeasure-level constructor
133: public static AbstractMeasure newMeasure(String s, int unit) {
134: return newTemperature(s, unit);
135: }
136:
137: public static AbstractMeasure newMeasure(double v, int unit) {
138: return newTemperature(v, unit);
139: }
140:
141: // Unit-based Reader methods
142: public double getCelsius() {
143: return (theValue);
144: }
145:
146: public double getFahrenheit() {
147: return (CELSIUS_TO_FAHRENHEIT.convert(theValue));
148: }
149:
150: public double getValue(int unit) {
151: if (unit >= 0 && unit <= MAXUNIT)
152: return convFactor[MAXUNIT + 1 + unit].convert(theValue);
153: else
154: throw new UnknownUnitException();
155: }
156:
157: public static Conversion getConversion(final int from, final int to) {
158: if (from >= 0 && from <= MAXUNIT && to >= 0 && to <= MAXUNIT) {
159: return new Conversion() {
160: public double convert(double value) {
161: return convFactor[MAXUNIT + 1 + to]
162: .convert(convFactor[from].convert(value));
163: }
164: };
165: } else
166: throw new UnknownUnitException();
167: }
168:
169: public boolean equals(Object o) {
170: return (o instanceof Temperature && theValue == ((Temperature) o)
171: .getCelsius());
172: }
173:
174: public String toString() {
175: return Double.toString(theValue) + "c";
176: }
177:
178: public int hashCode() {
179: return (new Double(theValue)).hashCode();
180: }
181:
182: /**
183: * TODO : fill in
184: * @param other
185: * @return
186: */
187: public Measure add(Measure other) {
188: return null;
189: }
190:
191: /**
192: * TODO : fill in
193: * @param other
194: * @return
195: */
196: public Measure subtract(Measure other) {
197: return null;
198: }
199:
200: public Measure negate() {
201: return null;
202: }
203:
204: public Measure scale(double scale) {
205: return null;
206: }
207:
208: public Measure floor(int unit) {
209: return null;
210: }
211:
212: public Measure valueOf(double value) {
213: return null;
214: }
215:
216: public Measure valueOf(double value, int unit) {
217: return null;
218: }
219:
220: public int getNativeUnit() {
221: return 0;
222: }
223:
224: public double getNativeValue() {
225: return getValue(getNativeUnit());
226: }
227:
228: public Duration divide(Rate rate) {
229: return null;
230: }
231: } // end Temperature
|