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