[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
|
---|
| 72 | public NumberValue get(long index) {
|
---|
| 73 | return get(BigInteger.valueOf(index));
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | @Override
|
---|
| 77 | public Iterator iterator() {
|
---|
| 78 | return new Iterator<NumberValue>() {
|
---|
| 79 | Iterator<BigDecimal> it = range.iterator();
|
---|
| 80 |
|
---|
| 81 | @Override
|
---|
| 82 | public boolean hasNext() {
|
---|
| 83 | return it.hasNext();
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | @Override
|
---|
| 87 | public NumberValue next() {
|
---|
| 88 | return new NumberValue(it.next());
|
---|
| 89 | }
|
---|
| 90 | };
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | @Override
|
---|
| 94 | public String toString() {
|
---|
| 95 | return "numberValueSet[" + range.getLow() + "," + range.getHigh() + ","
|
---|
| 96 | + range.getStep() + "]";
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | @Override
|
---|
| 100 | public int hashCode() {
|
---|
| 101 | final int prime = 31;
|
---|
| 102 | int result = 1;
|
---|
| 103 | result = prime * result + ((range == null) ? 0 : range.hashCode());
|
---|
| 104 | return result;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | @Override
|
---|
| 108 | public boolean equals(Object obj) {
|
---|
| 109 | if (this == obj)
|
---|
| 110 | return true;
|
---|
| 111 | if (obj == null)
|
---|
| 112 | return false;
|
---|
| 113 | if (getClass() != obj.getClass())
|
---|
| 114 | return false;
|
---|
| 115 | NumberValueSet other = (NumberValueSet) obj;
|
---|
| 116 | if (range == null) {
|
---|
| 117 | if (other.range != null)
|
---|
| 118 | return false;
|
---|
| 119 | } else if (!range.equals(other.range))
|
---|
| 120 | return false;
|
---|
| 121 | return true;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | @Override
|
---|
| 125 | public Boolean contains(Value value) {
|
---|
| 126 | if (!(value instanceof NumberValue))
|
---|
| 127 | return false;
|
---|
| 128 | return range.contains(((NumberValue) value).getValue());
|
---|
| 129 | }
|
---|
| 130 | }
|
---|