[519] | 1 | package geniusweb.issuevalue;
|
---|
| 2 |
|
---|
| 3 | import java.math.BigDecimal;
|
---|
| 4 | import java.math.BigInteger;
|
---|
| 5 | import java.util.Iterator;
|
---|
| 6 |
|
---|
[804] | 7 | import org.eclipse.jdt.annotation.NonNull;
|
---|
| 8 |
|
---|
[519] | 9 | import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
---|
| 10 | import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
---|
| 11 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
| 12 | import com.fasterxml.jackson.annotation.JsonProperty;
|
---|
| 13 | import com.fasterxml.jackson.databind.JsonDeserializer;
|
---|
| 14 | import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
---|
| 15 |
|
---|
| 16 | import tudelft.utilities.immutablelist.Range;
|
---|
| 17 |
|
---|
| 18 | /**
|
---|
[692] | 19 | * Internal iterator for NumberValues
|
---|
| 20 | */
|
---|
[805] | 21 | class NumberValueIterator implements Iterator<Value> {
|
---|
[804] | 22 | private final @NonNull Iterator<BigDecimal> it;
|
---|
[692] | 23 |
|
---|
[804] | 24 | @SuppressWarnings("null")
|
---|
| 25 | public NumberValueIterator(@NonNull Range range) {
|
---|
[692] | 26 | it = range.iterator();
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | @Override
|
---|
| 30 | public boolean hasNext() {
|
---|
| 31 | return it.hasNext();
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | @Override
|
---|
[804] | 35 | public @NonNull NumberValue next() {
|
---|
[692] | 36 | return new NumberValue(it.next());
|
---|
| 37 | }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | /**
|
---|
[519] | 41 | * number range from low to high with given step size
|
---|
| 42 | */
|
---|
| 43 | // disable the inherited deserializer to prevent getting into infinite loop...
|
---|
| 44 | @JsonDeserialize(using = JsonDeserializer.None.class)
|
---|
| 45 | @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
|
---|
| 46 | public class NumberValueSet implements ValueSet {
|
---|
| 47 |
|
---|
[804] | 48 | private final @NonNull Range range;
|
---|
[519] | 49 |
|
---|
[804] | 50 | @SuppressWarnings("unused")
|
---|
[519] | 51 | @JsonCreator
|
---|
[804] | 52 | public NumberValueSet(@JsonProperty("range") @NonNull Range range) {
|
---|
| 53 | if (range == null)
|
---|
| 54 | throw new NullPointerException("Range must not be null");
|
---|
[519] | 55 | this.range = range;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | /**
|
---|
| 59 | * number range from low to high with given step size
|
---|
| 60 | *
|
---|
| 61 | * @param low the lowest value in the list. Must be ≤ high
|
---|
| 62 | * @param high the highest value in the list
|
---|
| 63 | * @param stepsize the step size. Must be > 0
|
---|
| 64 | */
|
---|
[804] | 65 | public NumberValueSet(@NonNull BigDecimal low, @NonNull BigDecimal high,
|
---|
| 66 | @NonNull BigDecimal stepsize) {
|
---|
| 67 | if (low == null || high == null || stepsize == null)
|
---|
| 68 | throw new NullPointerException(
|
---|
| 69 | "low, high and stepsize must be not null");
|
---|
[519] | 70 | if (stepsize.compareTo(BigDecimal.ZERO) <= 0) {
|
---|
| 71 | throw new IllegalArgumentException(
|
---|
| 72 | "stepsize must be >0 but is " + stepsize);
|
---|
| 73 | }
|
---|
| 74 | if (low.compareTo(high) > 0) {
|
---|
| 75 | throw new IllegalArgumentException("low must be <= high");
|
---|
| 76 | }
|
---|
| 77 | range = new Range(low, high, stepsize);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | /**
|
---|
| 81 | * @return the range defining this NumberValueSet.
|
---|
| 82 | */
|
---|
[804] | 83 | public @NonNull Range getRange() {
|
---|
[519] | 84 | return range;
|
---|
| 85 |
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | @Override
|
---|
[804] | 89 | public @NonNull NumberValue get(@NonNull BigInteger index) {
|
---|
| 90 | if (index.compareTo(size()) > 0)
|
---|
| 91 | throw new IndexOutOfBoundsException("" + index);
|
---|
[519] | 92 | return new NumberValue(range.get(index));
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | @Override
|
---|
[804] | 96 | public @NonNull BigInteger size() {
|
---|
[519] | 97 | return range.size();
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | @Override
|
---|
[584] | 101 | //#PY # not needed, BigInteger and long are both int in python.
|
---|
[804] | 102 | public @NonNull NumberValue get(long index) {
|
---|
[519] | 103 | return get(BigInteger.valueOf(index));
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | @Override
|
---|
[810] | 107 | public @NonNull Iterator<@NonNull Value> iterator() {
|
---|
[692] | 108 | return new NumberValueIterator(range);
|
---|
[519] | 109 | }
|
---|
| 110 |
|
---|
| 111 | @Override
|
---|
[805] | 112 | public @NonNull String toString() {
|
---|
[734] | 113 | return "numberValueSet[" + range.getLow().toString() + ","
|
---|
| 114 | + range.getHigh().toString() + "," + range.getStep().toString()
|
---|
| 115 | + "]";
|
---|
[519] | 116 | }
|
---|
| 117 |
|
---|
| 118 | @Override
|
---|
| 119 | public int hashCode() {
|
---|
| 120 | final int prime = 31;
|
---|
| 121 | int result = 1;
|
---|
| 122 | result = prime * result + ((range == null) ? 0 : range.hashCode());
|
---|
| 123 | return result;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | @Override
|
---|
| 127 | public boolean equals(Object obj) {
|
---|
| 128 | if (this == obj)
|
---|
| 129 | return true;
|
---|
| 130 | if (obj == null)
|
---|
| 131 | return false;
|
---|
| 132 | if (getClass() != obj.getClass())
|
---|
| 133 | return false;
|
---|
| 134 | NumberValueSet other = (NumberValueSet) obj;
|
---|
| 135 | if (range == null) {
|
---|
| 136 | if (other.range != null)
|
---|
| 137 | return false;
|
---|
| 138 | } else if (!range.equals(other.range))
|
---|
| 139 | return false;
|
---|
| 140 | return true;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | @Override
|
---|
[805] | 144 | public boolean contains(@NonNull Value value) {
|
---|
[519] | 145 | if (!(value instanceof NumberValue))
|
---|
| 146 | return false;
|
---|
| 147 | return range.contains(((NumberValue) value).getValue());
|
---|
| 148 | }
|
---|
| 149 | }
|
---|