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

Last change on this file since 770 was 770, checked in by wouter, 10 months ago

#169 fix #PY code

File size: 3.2 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 * Deserializes a ValueSet by checking the name of the (one and only) property
17 * in there.}. This property is coming from the concrete implementations that we
18 * have: {@link NumberValueSet} and {@link DiscreteValueSet}. This way we can
19 * avoid putting (redundant) type info in each and every valueset. <br>
20 * Python compatibility: define helper class first.
21 */
22@SuppressWarnings("serial")
23/*#PY
24 * from pyson.Deserializer import Deserializer
25 * from pyson.ObjectMapper import ObjectMapper
26 * from decimal import Decimal
27 * from tudelft.utilities.immutablelist.Range import Range
28 * from geniusweb.issuevalue.DiscreteValue import DiscreteValue
29 * from typing import List
30 *
31 * class ValueSetDeserializer (Deserializer):
32 * jackson=ObjectMapper()
33 *
34 * def deserialize(self, data:object, clas: object) -> ValueSet:
35 * if not isinstance(data,dict):
36 * raise ValueError("Expected dict but found " + str(data)
37 * + " of type " + str(type(data)))
38 * if 'range' in data.keys():
39 * from geniusweb.issuevalue.NumberValueSet import NumberValueSet
40 * return NumberValueSet(self.jackson.parse(data['range'], Range))
41 * if 'values' in data.keys():
42 * from geniusweb.issuevalue.DiscreteValueSet import DiscreteValueSet
43 * return DiscreteValueSet(self.jackson.parse(data['values'], List[DiscreteValue]))
44 * raise ValueError("Expected 'range' or 'values' property for ValueSet contents");
45 */
46class ValueSetDeserializer extends StdDeserializer<ValueSet> {
47
48 public ValueSetDeserializer() {
49 this(null);
50 }
51
52 public ValueSetDeserializer(Class<?> vc) {
53 super(vc);
54 }
55
56 @Override
57 public ValueSet deserialize(JsonParser jp, DeserializationContext ctxt)
58 throws IOException, JsonProcessingException {
59 ObjectCodec codec = jp.getCodec();
60 JsonNode node = codec.readTree(jp);
61 if (node.get("range") != null) {
62 return codec.treeToValue(node, NumberValueSet.class);
63 } else if (node.get("values") != null) {
64 return codec.treeToValue(node, DiscreteValueSet.class);
65 }
66 throw new IllegalArgumentException(
67 "Expected 'range' or 'values' property for ValueSet contents");
68 }
69
70}
71
72/**
73 *
74 * A set of possible {@link Value}s (usually, of an Issue (which is represented
75 * by a String)).
76 *
77 * Value is the type of objects in this value set. We do not implement ValueSet
78 * right away because types are lost at runtime. Implementing separate classes
79 * for implementing the ValueSet ensures we can get back the type at runtime.
80 * immutable. Thread safe.
81 */
82@JsonDeserialize(using = ValueSetDeserializer.class)
83public interface ValueSet extends ImmutableList<Value> {
84
85 /**
86 *
87 * @param value the value to check
88 * @return true iff this set contains given value
89 */
90 Boolean contains(Value value);
91
92}
Note: See TracBrowser for help on using the repository browser.