[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 | * A set of possible {@link Value}s (usually, of an Issue (which is represented
|
---|
| 17 | * by a String)).
|
---|
| 18 | *
|
---|
| 19 | * Value is the type of objects in this value set. We do not implement ValueSet
|
---|
| 20 | * right away because types are lost at runtime. Implementing separate classes
|
---|
| 21 | * for implementing the ValueSet ensures we can get back the type at runtime.
|
---|
| 22 | * immutable. Thread safe.
|
---|
| 23 | */
|
---|
| 24 | @JsonDeserialize(using = ValueSetDeserializer.class)
|
---|
| 25 | public interface ValueSet extends ImmutableList<Value> {
|
---|
| 26 |
|
---|
| 27 | /**
|
---|
| 28 | *
|
---|
| 29 | * @param value the value to check
|
---|
| 30 | * @return true iff this set contains given value
|
---|
| 31 | */
|
---|
| 32 | Boolean contains(Value value);
|
---|
| 33 |
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | /**
|
---|
| 37 | * Deserializes a ValueSet by checking the name of the (one and only) property
|
---|
| 38 | * in there.}. This property is coming from the concrete implementations that we
|
---|
| 39 | * have: {@link NumberValueSet} and {@link DiscreteValueSet}. This way we can
|
---|
| 40 | * avoid putting (redundant) type info in each and every valueset.
|
---|
| 41 | *
|
---|
| 42 | */
|
---|
| 43 | @SuppressWarnings("serial")
|
---|
| 44 | class ValueSetDeserializer extends StdDeserializer<ValueSet> {
|
---|
| 45 |
|
---|
| 46 | public ValueSetDeserializer() {
|
---|
| 47 | this(null);
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | public ValueSetDeserializer(Class<?> vc) {
|
---|
| 51 | super(vc);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | @Override
|
---|
| 55 | public ValueSet deserialize(JsonParser jp, DeserializationContext ctxt)
|
---|
| 56 | throws IOException, JsonProcessingException {
|
---|
| 57 | ObjectCodec codec = jp.getCodec();
|
---|
| 58 | JsonNode node = codec.readTree(jp);
|
---|
| 59 | if (node.get("range") != null) {
|
---|
| 60 | return codec.treeToValue(node, NumberValueSet.class);
|
---|
| 61 | } else if (node.get("values") != null) {
|
---|
| 62 | return codec.treeToValue(node, DiscreteValueSet.class);
|
---|
| 63 | }
|
---|
| 64 | throw new IllegalArgumentException(
|
---|
| 65 | "Expected 'range' or 'values' property for ValueSet contents");
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | } |
---|