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