1 16 package org.apache.commons.lang.math; 17 18 import java.io.Serializable ; 19 20 27 public final class IntRange extends Range implements Serializable { 28 29 private static final long serialVersionUID = 71849363892730L; 30 31 34 private final int min; 35 38 private final int max; 39 40 43 private transient Integer minObject = null; 44 47 private transient Integer maxObject = null; 48 51 private transient int hashCode = 0; 52 55 private transient String toString = null; 56 57 63 public IntRange(int number) { 64 super(); 65 this.min = number; 66 this.max = number; 67 } 68 69 76 public IntRange(Number number) { 77 super(); 78 if (number == null) { 79 throw new IllegalArgumentException ("The number must not be null"); 80 } 81 this.min = number.intValue(); 82 this.max = number.intValue(); 83 if (number instanceof Integer ) { 84 this.minObject = (Integer ) number; 85 this.maxObject = (Integer ) number; 86 } 87 } 88 89 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 121 public IntRange(Number number1, Number number2) { 122 super(); 123 if (number1 == null || number2 == null) { 124 throw new IllegalArgumentException ("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 ) { 132 this.minObject = (Integer ) number2; 133 } 134 if (number1 instanceof Integer ) { 135 this.maxObject = (Integer ) number1; 136 } 137 } else { 138 this.min = number1val; 139 this.max = number2val; 140 if (number1 instanceof Integer ) { 141 this.minObject = (Integer ) number1; 142 } 143 if (number2 instanceof Integer ) { 144 this.maxObject = (Integer ) number2; 145 } 146 } 147 } 148 149 152 157 public Number getMinimumNumber() { 158 if (minObject == null) { 159 minObject = new Integer (min); 160 } 161 return minObject; 162 } 163 164 169 public long getMinimumLong() { 170 return min; 171 } 172 173 178 public int getMinimumInteger() { 179 return min; 180 } 181 182 187 public double getMinimumDouble() { 188 return min; 189 } 190 191 196 public float getMinimumFloat() { 197 return min; 198 } 199 200 205 public Number getMaximumNumber() { 206 if (maxObject == null) { 207 maxObject = new Integer (max); 208 } 209 return maxObject; 210 } 211 212 217 public long getMaximumLong() { 218 return max; 219 } 220 221 226 public int getMaximumInteger() { 227 return max; 228 } 229 230 235 public double getMaximumDouble() { 236 return max; 237 } 238 239 244 public float getMaximumFloat() { 245 return max; 246 } 247 248 251 260 public boolean containsNumber(Number number) { 261 if (number == null) { 262 return false; 263 } 264 return containsInteger(number.intValue()); 265 } 266 267 278 public boolean containsInteger(int value) { 279 return value >= min && value <= max; 280 } 281 282 285 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 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 324 332 public boolean equals(Object 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 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 365 public String toString() { 366 if (toString == null) { 367 StringBuffer buf = new StringBuffer (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 |