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: package org.cougaar.planning.ldm.measure;
027:
028: import java.lang.reflect.Constructor;
029:
030: /** A generic Scalar per Duration class that can be interpreted in a
031: * variety of ways depending on context.
032: **/
033:
034: public class Capacity extends AbstractMeasure {
035: private Scalar quantity;
036: private Duration period;
037:
038: /** Construct a capacity object which represents
039: * a Scalar Measure (e.g. Volume, Distance, etc) divided by a
040: * Duration (time). Example: Miles per hour, hours per day, gallons per second.
041: * May also be used
042: **/
043: public Capacity(Scalar quantity, Duration period) {
044: this .quantity = quantity;
045: this .period = period;
046: }
047:
048: /** String constructor. Allowed syntax is:
049: * "Scalarmeasure=ValueUnits Duration=ValueUnits"
050: * Example: "Count=20units Duration=1weeks"
051: * For instantaneous: "Count=20units"
052: **/
053: public Capacity(String s) {
054: int i = s.indexOf(' ');
055: String qs;
056: String ps;
057: if (i == -1) {
058: qs = s;
059: ps = null;
060: } else {
061: qs = s.substring(0, i);
062: ps = s.substring(i + 1);
063: }
064:
065: try {
066: Class[] sa = new Class[1];
067: sa[0] = String.class;
068:
069: int qse = qs.indexOf('=');
070: String qsn = qs.substring(0, qse);
071: Class qsc = Class
072: .forName("org.cougaar.planning.ldm.measure." + qsn);
073: Constructor qscc = qsc.getConstructor(sa);
074: Object[] qargs = new Object[1];
075: qargs[0] = qs.substring(qse + 1);
076: quantity = (Scalar) qscc.newInstance(qargs);
077:
078: if (ps != null) {
079: int pse = ps.indexOf('=');
080: String psn = ps.substring(0, pse);
081: Class psc = Class
082: .forName("org.cougaar.planning.ldm.measure."
083: + psn);
084: Constructor pscc = psc.getConstructor(sa);
085: Object[] pargs = new Object[1];
086: pargs[0] = ps.substring(pse + 1);
087: period = (Duration) pscc.newInstance(pargs);
088: } else {
089: period = null;
090: }
091: } catch (Exception e) {
092: e.printStackTrace();
093: throw new RuntimeException("Exception: " + e);
094: }
095: }
096:
097: /** @return an illegal value, since Capacity is a tuple **/
098: public int getCommonUnit() {
099: return -1;
100: }
101:
102: public int getMaxUnit() {
103: return -1;
104: }
105:
106: /** @return null, since Capacity is a tuple **/
107: public String getUnitName(int i) {
108: return null;
109: }
110:
111: public double getValue(int unit) {
112: throw new IllegalArgumentException();
113: }
114:
115: /** Construct an "instantaneous" capacity object - e.g.
116: * the capactity of "holding 10000 Gallons" or "having 48 Het-equivalents".
117: **/
118: public Capacity(Scalar quantity) {
119: this .quantity = quantity;
120: this .period = null;
121: }
122:
123: /** @return the numerator of the Capacity **/
124: public Scalar getQuantity() {
125: return quantity;
126: }
127:
128: /** @return the denominator of the Capacty **/
129: public Duration getPeriod() {
130: return period;
131: }
132:
133: /** is this a representation of instantaneous capacity? **/
134: public boolean isInstantaneous() {
135: return period == null;
136: }
137:
138: /** compute the figure that the capacity represents. E.g.
139: * Capacity cap = new Capacity(new Volume("10 liters"), new Duration("1 seconds"));
140: * double rate = cap.getCapacity(Volume.GALLONS, Duration.HOURS);
141: *
142: * Note that to get the quantityUnits correct, you need to know
143: * what the concrete type of the quantity is.
144: *
145: * @exception UnknownUnitException if a bad unit in either argument.
146: * @exceptionx DivideByZero if duration is 0.
147: * @exception NullPointerException if duration is null.
148: **/
149: public double getRate(int quantityUnits, int periodUnits) {
150: if (isInstantaneous())
151: throw new IllegalArgumentException(
152: "Cannot compute rate of an instantaneous Capacity");
153: return quantity.getValue(quantityUnits)
154: / period.getValue(periodUnits);
155: }
156:
157: /**
158: * TODO : fill in
159: * @param other
160: * @return
161: */
162: public Measure add(Measure other) {
163: return null;
164: }
165:
166: /**
167: * TODO : fill in
168: * @param other
169: * @return
170: */
171: public Measure subtract(Measure other) {
172: return null;
173: }
174:
175: /**
176: * TODO : fill in
177: * @param other
178: * @return
179: */
180: public Measure multiply(Measure other) {
181: return null;
182: }
183:
184: /**
185: * TODO : fill in
186: * @param other
187: * @return
188: */
189: public Duration divide(Rate other) {
190: return null;
191: }
192:
193: public Measure negate() {
194: return null;
195: }
196:
197: public Measure scale(double scale) {
198: return null;
199: }
200:
201: public Measure floor(int unit) {
202: return null;
203: }
204:
205: public Measure valueOf(double value) {
206: return null;
207: }
208:
209: public Measure valueOf(double value, int unit) {
210: return null;
211: }
212:
213: public int getNativeUnit() {
214: return 0;
215: }
216:
217: public double getNativeValue() {
218: return getValue(getNativeUnit());
219: }
220:
221: public String toString() {
222: if (isInstantaneous()) {
223: return "Capacity of " + quantity;
224: } else {
225: return quantity + " per " + period;
226: }
227: }
228:
229: public boolean equals(Object o) {
230: if (o instanceof Capacity) {
231: Capacity oc = (Capacity) o;
232: Duration ocp = oc.getPeriod();
233: if ((period == null) != (ocp == null))
234: return false;
235: if (period == null)
236: return quantity.equals(oc.getQuantity());
237: else
238: return (getRate(0, 0) == oc.getRate(0, 0));
239: } else
240: return false;
241: }
242:
243: public int hashCode() {
244: int qh = quantity.hashCode();
245: if (period != null)
246: qh = (qh << 2) + period.hashCode();
247: return qh;
248: }
249:
250: }
|