source: src/main/java/agents/org/apache/commons/lang/NumberRange.java

Last change on this file was 127, checked in by Wouter Pasman, 6 years ago

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 7.1 KB
Line 
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package agents.org.apache.commons.lang;
18
19import agents.org.apache.commons.lang.text.StrBuilder;
20
21/**
22 * <p>Represents a range of {@link Number} objects.</p>
23 *
24 * <p>This class uses <code>double</code> comparisons. This means that it
25 * is unsuitable for dealing with large <code>Long</code>, <code>BigDecimal</code>
26 * or <code>BigInteger</code> numbers.</p>
27 *
28 * @author Apache Software Foundation
29 * @author <a href="mailto:chrise@esha.com">Christopher Elkins</a>
30 * @since 1.0
31 * @version $Revision: 1057072 $ $Date: 2011-01-10 01:55:57 +0000 (Mon, 10 Jan 2011) $
32 *
33 * @deprecated Use one of the Range classes in org.apache.commons.lang.math.
34 * Class will be removed in Commons Lang 3.0.
35 *
36 */
37public final class NumberRange {
38
39 /* The minimum number in this range. */
40 private final Number min;
41
42 /* The maximum number in this range. */
43 private final Number max;
44
45
46 /**
47 * <p>Constructs a new <code>NumberRange</code> using
48 * <code>number</code> as both the minimum and maximum in
49 * this range.</p>
50 *
51 * @param num the number to use for this range
52 * @throws NullPointerException if the number is <code>null</code>
53 */
54 public NumberRange(Number num) {
55 if (num == null) {
56 throw new NullPointerException("The number must not be null");
57 }
58
59 this.min = num;
60 this.max = num;
61 }
62
63 /**
64 * <p>Constructs a new <code>NumberRange</code> with the specified
65 * minimum and maximum numbers.</p>
66 *
67 * <p><em>If the maximum is less than the minimum, the range will be constructed
68 * from the minimum value to the minimum value, not what you would expect!.</em></p>
69 *
70 * @param min the minimum number in this range
71 * @param max the maximum number in this range
72 * @throws NullPointerException if either the minimum or maximum number is
73 * <code>null</code>
74 */
75 public NumberRange(Number min, Number max) {
76 if (min == null) {
77 throw new NullPointerException("The minimum value must not be null");
78 } else if (max == null) {
79 throw new NullPointerException("The maximum value must not be null");
80 }
81
82 if (max.doubleValue() < min.doubleValue()) {
83 this.min = this.max = min;
84 } else {
85 this.min = min;
86 this.max = max;
87 }
88 }
89
90 /**
91 * <p>Returns the minimum number in this range.</p>
92 *
93 * @return the minimum number in this range
94 */
95 public Number getMinimum() {
96 return min;
97 }
98
99 /**
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) && includesNumber(range.max);
138 }
139 }
140
141 /**
142 * <p>Tests whether the specified range overlaps with this range
143 * using <code>double</code> comparison.</p>
144 *
145 * @param range the range to test
146 * @return <code>true</code> if the specified range overlaps with this
147 * range; otherwise, <code>false</code>
148 */
149 public boolean overlaps(NumberRange range) {
150 if (range == null) {
151 return false;
152 } else {
153 return range.includesNumber(min) || range.includesNumber(max) ||
154 includesRange(range);
155 }
156 }
157
158 /**
159 * <p>Indicates whether some other <code>Object</code> is
160 * &quot;equal&quot; to this one.</p>
161 *
162 * @param obj the reference object with which to compare
163 * @return <code>true</code> if this object is the same as the obj
164 * argument; <code>false</code> otherwise
165 */
166 public boolean equals(Object obj) {
167 if (obj == this) {
168 return true;
169 } else if (!(obj instanceof NumberRange)) {
170 return false;
171 } else {
172 NumberRange range = (NumberRange)obj;
173 return min.equals(range.min) && max.equals(range.max);
174 }
175 }
176
177 /**
178 * <p>Returns a hash code value for this object.</p>
179 *
180 * @return a hash code value for this object
181 */
182 public int hashCode() {
183 int result = 17;
184 result = 37 * result + min.hashCode();
185 result = 37 * result + max.hashCode();
186 return result;
187 }
188
189 /**
190 * <p>Returns the string representation of this range.</p>
191 *
192 * <p>This string is the string representation of the minimum and
193 * maximum numbers in the range, separated by a hyphen. If a number
194 * is negative, then it is enclosed in parentheses.</p>
195 *
196 * @return the string representation of this range
197 */
198 public String toString() {
199 StrBuilder sb = new StrBuilder();
200
201 if (min.doubleValue() < 0) {
202 sb.append('(')
203 .append(min)
204 .append(')');
205 } else {
206 sb.append(min);
207 }
208
209 sb.append('-');
210
211 if (max.doubleValue() < 0) {
212 sb.append('(')
213 .append(max)
214 .append(')');
215 } else {
216 sb.append(max);
217 }
218
219 return sb.toString();
220 }
221
222}
Note: See TracBrowser for help on using the repository browser.