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

Last change on this file since 13917 was 12378, checked in by michael2402, 7 years ago

Document the gui.mappaint package

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