[519] | 1 | package geniusweb.issuevalue;
|
---|
| 2 |
|
---|
| 3 | import java.math.BigInteger;
|
---|
| 4 | import java.util.Collection;
|
---|
| 5 | import java.util.Collections;
|
---|
| 6 | import java.util.Iterator;
|
---|
| 7 | import java.util.List;
|
---|
| 8 |
|
---|
[804] | 9 | import org.eclipse.jdt.annotation.NonNull;
|
---|
| 10 |
|
---|
[519] | 11 | import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
---|
| 12 | import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
---|
| 13 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
| 14 | import com.fasterxml.jackson.annotation.JsonProperty;
|
---|
| 15 | import com.fasterxml.jackson.databind.JsonDeserializer;
|
---|
| 16 | import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
---|
| 17 |
|
---|
| 18 | import tudelft.utilities.immutablelist.ArrayListSet;
|
---|
| 19 |
|
---|
| 20 | /**
|
---|
| 21 | * set of discrete values
|
---|
| 22 | */
|
---|
| 23 | // disable the inherited deserializer to prevent getting into infinite loop...
|
---|
| 24 | @JsonDeserialize(using = JsonDeserializer.None.class)
|
---|
| 25 | @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
|
---|
| 26 | public class DiscreteValueSet implements ValueSet {
|
---|
| 27 | /*
|
---|
| 28 | * final, must be immutable so do not provide external access to this list.
|
---|
| 29 | * ArrayList (instead of List) to help Jackson serializer which avoids
|
---|
| 30 | * per-field type annotations.
|
---|
| 31 | */
|
---|
[810] | 32 | private final @NonNull ArrayListSet<@NonNull DiscreteValue> values = new ArrayListSet<DiscreteValue>();
|
---|
[519] | 33 |
|
---|
| 34 | @JsonCreator
|
---|
| 35 | public DiscreteValueSet(
|
---|
[810] | 36 | @JsonProperty("values") @NonNull Collection<@NonNull DiscreteValue> values) {
|
---|
| 37 | if (values.contains(null))
|
---|
| 38 | throw new NullPointerException("values should not contain null");
|
---|
[742] | 39 | this.values.addAll(values);
|
---|
[519] | 40 | }
|
---|
| 41 |
|
---|
[769] | 42 | //abandoned, because we can not overload constructor like this in Python
|
---|
| 43 | // public DiscreteValueSet(DiscreteValue... issuevalues) {
|
---|
| 44 | // for (DiscreteValue value : issuevalues) {
|
---|
| 45 | // this.values.add(value);
|
---|
| 46 | // }
|
---|
| 47 | // }
|
---|
[519] | 48 |
|
---|
[818] | 49 | public @NonNull List<@NonNull DiscreteValue> getValues() {
|
---|
[519] | 50 | return Collections.unmodifiableList(values);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | @Override
|
---|
[805] | 54 | public boolean contains(@NonNull Value value) {
|
---|
[519] | 55 | return values.contains(value);
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | @Override
|
---|
[804] | 59 | public @NonNull DiscreteValue get(@NonNull BigInteger index) {
|
---|
[519] | 60 | return values.get(index.intValue());
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | @Override
|
---|
[846] | 64 | @NonNull
|
---|
| 65 | public BigInteger size() {
|
---|
[519] | 66 | return BigInteger.valueOf(values.size());
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | @Override
|
---|
[846] | 70 | @NonNull
|
---|
[854] | 71 | //#PY # BigInteger and long are both int in python
|
---|
[846] | 72 | public DiscreteValue get(long index) {
|
---|
[519] | 73 | return values.get((int) index);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | @Override
|
---|
[810] | 77 | public @NonNull Iterator iterator() {
|
---|
[519] | 78 | return values.iterator();
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | @Override
|
---|
[804] | 82 | public @NonNull String toString() {
|
---|
[734] | 83 | return "DiscreteValueSet" + values.toString();
|
---|
[519] | 84 | }
|
---|
| 85 |
|
---|
| 86 | @Override
|
---|
| 87 | public int hashCode() {
|
---|
| 88 | final int prime = 31;
|
---|
| 89 | int result = 1;
|
---|
| 90 | result = prime * result + ((values == null) ? 0 : values.hashCode());
|
---|
| 91 | return result;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | @Override
|
---|
| 95 | public boolean equals(Object obj) {
|
---|
| 96 | if (this == obj)
|
---|
| 97 | return true;
|
---|
| 98 | if (obj == null)
|
---|
| 99 | return false;
|
---|
| 100 | if (getClass() != obj.getClass())
|
---|
| 101 | return false;
|
---|
| 102 | DiscreteValueSet other = (DiscreteValueSet) obj;
|
---|
| 103 | if (values == null) {
|
---|
| 104 | if (other.values != null)
|
---|
| 105 | return false;
|
---|
| 106 | } else if (!values.equals(other.values))
|
---|
| 107 | return false;
|
---|
| 108 | return true;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | }
|
---|