[519] | 1 | package geniusweb.issuevalue;
|
---|
| 2 |
|
---|
| 3 | import java.io.IOException;
|
---|
| 4 |
|
---|
[804] | 5 | import org.eclipse.jdt.annotation.NonNull;
|
---|
| 6 |
|
---|
[519] | 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
|
---|
[692] | 21 | * avoid putting (redundant) type info in each and every valueset. <br>
|
---|
| 22 | * Python compatibility: define helper class first.
|
---|
[660] | 23 | */
|
---|
| 24 | @SuppressWarnings("serial")
|
---|
| 25 | /*#PY
|
---|
| 26 | * from pyson.Deserializer import Deserializer
|
---|
[692] | 27 | * from pyson.ObjectMapper import ObjectMapper
|
---|
[660] | 28 | * from decimal import Decimal
|
---|
[769] | 29 | * from tudelft.utilities.immutablelist.Range import Range
|
---|
[770] | 30 | * from geniusweb.issuevalue.DiscreteValue import DiscreteValue
|
---|
| 31 | * from typing import List
|
---|
[519] | 32 | *
|
---|
[660] | 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():
|
---|
[769] | 41 | * from geniusweb.issuevalue.NumberValueSet import NumberValueSet
|
---|
[660] | 42 | * return NumberValueSet(self.jackson.parse(data['range'], Range))
|
---|
| 43 | * if 'values' in data.keys():
|
---|
[769] | 44 | * from geniusweb.issuevalue.DiscreteValueSet import DiscreteValueSet
|
---|
[770] | 45 | * return DiscreteValueSet(self.jackson.parse(data['values'], List[DiscreteValue]))
|
---|
[660] | 46 | * raise ValueError("Expected 'range' or 'values' property for ValueSet contents");
|
---|
[519] | 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 |
|
---|
[692] | 72 | }
|
---|
| 73 |
|
---|
[825] | 74 | @JsonDeserialize(using = ValueSetDeserializer.class)
|
---|
[692] | 75 | /**
|
---|
| 76 | *
|
---|
| 77 | * A set of possible {@link Value}s (usually, of an Issue (which is represented
|
---|
| 78 | * by a String)).
|
---|
| 79 | *
|
---|
| 80 | * Value is the type of objects in this value set. We do not implement ValueSet
|
---|
| 81 | * right away because types are lost at runtime. Implementing separate classes
|
---|
| 82 | * for implementing the ValueSet ensures we can get back the type at runtime.
|
---|
| 83 | * immutable. Thread safe.
|
---|
| 84 | */
|
---|
[810] | 85 | public interface ValueSet extends ImmutableList<@NonNull Value> {
|
---|
[692] | 86 |
|
---|
| 87 | /**
|
---|
| 88 | *
|
---|
| 89 | * @param value the value to check
|
---|
| 90 | * @return true iff this set contains given value
|
---|
| 91 | */
|
---|
[805] | 92 | boolean contains(@NonNull Value value);
|
---|
[692] | 93 |
|
---|
[519] | 94 | } |
---|