1 57 58 package org.jfree.data; 59 60 import java.io.Serializable ; 61 62 65 public strictfp class Range implements Serializable { 66 67 68 private static final long serialVersionUID = -906333695431863380L; 69 70 71 private double lower; 72 73 74 private double upper; 75 76 82 public Range(double lower, double upper) { 83 if (lower > upper) { 84 String msg = "Range(double, double): require lower (" + lower 85 + ") <= upper (" + upper + ")."; 86 throw new IllegalArgumentException (msg); 87 } 88 this.lower = lower; 89 this.upper = upper; 90 } 91 92 97 public double getLowerBound() { 98 return this.lower; 99 } 100 101 106 public double getUpperBound() { 107 return this.upper; 108 } 109 110 115 public double getLength() { 116 return this.upper - this.lower; 117 } 118 119 124 public double getCentralValue() { 125 return this.lower / 2.0 + this.upper / 2.0; 126 } 127 128 136 public boolean contains(double value) { 137 return (value >= this.lower && value <= this.upper); 138 } 139 140 149 public boolean intersects(double b0, double b1) { 150 if (b0 <= this.lower) { 151 return (b1 > this.lower); 152 } 153 else { 154 return (b0 < this.upper && b1 >= b0); 155 } 156 } 157 158 166 public double constrain(double value) { 167 double result = value; 168 if (!contains(value)) { 169 if (value > this.upper) { 170 result = this.upper; 171 } 172 else if (value < this.lower) { 173 result = this.lower; 174 } 175 } 176 return result; 177 } 178 179 195 public static Range combine(Range range1, Range range2) { 196 if (range1 == null) { 197 return range2; 198 } 199 else { 200 if (range2 == null) { 201 return range1; 202 } 203 else { 204 double l = Math.min( 205 range1.getLowerBound(), range2.getLowerBound() 206 ); 207 double u = Math.max( 208 range1.getUpperBound(), range2.getUpperBound() 209 ); 210 return new Range(l, u); 211 } 212 } 213 } 214 215 226 public static Range expand(Range range, 227 double lowerMargin, double upperMargin) { 228 if (range == null) { 229 throw new IllegalArgumentException ("Null 'range' argument."); 230 } 231 double length = range.getLength(); 232 double lower = length * lowerMargin; 233 double upper = length * upperMargin; 234 return new Range( 235 range.getLowerBound() - lower, range.getUpperBound() + upper 236 ); 237 } 238 239 247 public static Range shift(Range base, double delta) { 248 return shift(base, delta, false); 249 } 250 251 262 public static Range shift(Range base, double delta, 263 boolean allowZeroCrossing) { 264 if (allowZeroCrossing) { 265 return new Range( 266 base.getLowerBound() + delta, base.getUpperBound() + delta 267 ); 268 } 269 else { 270 return new Range( 271 shiftWithNoZeroCrossing(base.getLowerBound(), delta), 272 shiftWithNoZeroCrossing(base.getUpperBound(), delta) 273 ); 274 } 275 } 276 277 private static double shiftWithNoZeroCrossing(double value, double delta) { 278 if (value > 0.0) { 279 return Math.max(value + delta, 0.0); 280 } 281 else if (value < 0.0) { 282 return Math.min(value + delta, 0.0); 283 } 284 else { 285 return value + delta; 286 } 287 } 288 289 296 public boolean equals(Object obj) { 297 if (!(obj instanceof Range)) { 298 return false; 299 } 300 Range range = (Range) obj; 301 if (!(this.lower == range.lower)) { 302 return false; 303 } 304 if (!(this.upper == range.upper)) { 305 return false; 306 } 307 return true; 308 } 309 310 315 public int hashCode() { 316 int result; 317 long temp; 318 temp = Double.doubleToLongBits(this.lower); 319 result = (int) (temp ^ (temp >>> 32)); 320 temp = Double.doubleToLongBits(this.upper); 321 result = 29 * result + (int) (temp ^ (temp >>> 32)); 322 return result; 323 } 324 325 331 public String toString() { 332 return ("Range[" + this.lower + "," + this.upper + "]"); 333 } 334 335 } 336 | Popular Tags |