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

Last change on this file since 805 was 804, checked in by wouter, 7 months ago

#278 added NonNull annotation in many places in the geniusweb code

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