source: java2python/geniuswebtranslator/geniuswebsrc/geniusweb/issuevalue/ValueSet.java@ 670

Last change on this file since 670 was 660, checked in by wouter, 14 months ago

#169 Added manual translator of ValueSetDeserializer

File size: 2.8 KB
Line 
1package geniusweb.issuevalue;
2
3import java.io.IOException;
4
5import com.fasterxml.jackson.core.JsonParser;
6import com.fasterxml.jackson.core.JsonProcessingException;
7import com.fasterxml.jackson.core.ObjectCodec;
8import com.fasterxml.jackson.databind.DeserializationContext;
9import com.fasterxml.jackson.databind.JsonNode;
10import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
11import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
12
13import 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)
25public 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@SuppressWarnings("serial")
43/*#PY
44 * from pyson.Deserializer import Deserializer
45 * from decimal import Decimal
46 *
47 * class ValueSetDeserializer (Deserializer):
48 * jackson=ObjectMapper()
49 *
50 * def deserialize(self, data:object, clas: object) -> ValueSet:
51 * if not isinstance(data,dict):
52 * raise ValueError("Expected dict but found " + str(data)
53 * + " of type " + str(type(data)))
54 * if 'range' in data.keys():
55 * return NumberValueSet(self.jackson.parse(data['range'], Range))
56 * if 'values' in data.keys():
57 * return DiscreteValueSet(self.jackson.parse(data['issuevalues'], List))
58 * raise ValueError("Expected 'range' or 'values' property for ValueSet contents");
59 */
60class ValueSetDeserializer extends StdDeserializer<ValueSet> {
61
62 public ValueSetDeserializer() {
63 this(null);
64 }
65
66 public ValueSetDeserializer(Class<?> vc) {
67 super(vc);
68 }
69
70 @Override
71 public ValueSet deserialize(JsonParser jp, DeserializationContext ctxt)
72 throws IOException, JsonProcessingException {
73 ObjectCodec codec = jp.getCodec();
74 JsonNode node = codec.readTree(jp);
75 if (node.get("range") != null) {
76 return codec.treeToValue(node, NumberValueSet.class);
77 } else if (node.get("values") != null) {
78 return codec.treeToValue(node, DiscreteValueSet.class);
79 }
80 throw new IllegalArgumentException(
81 "Expected 'range' or 'values' property for ValueSet contents");
82 }
83
84}
Note: See TracBrowser for help on using the repository browser.