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 Count.
029: **/package org.cougaar.planning.ldm.measure;
030:
031: import java.io.*;
032:
033: public final class Count extends Scalar implements Externalizable {
034: // Conversion factor constants
035: public static final double UNITS_PER_EACHES = (1.0d / 1.0);
036: public static final double EACHES_PER_UNITS = 1.0;
037: public static final double UNITS_PER_DOZEN = 12;
038: public static final double DOZEN_PER_UNITS = (1.0d / 12);
039: public static final double UNITS_PER_HUNDRED = 100;
040: public static final double HUNDRED_PER_UNITS = (1.0d / 100);
041: public static final double UNITS_PER_GROSS = 144;
042: public static final double GROSS_PER_UNITS = (1.0d / 144);
043: public static final double UNITS_PER_MILLION = 1000000;
044: public static final double MILLION_PER_UNITS = (1.0d / 1000000);
045:
046: // the value is stored as units
047: private double theValue;
048:
049: /** No-arg constructor is only for use by serialization **/
050: public Count() {
051: }
052:
053: // private constructor
054: private Count(double v) {
055: theValue = v;
056: }
057:
058: /** parameterized constructor **/
059: public Count(double v, int unit) {
060: if (unit >= 0 && unit <= MAXUNIT)
061: theValue = v * getConvFactor(unit);
062: else
063: throw new UnknownUnitException();
064: }
065:
066: /** takes strings of the form "Number unit" **/
067: public Count(String s) {
068: int i = indexOfType(s);
069: if (i < 0)
070: throw new UnknownUnitException();
071: double n = Double.valueOf(s.substring(0, i).trim())
072: .doubleValue();
073: String u = s.substring(i).trim().toLowerCase();
074: if (u.equals("units"))
075: theValue = n;
076: else if (u.equals("eaches"))
077: theValue = n * UNITS_PER_EACHES;
078: else if (u.equals("dozen"))
079: theValue = n * UNITS_PER_DOZEN;
080: else if (u.equals("hundred"))
081: theValue = n * UNITS_PER_HUNDRED;
082: else if (u.equals("gross"))
083: theValue = n * UNITS_PER_GROSS;
084: else if (u.equals("million"))
085: theValue = n * UNITS_PER_MILLION;
086: else
087: throw new UnknownUnitException();
088: }
089:
090: // TypeNamed factory methods
091: public static final Count newUnits(double v) {
092: return new Count(v);
093: }
094:
095: public static final Count newUnits(String s) {
096: return new Count((Double.valueOf(s).doubleValue()));
097: }
098:
099: public static final Count newEaches(double v) {
100: return new Count(v * UNITS_PER_EACHES);
101: }
102:
103: public static final Count newEaches(String s) {
104: return new Count((Double.valueOf(s).doubleValue())
105: * UNITS_PER_EACHES);
106: }
107:
108: public static final Count newDozen(double v) {
109: return new Count(v * UNITS_PER_DOZEN);
110: }
111:
112: public static final Count newDozen(String s) {
113: return new Count((Double.valueOf(s).doubleValue())
114: * UNITS_PER_DOZEN);
115: }
116:
117: public static final Count newHundred(double v) {
118: return new Count(v * UNITS_PER_HUNDRED);
119: }
120:
121: public static final Count newHundred(String s) {
122: return new Count((Double.valueOf(s).doubleValue())
123: * UNITS_PER_HUNDRED);
124: }
125:
126: public static final Count newGross(double v) {
127: return new Count(v * UNITS_PER_GROSS);
128: }
129:
130: public static final Count newGross(String s) {
131: return new Count((Double.valueOf(s).doubleValue())
132: * UNITS_PER_GROSS);
133: }
134:
135: public static final Count newMillion(double v) {
136: return new Count(v * UNITS_PER_MILLION);
137: }
138:
139: public static final Count newMillion(String s) {
140: return new Count((Double.valueOf(s).doubleValue())
141: * UNITS_PER_MILLION);
142: }
143:
144: public int getCommonUnit() {
145: return UNITS;
146: }
147:
148: public int getMaxUnit() {
149: return MAXUNIT;
150: }
151:
152: // unit names for getUnitName
153: private static final String unitNames[] = { "units", "eaches",
154: "dozen", "hundred", "gross", "million", };
155:
156: public String getUnitName(int unit) {
157: return unitNames[unit];
158: }
159:
160: // Index Typed factory methods
161: static final double convFactor[] = { 1.0, UNITS_PER_EACHES,
162: UNITS_PER_DOZEN, UNITS_PER_HUNDRED, UNITS_PER_GROSS,
163: UNITS_PER_MILLION, };
164:
165: public static final double getConvFactor(int i) {
166: return convFactor[i];
167: }
168:
169: // indexes into factor array
170: public static final int UNITS = 0;
171: public static final int EACHES = 1;
172: public static final int DOZEN = 2;
173: public static final int HUNDRED = 3;
174: public static final int GROSS = 4;
175: public static final int MILLION = 5;
176: public static final int MAXUNIT = 5;
177:
178: // Index Typed factory methods
179: public static final Count newCount(double v, int unit) {
180: if (unit >= 0 && unit <= MAXUNIT)
181: return new Count(v * getConvFactor(unit));
182: else
183: throw new UnknownUnitException();
184: }
185:
186: public static final Count newCount(String s, int unit) {
187: if (unit >= 0 && unit <= MAXUNIT)
188: return new Count((Double.valueOf(s).doubleValue())
189: * getConvFactor(unit));
190: else
191: throw new UnknownUnitException();
192: }
193:
194: // Support for AbstractMeasure-level constructor
195: public static final AbstractMeasure newMeasure(String s, int unit) {
196: return newCount(s, unit);
197: }
198:
199: public static final AbstractMeasure newMeasure(double v, int unit) {
200: return newCount(v, unit);
201: }
202:
203: // simple math : addition and subtraction
204: public final Measure add(Measure toAdd) {
205: if (!(toAdd instanceof Count))
206: throw new IllegalArgumentException();
207: return new Count(theValue + toAdd.getNativeValue());
208: }
209:
210: public final Measure subtract(Measure toSubtract) {
211: if (!(toSubtract instanceof Count))
212: throw new IllegalArgumentException();
213: return new Count(theValue - toSubtract.getNativeValue());
214: }
215:
216: public final Measure scale(double scale) {
217: return new Count(theValue * scale, 0);
218: }
219:
220: public final Measure negate() {
221: return newCount(-1 * theValue, 0);
222: }
223:
224: public final Measure floor(int unit) {
225: return newCount(Math.floor(getValue(unit)), 0);
226: }
227:
228: public final Measure valueOf(double value) {
229: return new Count(value);
230: }
231:
232: public final Measure valueOf(double value, int unit) {
233: return new Count(value, unit);
234: }
235:
236: public final double getNativeValue() {
237: return theValue;
238: }
239:
240: public final int getNativeUnit() {
241: return 0;
242: }
243:
244: public final Duration divide(Rate toRate) {
245: Measure canonicalNumerator = toRate.getCanonicalNumerator();
246: if (!(toRate.getCanonicalNumerator() instanceof Count)) {
247: throw new IllegalArgumentException(
248: "Expecting a Count/Duration");
249: }
250: int durationNativeUnit = toRate.getCanonicalDenominator()
251: .getNativeUnit(); // seconds
252: double value = toRate.getValue(canonicalNumerator
253: .getNativeUnit(), durationNativeUnit); // ?/seconds
254: return new Duration(theValue / value, durationNativeUnit); // ?/?/second = seconds
255: }
256:
257: // Unit-based Reader methods
258: public double getUnits() {
259: return (theValue);
260: }
261:
262: public double getEaches() {
263: return (theValue / UNITS_PER_EACHES);
264: }
265:
266: public double getDozen() {
267: return (theValue / UNITS_PER_DOZEN);
268: }
269:
270: public double getHundred() {
271: return (theValue / UNITS_PER_HUNDRED);
272: }
273:
274: public double getGross() {
275: return (theValue / UNITS_PER_GROSS);
276: }
277:
278: public double getMillion() {
279: return (theValue / UNITS_PER_MILLION);
280: }
281:
282: public double getValue(int unit) {
283: if (unit >= 0 && unit <= MAXUNIT)
284: return (theValue / getConvFactor(unit));
285: else
286: throw new UnknownUnitException();
287: }
288:
289: public boolean equals(Object o) {
290: return (o instanceof Count && theValue == ((Count) o).theValue);
291: }
292:
293: public String toString() {
294: return Double.toString(theValue) + "units";
295: }
296:
297: public int hashCode() {
298: return (new Double(theValue)).hashCode();
299: }
300:
301: // serialization
302: public void writeExternal(ObjectOutput out) throws IOException {
303: out.writeDouble(theValue);
304: }
305:
306: public void readExternal(ObjectInput in) throws IOException {
307: theValue = in.readDouble();
308: }
309: }
|