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