1 16 package org.apache.commons.lang.math; 17 18 import java.io.Serializable ; 19 20 27 public final class LongRange extends Range implements Serializable { 28 29 private static final long serialVersionUID = 71849363892720L; 30 31 34 private final long min; 35 38 private final long max; 39 40 43 private transient Long minObject = null; 44 47 private transient Long maxObject = null; 48 51 private transient int hashCode = 0; 52 55 private transient String toString = null; 56 57 63 public LongRange(long number) { 64 super(); 65 this.min = number; 66 this.max = number; 67 } 68 69 77 public LongRange(Number number) { 78 super(); 79 if (number == null) { 80 throw new IllegalArgumentException ("The number must not be null"); 81 } 82 this.min = number.longValue(); 83 this.max = number.longValue(); 84 if (number instanceof Long ) { 85 this.minObject = (Long ) number; 86 this.maxObject = (Long ) number; 87 } 88 } 89 90 100 public LongRange(long number1, long number2) { 101 super(); 102 if (number2 < number1) { 103 this.min = number2; 104 this.max = number1; 105 } else { 106 this.min = number1; 107 this.max = number2; 108 } 109 } 110 111 122 public LongRange(Number number1, Number number2) { 123 super(); 124 if (number1 == null || number2 == null) { 125 throw new IllegalArgumentException ("The numbers must not be null"); 126 } 127 long number1val = number1.longValue(); 128 long number2val = number2.longValue(); 129 if (number2val < number1val) { 130 this.min = number2val; 131 this.max = number1val; 132 if (number2 instanceof Long ) { 133 this.minObject = (Long ) number2; 134 } 135 if (number1 instanceof Long ) { 136 this.maxObject = (Long ) number1; 137 } 138 } else { 139 this.min = number1val; 140 this.max = number2val; 141 if (number1 instanceof Long ) { 142 this.minObject = (Long ) number1; 143 } 144 if (number2 instanceof Long ) { 145 this.maxObject = (Long ) number2; 146 } 147 } 148 } 149 150 153 158 public Number getMinimumNumber() { 159 if (minObject == null) { 160 minObject = new Long (min); 161 } 162 return minObject; 163 } 164 165 170 public long getMinimumLong() { 171 return min; 172 } 173 174 181 public int getMinimumInteger() { 182 return (int) min; 183 } 184 185 192 public double getMinimumDouble() { 193 return min; 194 } 195 196 203 public float getMinimumFloat() { 204 return min; 205 } 206 207 212 public Number getMaximumNumber() { 213 if (maxObject == null) { 214 maxObject = new Long (max); 215 } 216 return maxObject; 217 } 218 219 224 public long getMaximumLong() { 225 return max; 226 } 227 228 235 public int getMaximumInteger() { 236 return (int) max; 237 } 238 239 246 public double getMaximumDouble() { 247 return max; 248 } 249 250 257 public float getMaximumFloat() { 258 return max; 259 } 260 261 264 273 public boolean containsNumber(Number number) { 274 if (number == null) { 275 return false; 276 } 277 return containsLong(number.longValue()); 278 } 279 280 291 public boolean containsLong(long value) { 292 return value >= min && value <= max; 293 } 294 295 298 308 public boolean containsRange(Range range) { 309 if (range == null) { 310 return false; 311 } 312 return containsLong(range.getMinimumLong()) && 313 containsLong(range.getMaximumLong()); 314 } 315 316 325 public boolean overlapsRange(Range range) { 326 if (range == null) { 327 return false; 328 } 329 return range.containsLong(min) || 330 range.containsLong(max) || 331 containsLong(range.getMinimumLong()); 332 } 333 334 337 345 public boolean equals(Object obj) { 346 if (obj == this) { 347 return true; 348 } 349 if (obj instanceof LongRange == false) { 350 return false; 351 } 352 LongRange range = (LongRange) obj; 353 return min == range.min && max == range.max; 354 } 355 356 361 public int hashCode() { 362 if (hashCode == 0) { 363 hashCode = 17; 364 hashCode = 37 * hashCode + getClass().hashCode(); 365 hashCode = 37 * hashCode + ((int) (min ^ (min >> 32))); 366 hashCode = 37 * hashCode + ((int) (max ^ (max >> 32))); 367 } 368 return hashCode; 369 } 370 371 378 public String toString() { 379 if (toString == null) { 380 StringBuffer buf = new StringBuffer (32); 381 buf.append("Range["); 382 buf.append(min); 383 buf.append(','); 384 buf.append(max); 385 buf.append(']'); 386 toString = buf.toString(); 387 } 388 return toString; 389 } 390 391 } 392 | Popular Tags |