source: java2python/geniuswebtranslator/geniuswebsrc/geniusweb/issuevalue/NumberValueSet.java@ 744

Last change on this file since 744 was 734, checked in by wouter, 11 months ago

work around auto-boxing issues

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