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