package geniusweb.issuevalue; import java.math.BigInteger; import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.List; 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.ArrayListSet; /** * set of discrete values */ // 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 DiscreteValueSet implements ValueSet { /* * final, must be immutable so do not provide external access to this list. * ArrayList (instead of List) to help Jackson serializer which avoids * per-field type annotations. */ private final @NonNull ArrayListSet<@NonNull DiscreteValue> values = new ArrayListSet(); @JsonCreator public DiscreteValueSet( @JsonProperty("values") @NonNull Collection<@NonNull DiscreteValue> values) { if (values.contains(null)) throw new NullPointerException("values should not contain null"); this.values.addAll(values); } //abandoned, because we can not overload constructor like this in Python // public DiscreteValueSet(DiscreteValue... issuevalues) { // for (DiscreteValue value : issuevalues) { // this.values.add(value); // } // } public @NonNull List<@NonNull DiscreteValue> getValues() { return Collections.unmodifiableList(values); } @Override public boolean contains(@NonNull Value value) { return values.contains(value); } @Override public @NonNull DiscreteValue get(@NonNull BigInteger index) { return values.get(index.intValue()); } @Override public @NonNull BigInteger size() { return BigInteger.valueOf(values.size()); } @Override public @NonNull DiscreteValue get(long index) { return values.get((int) index); } @Override public @NonNull Iterator iterator() { return values.iterator(); } @Override public @NonNull String toString() { return "DiscreteValueSet" + values.toString(); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((values == null) ? 0 : values.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; DiscreteValueSet other = (DiscreteValueSet) obj; if (values == null) { if (other.values != null) return false; } else if (!values.equals(other.values)) return false; return true; } }