package geniusweb.issuevalue; import java.io.IOException; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.ObjectCodec; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.deser.std.StdDeserializer; import tudelft.utilities.immutablelist.ImmutableList; /** * A set of possible {@link Value}s (usually, of an Issue (which is represented * by a String)). * * Value is the type of objects in this value set. We do not implement ValueSet * right away because types are lost at runtime. Implementing separate classes * for implementing the ValueSet ensures we can get back the type at runtime. * immutable. Thread safe. */ @JsonDeserialize(using = ValueSetDeserializer.class) public interface ValueSet extends ImmutableList { /** * * @param value the value to check * @return true iff this set contains given value */ Boolean contains(Value value); } /** * Deserializes a ValueSet by checking the name of the (one and only) property * in there.}. This property is coming from the concrete implementations that we * have: {@link NumberValueSet} and {@link DiscreteValueSet}. This way we can * avoid putting (redundant) type info in each and every valueset. * */ @SuppressWarnings("serial") class ValueSetDeserializer extends StdDeserializer { public ValueSetDeserializer() { this(null); } public ValueSetDeserializer(Class vc) { super(vc); } @Override public ValueSet deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { ObjectCodec codec = jp.getCodec(); JsonNode node = codec.readTree(jp); if (node.get("range") != null) { return codec.treeToValue(node, NumberValueSet.class); } else if (node.get("values") != null) { return codec.treeToValue(node, DiscreteValueSet.class); } throw new IllegalArgumentException( "Expected 'range' or 'values' property for ValueSet contents"); } }