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

Last change on this file since 817 was 814, checked in by wouter, 6 months ago

#278 more NonNull annotations

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 /**
37 *
38 * @return the minimum value of the interval
39 */
40 public @NonNull BigDecimal getMin() {
41 return min;
42 }
43
44 /**
45 *
46 * @return the maximum value of the interval
47 */
48 public @NonNull BigDecimal getMax() {
49 return max;
50 }
51
52 /**
53 *
54 * @return true iff this range does not contain any element.
55 */
56 public boolean isEmpty() {
57 return min.compareTo(max) > 0;
58 }
59
60 /**
61 *
62 * @param value the value to test
63 * @return true iff min ≤ value ≤ max
64 */
65 public boolean contains(@NonNull BigDecimal value) {
66 return min.compareTo(value) <= 0 && max.compareTo(value) >= 0;
67 }
68
69 /**
70 *
71 * @param other {@link Interval} to be added to this
72 * @return new interval [ this.min + other.min , this.max + other.max ]
73 */
74 public @NonNull Interval add(@NonNull Interval other) {
75 return new Interval(min.add(other.min), max.add(other.max));
76 }
77
78 /**
79 *
80 * @param other another {@link Interval} intersect with
81 * @return intersection of this with other. returns null if intersection is
82 * empty.
83 */
84 public @NonNull Interval intersect(@NonNull Interval other) {
85 return new Interval(min.max(other.min), max.min(other.max));
86 }
87
88 /**
89 *
90 * @param other the other minmax to deal with
91 * @return the range of values that, when added to a value from other, will
92 * possibly get in our range. effectively, [min-other.max,
93 * max-other.min]. Returns {@link #EMPTY} if the resulting range is
94 * empty.
95 */
96 public @NonNull Interval invert(@NonNull Interval other) {
97 final @NonNull BigDecimal newmin = min.subtract(other.max);
98 final @NonNull BigDecimal newmax = max.subtract(other.min);
99 if (newmin.compareTo(newmax) > 0)
100 return EMPTY;
101 return new Interval(newmin, newmax);
102 }
103
104 /**
105 *
106 * @param value the value to subtract
107 * @return Interval with both min and max reduced by value.
108 */
109 public @NonNull Interval subtract(@NonNull BigDecimal value) {
110 return new Interval(min.subtract(value), max.subtract(value));
111 }
112
113 public @NonNull Interval multiply(@NonNull BigDecimal weight) {
114 return new Interval(min.multiply(weight), max.multiply(weight));
115 }
116
117 @Override
118 public @NonNull String toString() {
119 return "Interval[" + min + "," + max + "]";
120 }
121
122 /**
123 * @param precision number of digits required
124 * @return this but with modified precision. The interval is rounded so that
125 * the new interval is inside the old one.
126 */
127 public @NonNull Interval round(int precision) {
128 return new Interval(min.setScale(precision, RoundingMode.UP),
129 max.setScale(precision, RoundingMode.DOWN));
130 }
131
132 @Override
133 public int hashCode() {
134 final int prime = 31;
135 int result = 1;
136 result = prime * result + ((max == null) ? 0 : max.hashCode());
137 result = prime * result + ((min == null) ? 0 : min.hashCode());
138 return result;
139 }
140
141 @Override
142 public boolean equals(Object obj) {
143 if (this == obj)
144 return true;
145 if (obj == null)
146 return false;
147 if (getClass() != obj.getClass())
148 return false;
149 Interval other = (Interval) obj;
150 if (max == null) {
151 if (other.max != null)
152 return false;
153
154 } else if (max.compareTo(other.max) != 0)
155 // THIS WAS FIXED MANUALLY TO USE COMPARETO
156 return false;
157 if (min == null) {
158 if (other.min != null)
159 return false;
160 } else if (min.compareTo(other.min) != 0)
161 // THIS WAS FIXED MANUALLY TO USE COMPARETO
162 return false;
163 return true;
164 }
165
166}
Note: See TracBrowser for help on using the repository browser.