KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > NumberRange


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.lang;
17
18 /**
19  * <p>Represents a range of {@link Number} objects.</p>
20  *
21  * <p>This class uses <code>double</code> comparisons. This means that it
22  * is unsuitable for dealing with large <code>Long</code>, <code>BigDecimal</code>
23  * or <code>BigInteger</code> numbers.</p>
24  *
25  * @author <a HREF="mailto:chrise@esha.com">Christopher Elkins</a>
26  * @author Stephen Colebourne
27  * @since 1.0
28  * @version $Revision: 161243 $ $Date: 2005-04-14 00:30:28 -0400 (Thu, 14 Apr 2005) $
29  *
30  * @deprecated Use one of the Range classes in org.apache.commons.lang.math.
31  * Class will be removed in Commons Lang 3.0.
32  *
33  */

34 public final class NumberRange {
35
36     /* The minimum number in this range. */
37     private final Number JavaDoc min;
38
39     /* The maximum number in this range. */
40     private final Number JavaDoc max;
41
42
43     /**
44      * <p>Constructs a new <code>NumberRange</code> using
45      * <code>number</code> as both the minimum and maximum in
46      * this range.</p>
47      *
48      * @param num the number to use for this range
49      * @throws NullPointerException if the number is <code>null</code>
50      */

51     public NumberRange(Number JavaDoc num) {
52         if (num == null) {
53             throw new NullPointerException JavaDoc("The number must not be null");
54         }
55
56         this.min = num;
57         this.max = num;
58     }
59
60     /**
61      * <p>Constructs a new <code>NumberRange</code> with the specified
62      * minimum and maximum numbers.</p>
63      *
64      * <p><em>If the maximum is less than the minimum, the range will be constructed
65      * from the minimum value to the minimum value, not what you would expect!.</em></p>
66      *
67      * @param min the minimum number in this range
68      * @param max the maximum number in this range
69      * @throws NullPointerException if either the minimum or maximum number is
70      * <code>null</code>
71      */

72     public NumberRange(Number JavaDoc min, Number JavaDoc max) {
73         if (min == null) {
74             throw new NullPointerException JavaDoc("The minimum value must not be null");
75         } else if (max == null) {
76             throw new NullPointerException JavaDoc("The maximum value must not be null");
77         }
78
79         if (max.doubleValue() < min.doubleValue()) {
80             this.min = this.max = min;
81         } else {
82             this.min = min;
83             this.max = max;
84         }
85     }
86
87     /**
88      * <p>Returns the minimum number in this range.</p>
89      *
90      * @return the minimum number in this range
91      */

92     public Number JavaDoc getMinimum() {
93         return min;
94     }
95
96     /**
97      * <p>Returns the maximum number in this range.</p>
98      *
99      * @return the maximum number in this range
100      */

101     public Number JavaDoc getMaximum() {
102         return max;
103     }
104
105     /**
106      * <p>Tests whether the specified <code>number</code> occurs within
107      * this range using <code>double</code> comparison.</p>
108      *
109      * @param number the number to test
110      * @return <code>true</code> if the specified number occurs within this
111      * range; otherwise, <code>false</code>
112      */

113     public boolean includesNumber(Number JavaDoc number) {
114         if (number == null) {
115             return false;
116         } else {
117             return !(min.doubleValue() > number.doubleValue()) &&
118                 !(max.doubleValue() < number.doubleValue());
119         }
120     }
121
122     /**
123      * <p>Tests whether the specified range occurs entirely within this
124      * range using <code>double</code> comparison.</p>
125      *
126      * @param range the range to test
127      * @return <code>true</code> if the specified range occurs entirely within
128      * this range; otherwise, <code>false</code>
129      */

130     public boolean includesRange(NumberRange range) {
131         if (range == null) {
132             return false;
133         } else {
134             return includesNumber(range.min) && includesNumber(range.max);
135         }
136     }
137
138     /**
139      * <p>Tests whether the specified range overlaps with this range
140      * using <code>double</code> comparison.</p>
141      *
142      * @param range the range to test
143      * @return <code>true</code> if the specified range overlaps with this
144      * range; otherwise, <code>false</code>
145      */

146     public boolean overlaps(NumberRange range) {
147         if (range == null) {
148             return false;
149         } else {
150             return range.includesNumber(min) || range.includesNumber(max) ||
151                 includesRange(range);
152         }
153     }
154
155     /**
156      * <p>Indicates whether some other <code>Object</code> is
157      * &quot;equal&quot; to this one.</p>
158      *
159      * @param obj the reference object with which to compare
160      * @return <code>true</code> if this object is the same as the obj
161      * argument; <code>false</code> otherwise
162      */

163     public boolean equals(Object JavaDoc obj) {
164         if (obj == this) {
165             return true;
166         } else if (!(obj instanceof NumberRange)) {
167             return false;
168         } else {
169             NumberRange range = (NumberRange)obj;
170             return min.equals(range.min) && max.equals(range.max);
171         }
172     }
173
174     /**
175      * <p>Returns a hash code value for this object.</p>
176      *
177      * @return a hash code value for this object
178      */

179     public int hashCode() {
180         int result = 17;
181         result = 37 * result + min.hashCode();
182         result = 37 * result + max.hashCode();
183         return result;
184     }
185
186     /**
187      * <p>Returns the string representation of this range.</p>
188      *
189      * <p>This string is the string representation of the minimum and
190      * maximum numbers in the range, separated by a hyphen. If a number
191      * is negative, then it is enclosed in parentheses.</p>
192      *
193      * @return the string representation of this range
194      */

195     public String JavaDoc toString() {
196         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
197
198         if (min.doubleValue() < 0) {
199             sb.append('(')
200                 .append(min)
201                 .append(')');
202         } else {
203             sb.append(min);
204         }
205
206         sb.append('-');
207
208         if (max.doubleValue() < 0) {
209             sb.append('(')
210                 .append(max)
211                 .append(')');
212         } else {
213             sb.append(max);
214         }
215
216         return sb.toString();
217     }
218
219 }
220
Popular Tags