package geniusweb.issuevalue; import java.math.BigDecimal; import java.math.BigInteger; import java.util.Iterator; import org.eclipse.jdt.annotation.NonNull; 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 { private final @NonNull Iterator it; @SuppressWarnings("null") public NumberValueIterator(@NonNull Range range) { it = range.iterator(); } @Override public boolean hasNext() { return it.hasNext(); } @Override public @NonNull 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 @NonNull Range range; @SuppressWarnings("unused") @JsonCreator public NumberValueSet(@JsonProperty("range") @NonNull Range range) { if (range == null) throw new NullPointerException("Range must not be null"); 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(@NonNull BigDecimal low, @NonNull BigDecimal high, @NonNull BigDecimal stepsize) { if (low == null || high == null || stepsize == null) throw new NullPointerException( "low, high and stepsize must be not null"); if (stepsize.compareTo(BigDecimal.ZERO) <= 0) { throw new IllegalArgumentException( "stepsize must be >0 but is " + stepsize.toString()); } if (low.compareTo(high) > 0) { throw new IllegalArgumentException("low must be <= high"); } range = new Range(low, high, stepsize); } @NonNull /** * @return the range defining this NumberValueSet. */ public Range getRange() { return range; } @Override public @NonNull NumberValue get(@NonNull BigInteger index) { if (index.compareTo(size()) > 0) throw new IndexOutOfBoundsException("" + index); return new NumberValue(range.get(index)); } @Override public @NonNull BigInteger size() { return range.size(); } @Override @NonNull //#PY # BigInteger and long are both int in python. public NumberValue get(long index) { return get(BigInteger.valueOf(index)); } @Override public @NonNull Iterator<@NonNull Value> iterator() { return new NumberValueIterator(range); } @Override public @NonNull String toString() { return "NumberValueSet[" + range.getLow().toString() + "," + range.getHigh().toString() + "," + range.getStep().toString() + "]"; } @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(@NonNull Value value) { if (!(value instanceof NumberValue)) return false; return range.contains(((NumberValue) value).getValue()); } }