KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > math > IntRange


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.math;
17
18 import java.io.Serializable JavaDoc;
19
20 /**
21  * <p><code>IntRange</code> represents an inclusive range of <code>int</code>s.</p>
22  *
23  * @author Stephen Colebourne
24  * @since 2.0
25  * @version $Id: IntRange.java 161243 2005-04-14 04:30:28Z ggregory $
26  */

27 public final class IntRange extends Range implements Serializable JavaDoc {
28     
29     private static final long serialVersionUID = 71849363892730L;
30
31     /**
32      * The minimum number in this range (inclusive).
33      */

34     private final int min;
35     /**
36      * The maximum number in this range (inclusive).
37      */

38     private final int max;
39     
40     /**
41      * Cached output minObject (class is immutable).
42      */

43     private transient Integer JavaDoc minObject = null;
44     /**
45      * Cached output maxObject (class is immutable).
46      */

47     private transient Integer JavaDoc maxObject = null;
48     /**
49      * Cached output hashCode (class is immutable).
50      */

51     private transient int hashCode = 0;
52     /**
53      * Cached output toString (class is immutable).
54      */

55     private transient String JavaDoc toString = null;
56     
57     /**
58      * <p>Constructs a new <code>IntRange</code> using the specified
59      * number as both the minimum and maximum in this range.</p>
60      *
61      * @param number the number to use for this range
62      */

63     public IntRange(int number) {
64         super();
65         this.min = number;
66         this.max = number;
67     }
68
69     /**
70      * <p>Constructs a new <code>IntRange</code> using the specified
71      * number as both the minimum and maximum in this range.</p>
72      *
73      * @param number the number to use for this range, must not be <code>null</code>
74      * @throws IllegalArgumentException if the number is <code>null</code>
75      */

76     public IntRange(Number JavaDoc number) {
77         super();
78         if (number == null) {
79             throw new IllegalArgumentException JavaDoc("The number must not be null");
80         }
81         this.min = number.intValue();
82         this.max = number.intValue();
83         if (number instanceof Integer JavaDoc) {
84             this.minObject = (Integer JavaDoc) number;
85             this.maxObject = (Integer JavaDoc) number;
86         }
87     }
88
89     /**
90      * <p>Constructs a new <code>IntRange</code> with the specified
91      * minimum and maximum numbers (both inclusive).</p>
92      *
93      * <p>The arguments may be passed in the order (min,max) or (max,min). The
94      * getMinimum and getMaximum methods will return the correct values.</p>
95      *
96      * @param number1 first number that defines the edge of the range, inclusive
97      * @param number2 second number that defines the edge of the range, inclusive
98      */

99     public IntRange(int number1, int number2) {
100         super();
101         if (number2 < number1) {
102             this.min = number2;
103             this.max = number1;
104         } else {
105             this.min = number1;
106             this.max = number2;
107         }
108     }
109
110     /**
111      * <p>Constructs a new <code>IntRange</code> with the specified
112      * minimum and maximum numbers (both inclusive).</p>
113      *
114      * <p>The arguments may be passed in the order (min,max) or (max,min). The
115      * getMinimum and getMaximum methods will return the correct values.</p>
116      *
117      * @param number1 first number that defines the edge of the range, inclusive
118      * @param number2 second number that defines the edge of the range, inclusive
119      * @throws IllegalArgumentException if either number is <code>null</code>
120      */

121     public IntRange(Number JavaDoc number1, Number JavaDoc number2) {
122         super();
123         if (number1 == null || number2 == null) {
124             throw new IllegalArgumentException JavaDoc("The numbers must not be null");
125         }
126         int number1val = number1.intValue();
127         int number2val = number2.intValue();
128         if (number2val < number1val) {
129             this.min = number2val;
130             this.max = number1val;
131             if (number2 instanceof Integer JavaDoc) {
132                 this.minObject = (Integer JavaDoc) number2;
133             }
134             if (number1 instanceof Integer JavaDoc) {
135                 this.maxObject = (Integer JavaDoc) number1;
136             }
137         } else {
138             this.min = number1val;
139             this.max = number2val;
140             if (number1 instanceof Integer JavaDoc) {
141                 this.minObject = (Integer JavaDoc) number1;
142             }
143             if (number2 instanceof Integer JavaDoc) {
144                 this.maxObject = (Integer JavaDoc) number2;
145             }
146         }
147     }
148
149     // Accessors
150
//--------------------------------------------------------------------
151

152     /**
153      * <p>Returns the minimum number in this range.</p>
154      *
155      * @return the minimum number in this range
156      */

157     public Number JavaDoc getMinimumNumber() {
158         if (minObject == null) {
159             minObject = new Integer JavaDoc(min);
160         }
161         return minObject;
162     }
163
164     /**
165      * <p>Gets the minimum number in this range as a <code>long</code>.</p>
166      *
167      * @return the minimum number in this range
168      */

169     public long getMinimumLong() {
170         return min;
171     }
172
173     /**
174      * <p>Gets the minimum number in this range as a <code>int</code>.</p>
175      *
176      * @return the minimum number in this range
177      */

178     public int getMinimumInteger() {
179         return min;
180     }
181
182     /**
183      * <p>Gets the minimum number in this range as a <code>double</code>.</p>
184      *
185      * @return the minimum number in this range
186      */

187     public double getMinimumDouble() {
188         return min;
189     }
190
191     /**
192      * <p>Gets the minimum number in this range as a <code>float</code>.</p>
193      *
194      * @return the minimum number in this range
195      */

196     public float getMinimumFloat() {
197         return min;
198     }
199
200     /**
201      * <p>Returns the maximum number in this range.</p>
202      *
203      * @return the maximum number in this range
204      */

205     public Number JavaDoc getMaximumNumber() {
206         if (maxObject == null) {
207             maxObject = new Integer JavaDoc(max);
208         }
209         return maxObject;
210     }
211
212     /**
213      * <p>Gets the maximum number in this range as a <code>long</code>.</p>
214      *
215      * @return the maximum number in this range
216      */

217     public long getMaximumLong() {
218         return max;
219     }
220
221     /**
222      * <p>Gets the maximum number in this range as a <code>int</code>.</p>
223      *
224      * @return the maximum number in this range
225      */

226     public int getMaximumInteger() {
227         return max;
228     }
229
230     /**
231      * <p>Gets the maximum number in this range as a <code>double</code>.</p>
232      *
233      * @return the maximum number in this range
234      */

235     public double getMaximumDouble() {
236         return max;
237     }
238
239     /**
240      * <p>Gets the maximum number in this range as a <code>float</code>.</p>
241      *
242      * @return the maximum number in this range
243      */

244     public float getMaximumFloat() {
245         return max;
246     }
247
248     // Tests
249
//--------------------------------------------------------------------
250

251     /**
252      * <p>Tests whether the specified <code>number</code> occurs within
253      * this range using <code>int</code> comparison.</p>
254      *
255      * <p><code>null</code> is handled and returns <code>false</code>.</p>
256      *
257      * @param number the number to test, may be <code>null</code>
258      * @return <code>true</code> if the specified number occurs within this range
259      */

260     public boolean containsNumber(Number JavaDoc number) {
261         if (number == null) {
262             return false;
263         }
264         return containsInteger(number.intValue());
265     }
266
267     /**
268      * <p>Tests whether the specified <code>int</code> occurs within
269      * this range using <code>int</code> comparison.</p>
270      *
271      * <p>This implementation overrides the superclass for performance as it is
272      * the most common case.</p>
273      *
274      * @param value the int to test
275      * @return <code>true</code> if the specified number occurs within this
276      * range by <code>int</code> comparison
277      */

278     public boolean containsInteger(int value) {
279         return value >= min && value <= max;
280     }
281
282     // Range tests
283
//--------------------------------------------------------------------
284

285     /**
286      * <p>Tests whether the specified range occurs entirely within this range
287      * using <code>int</code> comparison.</p>
288      *
289      * <p><code>null</code> is handled and returns <code>false</code>.</p>
290      *
291      * @param range the range to test, may be <code>null</code>
292      * @return <code>true</code> if the specified range occurs entirely within this range
293      * @throws IllegalArgumentException if the range is not of this type
294      */

295     public boolean containsRange(Range range) {
296         if (range == null) {
297             return false;
298         }
299         return containsInteger(range.getMinimumInteger()) &&
300                containsInteger(range.getMaximumInteger());
301     }
302
303     /**
304      * <p>Tests whether the specified range overlaps with this range
305      * using <code>int</code> comparison.</p>
306      *
307      * <p><code>null</code> is handled and returns <code>false</code>.</p>
308      *
309      * @param range the range to test, may be <code>null</code>
310      * @return <code>true</code> if the specified range overlaps with this range
311      */

312     public boolean overlapsRange(Range range) {
313         if (range == null) {
314             return false;
315         }
316         return range.containsInteger(min) ||
317                range.containsInteger(max) ||
318                containsInteger(range.getMinimumInteger());
319     }
320
321     // Basics
322
//--------------------------------------------------------------------
323

324     /**
325      * <p>Compares this range to another object to test if they are equal.</p>.
326      *
327      * <p>To be equal, the class, minimum and maximum must be equal.</p>
328      *
329      * @param obj the reference object with which to compare
330      * @return <code>true</code> if this object is equal
331      */

332     public boolean equals(Object JavaDoc obj) {
333         if (obj == this) {
334             return true;
335         }
336         if (obj instanceof IntRange == false) {
337             return false;
338         }
339         IntRange range = (IntRange) obj;
340         return min == range.min && max == range.max;
341     }
342
343     /**
344      * <p>Gets a hashCode for the range.</p>
345      *
346      * @return a hash code value for this object
347      */

348     public int hashCode() {
349         if (hashCode == 0) {
350             hashCode = 17;
351             hashCode = 37 * hashCode + getClass().hashCode();
352             hashCode = 37 * hashCode + min;
353             hashCode = 37 * hashCode + max;
354         }
355         return hashCode;
356     }
357
358     /**
359      * <p>Gets the range as a <code>String</code>.</p>
360      *
361      * <p>The format of the String is 'Range[<i>min</i>,<i>max</i>]'.</p>
362      *
363      * @return the <code>String</code> representation of this range
364      */

365     public String JavaDoc toString() {
366         if (toString == null) {
367             StringBuffer JavaDoc buf = new StringBuffer JavaDoc(32);
368             buf.append("Range[");
369             buf.append(min);
370             buf.append(',');
371             buf.append(max);
372             buf.append(']');
373             toString = buf.toString();
374         }
375         return toString;
376     }
377
378 }
379
Popular Tags