package geniusweb.issuevalue; import java.math.BigDecimal; import java.math.BigInteger; import java.util.Iterator; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import tudelft.utilities.immutablelist.Range; /** * Internal iterator for NumberValues */ class NumberValueIterator implements Iterator { Iterator it; public NumberValueIterator(Range range) { it = range.iterator(); } @Override public boolean hasNext() { return it.hasNext(); } @Override public NumberValue next() { return new NumberValue(it.next()); } } /** * number range from low to high with given step size */ // disable the inherited deserializer to prevent getting into infinite loop... @JsonDeserialize(using = JsonDeserializer.None.class) @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE) public class NumberValueSet implements ValueSet { private final Range range; @JsonCreator public NumberValueSet(@JsonProperty("range") Range range) { this.range = range; } /** * number range from low to high with given step size * * @param low the lowest value in the list. Must be ≤ high * @param high the highest value in the list * @param stepsize the step size. Must be > 0 */ public NumberValueSet(BigDecimal low, BigDecimal high, BigDecimal stepsize) { if (stepsize.compareTo(BigDecimal.ZERO) <= 0) { throw new IllegalArgumentException( "stepsize must be >0 but is " + stepsize); } if (low.compareTo(high) > 0) { throw new IllegalArgumentException("low must be <= high"); } range = new Range(low, high, stepsize); } /** * @return the range defining this NumberValueSet. */ public Range getRange() { return range; } @Override public NumberValue get(BigInteger index) { if (index.compareTo(size()) > 0) { return null; } return new NumberValue(range.get(index)); } @Override public BigInteger size() { return range.size(); } @Override //#PY # not needed, BigInteger and long are both int in python. public NumberValue get(long index) { return get(BigInteger.valueOf(index)); } @Override public Iterator iterator() { return new NumberValueIterator(range); } @Override public String toString() { return "numberValueSet[" + range.getLow() + "," + range.getHigh() + "," + range.getStep() + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((range == null) ? 0 : range.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; NumberValueSet other = (NumberValueSet) obj; if (range == null) { if (other.range != null) return false; } else if (!range.equals(other.range)) return false; return true; } @Override public Boolean contains(Value value) { if (!(value instanceof NumberValue)) return false; return range.contains(((NumberValue) value).getValue()); } }