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

Last change on this file since 829 was 825, checked in by wouter, 6 months ago

#291 move annotation to above the javadoc

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