source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/Range.java@ 6986

Last change on this file since 6986 was 6986, checked in by Don-vip, 10 years ago

sonar - fix various minor issues

  • Property svn:eol-style set to native
File size: 2.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint;
3
4/**
5 * An interval of the form "lower < x <= upper" where 0 <= lower < upper.
6 * (upper can be Double.POSITIVE_INFINITY)
7 * immutable class
8 */
9public class Range {
10 private double lower;
11 private double upper;
12
13 public static final Range ZERO_TO_INFINITY = new Range(0.0, Double.POSITIVE_INFINITY);
14
15 public Range(double lower, double upper) {
16 if (lower < 0 || lower >= upper)
17 throw new IllegalArgumentException();
18 this.lower = lower;
19 this.upper = upper;
20 }
21
22 public boolean contains(double x) {
23 return lower < x && x <= upper;
24 }
25
26 /**
27 * provides the intersection of 2 overlapping ranges
28 */
29 public static Range cut(Range a, Range b) {
30 if (b.lower >= a.upper || b.upper <= a.lower)
31 throw new IllegalArgumentException();
32 return new Range(Math.max(a.lower, b.lower), Math.min(a.upper, b.upper));
33 }
34
35 /**
36 * under the premise, that x is within this range,
37 * and not within the other range, it shrinks this range in a way
38 * to exclude the other range, but still contain x.
39 *
40 * x |
41 *
42 * this (------------------------------]
43 *
44 * other (-------] or
45 * (-----------------]
46 *
47 * result (----------------]
48 */
49 public Range reduceAround(double x, Range other) {
50 if (!contains(x))
51 throw new IllegalArgumentException();
52 if (other.contains(x))
53 throw new IllegalArgumentException();
54
55 if (x < other.lower && other.lower < upper)
56 return new Range(lower, other.lower);
57
58 if (this.lower < other.upper && other.upper < x)
59 return new Range(other.upper, this.upper);
60
61 return this;
62 }
63
64 public double getLower() {
65 return lower;
66 }
67
68 public double getUpper() {
69 return upper;
70 }
71
72 @Override
73 public String toString() {
74 return String.format("|s%s-%s", lower, upper);
75 }
76
77 @Override
78 public boolean equals(Object o) {
79 if (this == o) return true;
80 if (o == null || getClass() != o.getClass()) return false;
81
82 Range range = (Range) o;
83
84 if (Double.compare(range.lower, lower) != 0) return false;
85 if (Double.compare(range.upper, upper) != 0) return false;
86
87 return true;
88 }
89
90 @Override
91 public int hashCode() {
92 int result;
93 long temp;
94 temp = Double.doubleToLongBits(lower);
95 result = (int) (temp ^ (temp >>> 32));
96 temp = Double.doubleToLongBits(upper);
97 result = 31 * result + (int) (temp ^ (temp >>> 32));
98 return result;
99 }
100}
Note: See TracBrowser for help on using the repository browser.