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: package org.cougaar.planning.ldm.plan;
028:
029: /** An AspectValue implementation which stores a float.
030: */
031:
032: public class FloatAspectValue extends TypedAspectValue {
033: private final float value;
034:
035: // zeros cache
036: private static final int ZEROS = 20;
037: private static final AspectValue zero[] = new AspectValue[ZEROS];
038: static {
039: // dumb, but we'll not worry about it (minimal excess AV creation)
040: for (int i = 0; i < ZEROS; i++) {
041: zero[i] = new FloatAspectValue(i, 0.0f);
042: }
043: }
044:
045: protected FloatAspectValue(int type, float value) {
046: super (type);
047: if (Float.isNaN(value) || Float.isInfinite(value))
048: throw new IllegalArgumentException(
049: "The value of a FloatAspectValue must be a finite, non-NaN");
050: this .value = value;
051: }
052:
053: public static AspectValue create(int type, Object o) {
054: float value;
055: if (o instanceof Number) {
056: value = ((Number) o).floatValue();
057: } else if (o instanceof AspectValue) {
058: value = ((AspectValue) o).floatValue();
059: } else {
060: throw new IllegalArgumentException(
061: "Cannot construct a FloatAspectValue from " + o);
062: }
063: return create(type, value);
064: }
065:
066: public static AspectValue create(int type, float value) {
067: if (value == 0.0 && type >= 0 && type < ZEROS) {
068: return zero[type];
069: }
070: return new FloatAspectValue(type, value);
071: }
072:
073: public final double doubleValue() {
074: return (double) value;
075: }
076:
077: public final long longValue() {
078: return Math.round(value);
079: }
080:
081: public final float floatValue() {
082: return value;
083: }
084:
085: public final int intValue() {
086: return (int) Math.round(value);
087: }
088:
089: public boolean equals(Object v) {
090: if (v instanceof FloatAspectValue) {
091: return (getType() == ((AspectValue) v).getType() && floatValue() == ((AspectValue) v)
092: .floatValue());
093: } else {
094: return false;
095: }
096: }
097:
098: public int hashCode() {
099: return getType() + ((int) (floatValue() * 128));
100: }
101:
102: public String toString() {
103: return Float.toString(floatValue()) + "[" + getType() + "]";
104: }
105:
106: }
|