1 | package geniusweb.bidspace;
|
---|
2 |
|
---|
3 | import java.math.BigDecimal;
|
---|
4 | import java.math.RoundingMode;
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * An interval [min, max].
|
---|
8 | *
|
---|
9 | */
|
---|
10 | public class Interval {
|
---|
11 | public static Interval ZERO = new Interval(BigDecimal.ZERO,
|
---|
12 | BigDecimal.ZERO);
|
---|
13 |
|
---|
14 | private final BigDecimal min;
|
---|
15 | private final BigDecimal max;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * AN interval [min,max]. If min > max, then there are no values in this
|
---|
19 | * interval.
|
---|
20 | *
|
---|
21 | * @param min the minimum value of the interval
|
---|
22 | * @param max the maximum value of the interval
|
---|
23 | */
|
---|
24 | public Interval(BigDecimal min, BigDecimal max) {
|
---|
25 | if (min == null || max == null) {
|
---|
26 | throw new NullPointerException("min and max must contain not null");
|
---|
27 | }
|
---|
28 | this.min = min;
|
---|
29 | this.max = max;
|
---|
30 | }
|
---|
31 |
|
---|
32 | public BigDecimal getMin() {
|
---|
33 | return min;
|
---|
34 | }
|
---|
35 |
|
---|
36 | public BigDecimal getMax() {
|
---|
37 | return max;
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | *
|
---|
42 | * @return true iff this range does not contain any element.
|
---|
43 | */
|
---|
44 | public boolean isEmpty() {
|
---|
45 | return min.compareTo(max) > 0;
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | *
|
---|
50 | * @param value the value to test
|
---|
51 | * @return true iff min ≤ value ≤ max
|
---|
52 | */
|
---|
53 | public boolean contains(BigDecimal value) {
|
---|
54 | return min.compareTo(value) <= 0 && max.compareTo(value) >= 0;
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | *
|
---|
59 | * @param other {@link Interval} to be added to this
|
---|
60 | * @return new interval [ this.min + other.min , this.max + other.max ]
|
---|
61 | */
|
---|
62 | public Interval add(Interval other) {
|
---|
63 | return new Interval(min.add(other.min), max.add(other.max));
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | *
|
---|
68 | * @param other another {@link Interval} intersect with
|
---|
69 | * @return intersection of this with other. returns null if intersection is
|
---|
70 | * empty.
|
---|
71 | */
|
---|
72 | public Interval intersect(Interval other) {
|
---|
73 | return new Interval(min.max(other.min), max.min(other.max));
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | *
|
---|
78 | * @param other the other minmax to deal with
|
---|
79 | * @return the range of values that, when added to a value from other, will
|
---|
80 | * possibly get in our range. effectively, [min-other.max,
|
---|
81 | * max-other.min]. Returns null if the resulting range is empty.
|
---|
82 | */
|
---|
83 | public Interval invert(Interval other) {
|
---|
84 | BigDecimal newmin = min.subtract(other.max);
|
---|
85 | BigDecimal newmax = max.subtract(other.min);
|
---|
86 | if (newmin.compareTo(newmax) > 0)
|
---|
87 | return null;
|
---|
88 | return new Interval(newmin, newmax);
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | *
|
---|
93 | * @param value the value to subtract
|
---|
94 | * @return Interval with both min and max reduced by value.
|
---|
95 | */
|
---|
96 | public Interval subtract(BigDecimal value) {
|
---|
97 | return new Interval(min.subtract(value), max.subtract(value));
|
---|
98 | }
|
---|
99 |
|
---|
100 | public Interval multiply(BigDecimal weight) {
|
---|
101 | return new Interval(min.multiply(weight), max.multiply(weight));
|
---|
102 | }
|
---|
103 |
|
---|
104 | @Override
|
---|
105 | public String toString() {
|
---|
106 | return "Interval[" + min + "," + max + "]";
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * @param precision number of digits required
|
---|
111 | * @return this but with modified precision. The interval is rounded so that
|
---|
112 | * the new interval is inside the old one.
|
---|
113 | */
|
---|
114 | public Interval round(int precision) {
|
---|
115 | return new Interval(min.setScale(precision, RoundingMode.UP),
|
---|
116 | max.setScale(precision, RoundingMode.DOWN));
|
---|
117 | }
|
---|
118 |
|
---|
119 | @Override
|
---|
120 | public int hashCode() {
|
---|
121 | final int prime = 31;
|
---|
122 | int result = 1;
|
---|
123 | result = prime * result + ((max == null) ? 0 : max.hashCode());
|
---|
124 | result = prime * result + ((min == null) ? 0 : min.hashCode());
|
---|
125 | return result;
|
---|
126 | }
|
---|
127 |
|
---|
128 | @Override
|
---|
129 | public boolean equals(Object obj) {
|
---|
130 | if (this == obj)
|
---|
131 | return true;
|
---|
132 | if (obj == null)
|
---|
133 | return false;
|
---|
134 | if (getClass() != obj.getClass())
|
---|
135 | return false;
|
---|
136 | Interval other = (Interval) obj;
|
---|
137 | if (max == null) {
|
---|
138 | if (other.max != null)
|
---|
139 | return false;
|
---|
140 |
|
---|
141 | } else if (max.compareTo(other.max) != 0)
|
---|
142 | // THIS WAS FIXED MANUALLY TO USE COMPARETO
|
---|
143 | return false;
|
---|
144 | if (min == null) {
|
---|
145 | if (other.min != null)
|
---|
146 | return false;
|
---|
147 | } else if (min.compareTo(other.min) != 0)
|
---|
148 | // THIS WAS FIXED MANUALLY TO USE COMPARETO
|
---|
149 | return false;
|
---|
150 | return true;
|
---|
151 | }
|
---|
152 |
|
---|
153 | }
|
---|