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;
/**
* 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.
* Python compatibility: define helper class first.
*/
@SuppressWarnings("serial")
/*#PY
* from pyson.Deserializer import Deserializer
* from pyson.ObjectMapper import ObjectMapper
* from decimal import Decimal
* from tudelft.utilities.immutablelist.Range import Range
*
* class ValueSetDeserializer (Deserializer):
* jackson=ObjectMapper()
*
* def deserialize(self, data:object, clas: object) -> ValueSet:
* if not isinstance(data,dict):
* raise ValueError("Expected dict but found " + str(data)
* + " of type " + str(type(data)))
* if 'range' in data.keys():
* from geniusweb.issuevalue.NumberValueSet import NumberValueSet
* return NumberValueSet(self.jackson.parse(data['range'], Range))
* if 'values' in data.keys():
* from geniusweb.issuevalue.DiscreteValueSet import DiscreteValueSet
* return DiscreteValueSet(self.jackson.parse(data['issuevalues'], List))
* raise ValueError("Expected 'range' or 'values' property for ValueSet contents");
*/
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");
}
}
/**
*
* 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);
}