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

Last change on this file since 831 was 825, checked in by wouter, 6 months ago

#291 move annotation to above the javadoc

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