source: java2python/geniuswebtranslator/geniuswebsrc/geniusweb/profile/DefaultPartialOrdering.java@ 749

Last change on this file since 749 was 742, checked in by wouter, 12 months ago

#169 fixed @JsonProperty

File size: 5.2 KB
RevLine 
[519]1package geniusweb.profile;
2
[579]3import java.io.IOException;
[519]4import java.util.ArrayList;
5import java.util.Arrays;
6import java.util.HashSet;
7import java.util.LinkedList;
8import java.util.List;
9import java.util.Map;
10import java.util.Set;
11
[579]12import com.fasterxml.jackson.annotation.JsonAutoDetect;
13import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
[519]14import com.fasterxml.jackson.annotation.JsonCreator;
15import com.fasterxml.jackson.annotation.JsonProperty;
[579]16import com.fasterxml.jackson.core.JsonGenerator;
17import com.fasterxml.jackson.databind.DeserializationContext;
18import com.fasterxml.jackson.databind.JsonSerializer;
19import com.fasterxml.jackson.databind.KeyDeserializer;
20import com.fasterxml.jackson.databind.ObjectMapper;
21import com.fasterxml.jackson.databind.SerializerProvider;
22import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
23import com.fasterxml.jackson.databind.annotation.JsonSerialize;
[519]24
25import geniusweb.issuevalue.Bid;
26import geniusweb.issuevalue.Domain;
27
28/**
[579]29 * Strange that we need to do this. Bid is completely standard. The complication
30 * that this solves is that the keys are places as STRING in the json code
31 * because json allows only strings as key.
32 */
33@SuppressWarnings("serial")
34class BidDeserializer extends KeyDeserializer {
35 private ObjectMapper jackson = new ObjectMapper();
36
37 @Override
38 public Bid deserializeKey(String key, DeserializationContext ctxt)
39 throws IOException {
40 return jackson.readValue(key, Bid.class);
41 }
42
43}
44
45/**
46 * Serializes a Bid to string. Unfortunately by default jackson uses
47 * key.toString() for serializing (rather than
48 * {@link ObjectMapper#writeValueAsString(Object)}).
49 *
50 */
51class BidSerializer extends JsonSerializer<Bid> {
52 private ObjectMapper jackson = new ObjectMapper();
53
54 @Override
55 public void serialize(Bid bid, JsonGenerator gen,
56 SerializerProvider serializers) throws IOException {
57 gen.writeFieldName(jackson.writeValueAsString(bid));
58 }
59
60}
61
62/**
[519]63 * Default implementation of partial ordering that stores all is-better
64 * relations explicitly in a map.
65 *
66 * NOTICE this can handle profiles of max size Integer. This is because various
67 * functions used here rely on basic java functions that can handle only int.
68 * Besides, the size of partial maps grows very rapidly so this approach is
69 * limited anyway.
70 */
[579]71@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE, isGetterVisibility = Visibility.NONE)
[519]72public class DefaultPartialOrdering extends DefaultProfile
73 implements PartialOrdering {
74
75 /**
76 * Set is sparsely filled. If a Bid is not a key, it is not better than any
77 * other bid.
78 */
[579]79 //#PY # not needed in pyson, has fallback for key (de)serialization
80 @JsonDeserialize(keyUsing = BidDeserializer.class)
81 //#PY # not needed in pyson, has fallback for key (de)serialization
82 @JsonSerialize(keyUsing = BidSerializer.class)
83 private final Map<Bid, Set<Bid>> better;
[519]84
85 /**
[579]86 * @param name the name for the profile
87 * @param domain the {@link Domain} description
[742]88 * @param reservationBid the reservation {@link Bid}
[579]89 * @param better a map with keys = a better bid and value=a less
90 * good bid.
[519]91 */
92 @JsonCreator
93 public DefaultPartialOrdering(@JsonProperty("name") String name,
94 @JsonProperty("domain") Domain domain,
[742]95 @JsonProperty("reservationBid") Bid reservationBid,
[579]96 @JsonProperty("better") Map<Bid, Set<Bid>> better) {
[742]97 super(name, domain, reservationBid);
[579]98 this.better = better;
[519]99 }
100
101 @Override
102 public boolean isPreferredOrEqual(Bid bid1, Bid bid2) {
[579]103 if (!better.containsKey(bid1))
[519]104 return false;
[579]105 return better.get(bid1).contains(bid2);
[519]106 }
107
108 /**
109 *
110 * @return a list with all the bids that are referred to, either as better
111 * or as worse than another bid
112 */
113 public List<Bid> getBids() {
114 // FIXME the iteration order may not be guaranteed!
115 Set<Bid> bids = new HashSet<>();
[579]116 for (Bid bid : better.keySet()) {
[519]117 bids.add(bid);
[579]118 bids.addAll(better.get(bid));
[519]119 }
120 return new ArrayList<Bid>(bids);
121 }
122
123 /**
124 *
125 * @return a list of tuples [bid1index, bid2index]. It indicates that
126 * bids[bid1index] isbetterthan bids[bid2index].
127 */
128 public List<List<Integer>> getBetter() {
129 List<List<Integer>> betterlist = new LinkedList<>();
130 List<Bid> bidslist = getBids();
131
132 for (Bid bid : bidslist) {
[579]133 if (better.containsKey(bid)) {
134 for (Bid worsebid : better.get(bid)) {
[519]135 betterlist.add(Arrays.asList(bidslist.indexOf(bid),
136 bidslist.indexOf(worsebid)));
137 }
138 }
139 }
140
141 return betterlist;
142 }
143
144 @Override
145 public String toString() {
[579]146 return "DefaultPartialOrdering[" + getValuesString() + "," + better
[519]147 + "]";
148 }
149
150 @Override
151 public int hashCode() {
152 final int prime = 31;
153 int result = super.hashCode();
[579]154 result = prime * result + ((better == null) ? 0 : better.hashCode());
[519]155 return result;
156 }
157
158 @Override
159 public boolean equals(Object obj) {
160 if (this == obj)
161 return true;
162 if (!super.equals(obj))
163 return false;
164 if (getClass() != obj.getClass())
165 return false;
166 DefaultPartialOrdering other = (DefaultPartialOrdering) obj;
[579]167 if (better == null) {
168 if (other.better != null)
[519]169 return false;
[579]170 } else if (!better.equals(other.better))
[519]171 return false;
172 return true;
173 }
174
175}
Note: See TracBrowser for help on using the repository browser.