001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.lang;
018:
019: /**
020: * <p>Represents a range of {@link Number} objects.</p>
021: *
022: * <p>This class uses <code>double</code> comparisons. This means that it
023: * is unsuitable for dealing with large <code>Long</code>, <code>BigDecimal</code>
024: * or <code>BigInteger</code> numbers.</p>
025: *
026: * @author <a href="mailto:chrise@esha.com">Christopher Elkins</a>
027: * @author Stephen Colebourne
028: * @since 1.0
029: * @version $Revision: 437554 $ $Date: 2006-08-27 23:21:41 -0700 (Sun, 27 Aug 2006) $
030: *
031: * @deprecated Use one of the Range classes in org.apache.commons.lang.math.
032: * Class will be removed in Commons Lang 3.0.
033: *
034: */
035: public final class NumberRange {
036:
037: /* The minimum number in this range. */
038: private final Number min;
039:
040: /* The maximum number in this range. */
041: private final Number max;
042:
043: /**
044: * <p>Constructs a new <code>NumberRange</code> using
045: * <code>number</code> as both the minimum and maximum in
046: * this range.</p>
047: *
048: * @param num the number to use for this range
049: * @throws NullPointerException if the number is <code>null</code>
050: */
051: public NumberRange(Number num) {
052: if (num == null) {
053: throw new NullPointerException(
054: "The number must not be null");
055: }
056:
057: this .min = num;
058: this .max = num;
059: }
060:
061: /**
062: * <p>Constructs a new <code>NumberRange</code> with the specified
063: * minimum and maximum numbers.</p>
064: *
065: * <p><em>If the maximum is less than the minimum, the range will be constructed
066: * from the minimum value to the minimum value, not what you would expect!.</em></p>
067: *
068: * @param min the minimum number in this range
069: * @param max the maximum number in this range
070: * @throws NullPointerException if either the minimum or maximum number is
071: * <code>null</code>
072: */
073: public NumberRange(Number min, Number max) {
074: if (min == null) {
075: throw new NullPointerException(
076: "The minimum value must not be null");
077: } else if (max == null) {
078: throw new NullPointerException(
079: "The maximum value must not be null");
080: }
081:
082: if (max.doubleValue() < min.doubleValue()) {
083: this .min = this .max = min;
084: } else {
085: this .min = min;
086: this .max = max;
087: }
088: }
089:
090: /**
091: * <p>Returns the minimum number in this range.</p>
092: *
093: * @return the minimum number in this range
094: */
095: public Number getMinimum() {
096: return min;
097: }
098:
099: /**
100: * <p>Returns the maximum number in this range.</p>
101: *
102: * @return the maximum number in this range
103: */
104: public Number getMaximum() {
105: return max;
106: }
107:
108: /**
109: * <p>Tests whether the specified <code>number</code> occurs within
110: * this range using <code>double</code> comparison.</p>
111: *
112: * @param number the number to test
113: * @return <code>true</code> if the specified number occurs within this
114: * range; otherwise, <code>false</code>
115: */
116: public boolean includesNumber(Number number) {
117: if (number == null) {
118: return false;
119: } else {
120: return !(min.doubleValue() > number.doubleValue())
121: && !(max.doubleValue() < number.doubleValue());
122: }
123: }
124:
125: /**
126: * <p>Tests whether the specified range occurs entirely within this
127: * range using <code>double</code> comparison.</p>
128: *
129: * @param range the range to test
130: * @return <code>true</code> if the specified range occurs entirely within
131: * this range; otherwise, <code>false</code>
132: */
133: public boolean includesRange(NumberRange range) {
134: if (range == null) {
135: return false;
136: } else {
137: return includesNumber(range.min)
138: && includesNumber(range.max);
139: }
140: }
141:
142: /**
143: * <p>Tests whether the specified range overlaps with this range
144: * using <code>double</code> comparison.</p>
145: *
146: * @param range the range to test
147: * @return <code>true</code> if the specified range overlaps with this
148: * range; otherwise, <code>false</code>
149: */
150: public boolean overlaps(NumberRange range) {
151: if (range == null) {
152: return false;
153: } else {
154: return range.includesNumber(min)
155: || range.includesNumber(max)
156: || includesRange(range);
157: }
158: }
159:
160: /**
161: * <p>Indicates whether some other <code>Object</code> is
162: * "equal" to this one.</p>
163: *
164: * @param obj the reference object with which to compare
165: * @return <code>true</code> if this object is the same as the obj
166: * argument; <code>false</code> otherwise
167: */
168: public boolean equals(Object obj) {
169: if (obj == this ) {
170: return true;
171: } else if (!(obj instanceof NumberRange)) {
172: return false;
173: } else {
174: NumberRange range = (NumberRange) obj;
175: return min.equals(range.min) && max.equals(range.max);
176: }
177: }
178:
179: /**
180: * <p>Returns a hash code value for this object.</p>
181: *
182: * @return a hash code value for this object
183: */
184: public int hashCode() {
185: int result = 17;
186: result = 37 * result + min.hashCode();
187: result = 37 * result + max.hashCode();
188: return result;
189: }
190:
191: /**
192: * <p>Returns the string representation of this range.</p>
193: *
194: * <p>This string is the string representation of the minimum and
195: * maximum numbers in the range, separated by a hyphen. If a number
196: * is negative, then it is enclosed in parentheses.</p>
197: *
198: * @return the string representation of this range
199: */
200: public String toString() {
201: StringBuffer sb = new StringBuffer();
202:
203: if (min.doubleValue() < 0) {
204: sb.append('(').append(min).append(')');
205: } else {
206: sb.append(min);
207: }
208:
209: sb.append('-');
210:
211: if (max.doubleValue() < 0) {
212: sb.append('(').append(max).append(')');
213: } else {
214: sb.append(max);
215: }
216:
217: return sb.toString();
218: }
219:
220: }
|