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