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

Last change on this file since 694 was 692, checked in by wouter, 13 months ago

#239 fix call ambiguity: ExpressionLine implements Equals and use Set to collect candidates.

File size: 3.3 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() + "," + range.getHigh() + ","
106 + range.getStep() + "]";
107 }
108
109 @Override
110 public int hashCode() {
111 final int prime = 31;
112 int result = 1;
113 result = prime * result + ((range == null) ? 0 : range.hashCode());
114 return result;
115 }
116
117 @Override
118 public boolean equals(Object obj) {
119 if (this == obj)
120 return true;
121 if (obj == null)
122 return false;
123 if (getClass() != obj.getClass())
124 return false;
125 NumberValueSet other = (NumberValueSet) obj;
126 if (range == null) {
127 if (other.range != null)
128 return false;
129 } else if (!range.equals(other.range))
130 return false;
131 return true;
132 }
133
134 @Override
135 public Boolean contains(Value value) {
136 if (!(value instanceof NumberValue))
137 return false;
138 return range.contains(((NumberValue) value).getValue());
139 }
140}
Note: See TracBrowser for help on using the repository browser.