001: /*
002: * <copyright>
003: *
004: * Copyright 2002-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.core.adaptivity;
028:
029: import java.io.Serializable;
030:
031: /**
032: * Holds a range specification for an operating mode or condition
033: * value. Ranges are half-open intervals. The value of max _must_
034: * exceed low.
035: **/
036: public class OMCRange implements Serializable {
037: protected Comparable min, max;
038:
039: /**
040: * Constructor for int values
041: **/
042: protected OMCRange(int min, int max) {
043: this (new Integer(min), new Integer(max));
044: }
045:
046: /**
047: * Constructor for double values
048: **/
049: protected OMCRange(double min, double max) {
050: this (new Double(min), new Double(max));
051: }
052:
053: /**
054: * Constructor for Comparable values
055: **/
056: protected OMCRange(Comparable min, Comparable max) {
057: if (min.getClass() != max.getClass()) {
058: throw new IllegalArgumentException(
059: "Min and max have different classes");
060: }
061: if (min.compareTo(max) > 0) {
062: throw new IllegalArgumentException(
063: "Min must not exceed max");
064: }
065: this .min = min;
066: this .max = max;
067: }
068:
069: /**
070: * Test if a value is in this range.
071: * @return true if the value is in the (closed) interval between min
072: * and max.
073: * @param v The value to compare.
074: **/
075: public boolean contains(Comparable v) {
076: return min.compareTo(v) <= 0 && max.compareTo(v) >= 0;
077: }
078:
079: /**
080: * Gets the minimum value in the range.
081: **/
082: public Comparable getMin() {
083: return min;
084: }
085:
086: /**
087: * Gets the maximum value in the range.
088: **/
089: public Comparable getMax() {
090: return max;
091: }
092:
093: public int hashCode() {
094: return min.hashCode() ^ max.hashCode();
095: }
096:
097: public boolean equals(Object o) {
098: if (o instanceof OMCRange) {
099: OMCRange that = (OMCRange) o;
100: return this .min.equals(that.min)
101: && this .max.equals(that.max);
102: }
103: return false;
104: }
105: }
|