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

Last change on this file since 11137 was 9371, checked in by simon04, 8 years ago

Java 7: use Objects.equals and Objects.hash

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