source: java2python/geniuswebtranslator/geniuswebsrc/geniusweb/bidspace/Interval.java@ 830

Last change on this file since 830 was 825, checked in by wouter, 6 months ago

#291 move annotation to above the javadoc

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