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