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 |
|
---|
9 | import org.eclipse.jdt.annotation.NonNull;
|
---|
10 |
|
---|
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 | */
|
---|
32 | private final @NonNull ArrayListSet<@NonNull DiscreteValue> values = new ArrayListSet<DiscreteValue>();
|
---|
33 |
|
---|
34 | @JsonCreator
|
---|
35 | public DiscreteValueSet(
|
---|
36 | @JsonProperty("values") @NonNull Collection<@NonNull DiscreteValue> values) {
|
---|
37 | if (values.contains(null))
|
---|
38 | throw new NullPointerException("values should not contain null");
|
---|
39 | this.values.addAll(values);
|
---|
40 | }
|
---|
41 |
|
---|
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 | // }
|
---|
48 |
|
---|
49 | public @NonNull List<DiscreteValue> getValues() {
|
---|
50 | return Collections.unmodifiableList(values);
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | public boolean contains(@NonNull Value value) {
|
---|
55 | return values.contains(value);
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override
|
---|
59 | public @NonNull DiscreteValue get(@NonNull BigInteger index) {
|
---|
60 | return values.get(index.intValue());
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Override
|
---|
64 | public @NonNull BigInteger size() {
|
---|
65 | return BigInteger.valueOf(values.size());
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | public @NonNull DiscreteValue get(long index) {
|
---|
70 | return values.get((int) index);
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | public @NonNull Iterator iterator() {
|
---|
75 | return values.iterator();
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Override
|
---|
79 | public @NonNull String toString() {
|
---|
80 | return "DiscreteValueSet" + values.toString();
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Override
|
---|
84 | public int hashCode() {
|
---|
85 | final int prime = 31;
|
---|
86 | int result = 1;
|
---|
87 | result = prime * result + ((values == null) ? 0 : values.hashCode());
|
---|
88 | return result;
|
---|
89 | }
|
---|
90 |
|
---|
91 | @Override
|
---|
92 | public boolean equals(Object obj) {
|
---|
93 | if (this == obj)
|
---|
94 | return true;
|
---|
95 | if (obj == null)
|
---|
96 | return false;
|
---|
97 | if (getClass() != obj.getClass())
|
---|
98 | return false;
|
---|
99 | DiscreteValueSet other = (DiscreteValueSet) obj;
|
---|
100 | if (values == null) {
|
---|
101 | if (other.values != null)
|
---|
102 | return false;
|
---|
103 | } else if (!values.equals(other.values))
|
---|
104 | return false;
|
---|
105 | return true;
|
---|
106 | }
|
---|
107 |
|
---|
108 | }
|
---|