[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...
|
---|
[542] | 20 | //#PY @JsonDeserialize(using=None)
|
---|
[519] | 21 | @JsonDeserialize(using = JsonDeserializer.None.class)
|
---|
| 22 | @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
|
---|
| 23 | public class NumberValueSet implements ValueSet {
|
---|
| 24 |
|
---|
| 25 | private final Range range;
|
---|
| 26 |
|
---|
| 27 | @JsonCreator
|
---|
| 28 | public NumberValueSet(@JsonProperty("range") Range range) {
|
---|
| 29 | this.range = range;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | /**
|
---|
| 33 | * number range from low to high with given step size
|
---|
| 34 | *
|
---|
| 35 | * @param low the lowest value in the list. Must be ≤ high
|
---|
| 36 | * @param high the highest value in the list
|
---|
| 37 | * @param stepsize the step size. Must be > 0
|
---|
| 38 | */
|
---|
| 39 | public NumberValueSet(BigDecimal low, BigDecimal high,
|
---|
| 40 | BigDecimal stepsize) {
|
---|
| 41 | if (stepsize.compareTo(BigDecimal.ZERO) <= 0) {
|
---|
| 42 | throw new IllegalArgumentException(
|
---|
| 43 | "stepsize must be >0 but is " + stepsize);
|
---|
| 44 | }
|
---|
| 45 | if (low.compareTo(high) > 0) {
|
---|
| 46 | throw new IllegalArgumentException("low must be <= high");
|
---|
| 47 | }
|
---|
| 48 | range = new Range(low, high, stepsize);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | /**
|
---|
| 52 | * @return the range defining this NumberValueSet.
|
---|
| 53 | */
|
---|
| 54 | public Range getRange() {
|
---|
| 55 | return range;
|
---|
| 56 |
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | @Override
|
---|
| 60 | public NumberValue get(BigInteger index) {
|
---|
| 61 | if (index.compareTo(size()) > 0) {
|
---|
| 62 | return null;
|
---|
| 63 | }
|
---|
| 64 | return new NumberValue(range.get(index));
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | @Override
|
---|
| 68 | public BigInteger size() {
|
---|
| 69 | return range.size();
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | @Override
|
---|
| 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 | }
|
---|