01: /*
02: * <copyright>
03: *
04: * Copyright 1997-2004 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26: package org.cougaar.planning.ldm.measure;
27:
28: /** Base (abstract) implementation of all Measure classes.
29: * @see Measure for specification.
30: **/
31:
32: public abstract class AbstractMeasure implements Measure {
33:
34: public static AbstractMeasure newMeasure(String s, int unit) {
35: throw new UnknownUnitException();
36: }
37:
38: /** given a string like "100 meters", find the index of the 'm'
39: * in the units. Will search for the first of either the char
40: * after a space or the first letter found.
41: * if a likely spot is not found, return -1.
42: **/
43: protected final static int indexOfType(String s) {
44: int l = s.length();
45: for (int i = 0; i < l; i++) {
46: char c = s.charAt(i);
47: if (c == ' ')
48: return i + 1;
49: if (Character.isLetter(c))
50: return i;
51: }
52: return -1;
53: }
54:
55: public Measure min(Measure other) {
56: return (compareTo(other) <= 0 ? this : other);
57: }
58:
59: public Measure max(Measure other) {
60: return (compareTo(other) >= 0 ? this : other);
61: }
62:
63: public Measure apply(UnaryOperator op) {
64: return op.apply(this );
65: }
66:
67: public Measure apply(BinaryOperator op, Measure other) {
68: return op.apply(this , other);
69: }
70:
71: public int compareTo(Object o) {
72: double da = getNativeValue();
73: double db;
74: if (o == null) {
75: db = 0.0;
76: } else {
77: if (o.getClass() != getClass()) {
78: throw new IllegalArgumentException(
79: "Incompatible types:\n " + this + "\n " + o);
80: }
81: db = ((Measure) o).getNativeValue();
82: }
83: return (da < db ? -1 : da > db ? 1 : 0);
84: }
85: }
|