1 16 package org.apache.commons.lang.math; 17 18 import java.io.Serializable ; 19 20 29 public final class NumberRange extends Range implements Serializable { 30 31 private static final long serialVersionUID = 71849363892710L; 32 33 36 private final Number min; 37 40 private final Number max; 41 42 45 private transient int hashCode = 0; 46 49 private transient String toString = null; 50 51 60 public NumberRange(Number num) { 61 if (num == null) { 62 throw new IllegalArgumentException ("The number must not be null"); 63 } 64 if (num instanceof Comparable == false) { 65 throw new IllegalArgumentException ("The number must implement Comparable"); 66 } 67 if (num instanceof Double && ((Double ) num).isNaN()) { 68 throw new IllegalArgumentException ("The number must not be NaN"); 69 } 70 if (num instanceof Float && ((Float ) num).isNaN()) { 71 throw new IllegalArgumentException ("The number must not be NaN"); 72 } 73 74 this.min = num; 75 this.max = num; 76 } 77 78 96 public NumberRange(Number num1, Number num2) { 97 if (num1 == null || num2 == null) { 98 throw new IllegalArgumentException ("The numbers must not be null"); 99 } 100 if (num1.getClass() != num2.getClass()) { 101 throw new IllegalArgumentException ("The numbers must be of the same type"); 102 } 103 if (num1 instanceof Comparable == false) { 104 throw new IllegalArgumentException ("The numbers must implement Comparable"); 105 } 106 if (num1 instanceof Double ) { 107 if (((Double ) num1).isNaN() || ((Double ) num2).isNaN()) { 108 throw new IllegalArgumentException ("The number must not be NaN"); 109 } 110 } else if (num1 instanceof Float ) { 111 if (((Float ) num1).isNaN() || ((Float ) num2).isNaN()) { 112 throw new IllegalArgumentException ("The number must not be NaN"); 113 } 114 } 115 116 int compare = ((Comparable ) num1).compareTo(num2); 117 if (compare == 0) { 118 this.min = num1; 119 this.max = num1; 120 } else if (compare > 0) { 121 this.min = num2; 122 this.max = num1; 123 } else { 124 this.min = num1; 125 this.max = num2; 126 } 127 } 128 129 132 137 public Number getMinimumNumber() { 138 return min; 139 } 140 141 146 public Number getMaximumNumber() { 147 return max; 148 } 149 150 153 163 public boolean containsNumber(Number number) { 164 if (number == null) { 165 return false; 166 } 167 if (number.getClass() != min.getClass()) { 168 throw new IllegalArgumentException ("The number must be of the same type as the range numbers"); 169 } 170 int compareMin = ((Comparable ) min).compareTo(number); 171 int compareMax = ((Comparable ) max).compareTo(number); 172 return compareMin <= 0 && compareMax >= 0; 173 } 174 175 179 182 190 public boolean equals(Object obj) { 191 if (obj == this) { 192 return true; 193 } 194 if (obj instanceof NumberRange == false) { 195 return false; 196 } 197 NumberRange range = (NumberRange) obj; 198 return min.equals(range.min) && max.equals(range.max); 199 } 200 201 206 public int hashCode() { 207 if (hashCode == 0) { 208 hashCode = 17; 209 hashCode = 37 * hashCode + getClass().hashCode(); 210 hashCode = 37 * hashCode + min.hashCode(); 211 hashCode = 37 * hashCode + max.hashCode(); 212 } 213 return hashCode; 214 } 215 216 223 public String toString() { 224 if (toString == null) { 225 StringBuffer buf = new StringBuffer (32); 226 buf.append("Range["); 227 buf.append(min); 228 buf.append(','); 229 buf.append(max); 230 buf.append(']'); 231 toString = buf.toString(); 232 } 233 return toString; 234 } 235 236 } 237 | Popular Tags |