1 | package geniusweb.issuevalue;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 |
|
---|
5 | import org.eclipse.jdt.annotation.NonNull;
|
---|
6 |
|
---|
7 | import com.fasterxml.jackson.core.JsonParser;
|
---|
8 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
9 | import com.fasterxml.jackson.core.ObjectCodec;
|
---|
10 | import com.fasterxml.jackson.databind.DeserializationContext;
|
---|
11 | import com.fasterxml.jackson.databind.JsonNode;
|
---|
12 | import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
---|
13 | import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
---|
14 |
|
---|
15 | import tudelft.utilities.immutablelist.ImmutableList;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Deserializes a ValueSet by checking the name of the (one and only) property
|
---|
19 | * in there.}. This property is coming from the concrete implementations that we
|
---|
20 | * have: {@link NumberValueSet} and {@link DiscreteValueSet}. This way we can
|
---|
21 | * avoid putting (redundant) type info in each and every valueset. <br>
|
---|
22 | * Python compatibility: define helper class first.
|
---|
23 | */
|
---|
24 | @SuppressWarnings("serial")
|
---|
25 | /*#PY
|
---|
26 | * from pyson.Deserializer import Deserializer
|
---|
27 | * from pyson.ObjectMapper import ObjectMapper
|
---|
28 | * from decimal import Decimal
|
---|
29 | * from tudelft.utilities.immutablelist.Range import Range
|
---|
30 | * from geniusweb.issuevalue.DiscreteValue import DiscreteValue
|
---|
31 | * from typing import List
|
---|
32 | *
|
---|
33 | * class ValueSetDeserializer (Deserializer):
|
---|
34 | * jackson=ObjectMapper()
|
---|
35 | *
|
---|
36 | * def deserialize(self, data:object, clas: object) -> ValueSet:
|
---|
37 | * if not isinstance(data,dict):
|
---|
38 | * raise ValueError("Expected dict but found " + str(data)
|
---|
39 | * + " of type " + str(type(data)))
|
---|
40 | * if 'range' in data.keys():
|
---|
41 | * from geniusweb.issuevalue.NumberValueSet import NumberValueSet
|
---|
42 | * return NumberValueSet(self.jackson.parse(data['range'], Range))
|
---|
43 | * if 'values' in data.keys():
|
---|
44 | * from geniusweb.issuevalue.DiscreteValueSet import DiscreteValueSet
|
---|
45 | * return DiscreteValueSet(self.jackson.parse(data['values'], List[DiscreteValue]))
|
---|
46 | * raise ValueError("Expected 'range' or 'values' property for ValueSet contents");
|
---|
47 | */
|
---|
48 | class ValueSetDeserializer extends StdDeserializer<ValueSet> {
|
---|
49 |
|
---|
50 | public ValueSetDeserializer() {
|
---|
51 | this(null);
|
---|
52 | }
|
---|
53 |
|
---|
54 | public ValueSetDeserializer(Class<?> vc) {
|
---|
55 | super(vc);
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override
|
---|
59 | public ValueSet deserialize(JsonParser jp, DeserializationContext ctxt)
|
---|
60 | throws IOException, JsonProcessingException {
|
---|
61 | ObjectCodec codec = jp.getCodec();
|
---|
62 | JsonNode node = codec.readTree(jp);
|
---|
63 | if (node.get("range") != null) {
|
---|
64 | return codec.treeToValue(node, NumberValueSet.class);
|
---|
65 | } else if (node.get("values") != null) {
|
---|
66 | return codec.treeToValue(node, DiscreteValueSet.class);
|
---|
67 | }
|
---|
68 | throw new IllegalArgumentException(
|
---|
69 | "Expected 'range' or 'values' property for ValueSet contents");
|
---|
70 | }
|
---|
71 |
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | *
|
---|
76 | * A set of possible {@link Value}s (usually, of an Issue (which is represented
|
---|
77 | * by a String)).
|
---|
78 | *
|
---|
79 | * Value is the type of objects in this value set. We do not implement ValueSet
|
---|
80 | * right away because types are lost at runtime. Implementing separate classes
|
---|
81 | * for implementing the ValueSet ensures we can get back the type at runtime.
|
---|
82 | * immutable. Thread safe.
|
---|
83 | */
|
---|
84 | @JsonDeserialize(using = ValueSetDeserializer.class)
|
---|
85 | public interface ValueSet extends ImmutableList<Value> {
|
---|
86 |
|
---|
87 | /**
|
---|
88 | *
|
---|
89 | * @param value the value to check
|
---|
90 | * @return true iff this set contains given value
|
---|
91 | */
|
---|
92 | Boolean contains(@NonNull Value value);
|
---|
93 |
|
---|
94 | } |
---|