[519] | 1 | package geniusweb.bidspace;
|
---|
| 2 |
|
---|
| 3 | import java.math.BigDecimal;
|
---|
| 4 | import java.math.RoundingMode;
|
---|
| 5 |
|
---|
[810] | 6 | import org.eclipse.jdt.annotation.NonNull;
|
---|
| 7 |
|
---|
[519] | 8 | /**
|
---|
| 9 | * An interval [min, max].
|
---|
| 10 | *
|
---|
| 11 | */
|
---|
| 12 | public class Interval {
|
---|
[814] | 13 | public static @NonNull Interval ZERO = new Interval(BigDecimal.ZERO,
|
---|
[519] | 14 | BigDecimal.ZERO);
|
---|
[814] | 15 | public static @NonNull Interval EMPTY = new Interval(BigDecimal.ONE,
|
---|
[810] | 16 | BigDecimal.ZERO);
|
---|
[519] | 17 |
|
---|
[810] | 18 | private final @NonNull BigDecimal min;
|
---|
| 19 | private final @NonNull BigDecimal max;
|
---|
[519] | 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 | */
|
---|
[810] | 28 | public Interval(@NonNull BigDecimal min, @NonNull BigDecimal max) {
|
---|
[519] | 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 |
|
---|
[825] | 36 | @NonNull
|
---|
[519] | 37 | /**
|
---|
| 38 | *
|
---|
| 39 | * @return the minimum value of the interval
|
---|
| 40 | */
|
---|
[825] | 41 | public BigDecimal getMin() {
|
---|
[519] | 42 | return min;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[825] | 45 | @NonNull
|
---|
[519] | 46 | /**
|
---|
| 47 | *
|
---|
| 48 | * @return the maximum value of the interval
|
---|
| 49 | */
|
---|
[825] | 50 | public BigDecimal getMax() {
|
---|
[519] | 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 | */
|
---|
[810] | 67 | public boolean contains(@NonNull BigDecimal value) {
|
---|
[519] | 68 | return min.compareTo(value) <= 0 && max.compareTo(value) >= 0;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[825] | 71 | @NonNull
|
---|
[519] | 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 | */
|
---|
[825] | 77 | public Interval add(@NonNull Interval other) {
|
---|
[519] | 78 | return new Interval(min.add(other.min), max.add(other.max));
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[825] | 81 | @NonNull
|
---|
[519] | 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 | */
|
---|
[825] | 88 | public Interval intersect(@NonNull Interval other) {
|
---|
[519] | 89 | return new Interval(min.max(other.min), max.min(other.max));
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[825] | 92 | @NonNull
|
---|
[519] | 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,
|
---|
[810] | 98 | * max-other.min]. Returns {@link #EMPTY} if the resulting range is
|
---|
| 99 | * empty.
|
---|
[519] | 100 | */
|
---|
[825] | 101 | public Interval invert(@NonNull Interval other) {
|
---|
[810] | 102 | final @NonNull BigDecimal newmin = min.subtract(other.max);
|
---|
| 103 | final @NonNull BigDecimal newmax = max.subtract(other.min);
|
---|
[519] | 104 | if (newmin.compareTo(newmax) > 0)
|
---|
[810] | 105 | return EMPTY;
|
---|
[519] | 106 | return new Interval(newmin, newmax);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[825] | 109 | @NonNull
|
---|
[519] | 110 | /**
|
---|
| 111 | *
|
---|
| 112 | * @param value the value to subtract
|
---|
| 113 | * @return Interval with both min and max reduced by value.
|
---|
| 114 | */
|
---|
[825] | 115 | public Interval subtract(@NonNull BigDecimal value) {
|
---|
[519] | 116 | return new Interval(min.subtract(value), max.subtract(value));
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[810] | 119 | public @NonNull Interval multiply(@NonNull BigDecimal weight) {
|
---|
[519] | 120 | return new Interval(min.multiply(weight), max.multiply(weight));
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | @Override
|
---|
[810] | 124 | public @NonNull String toString() {
|
---|
[519] | 125 | return "Interval[" + min + "," + max + "]";
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[825] | 128 | @NonNull
|
---|
[519] | 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 | */
|
---|
[825] | 134 | public Interval round(int precision) {
|
---|
[519] | 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 | }
|
---|