package geniusweb.profile; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; import org.eclipse.jdt.annotation.NonNull; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.KeyDeserializer; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import geniusweb.issuevalue.Bid; import geniusweb.issuevalue.Domain; @SuppressWarnings("serial") /** * Strange that we need to do this. Bid is completely standard. The complication * that this solves is that the keys are places as STRING in the json code * because json allows only strings as key. */ //#PY # key deserializer not needed in python class BidDeserializer extends KeyDeserializer { private ObjectMapper jackson = new ObjectMapper(); @Override public Bid deserializeKey(String key, DeserializationContext ctxt) throws IOException { return jackson.readValue(key, Bid.class); } } /** * Serializes a Bid to string. Unfortunately by default jackson uses * key.toString() for serializing (rather than * {@link ObjectMapper#writeValueAsString(Object)}). * */ //#PY # Key serializer not needed in python class BidSerializer extends JsonSerializer { private ObjectMapper jackson = new ObjectMapper(); @Override public void serialize(Bid bid, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeFieldName(jackson.writeValueAsString(bid)); } } @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE, isGetterVisibility = Visibility.NONE) /** * Default implementation of partial ordering that stores all is-better * relations explicitly in a map. * * NOTICE this can handle profiles of max size Integer. This is because various * functions used here rely on basic java functions that can handle only int. * Besides, the size of partial maps grows very rapidly so this approach is * limited anyway. */ public class DefaultPartialOrdering extends DefaultProfile implements PartialOrdering { //#PY # not needed in pyson, has fallback for key (de)serialization @JsonDeserialize(keyUsing = BidDeserializer.class) //#PY # not needed in pyson, has fallback for key (de)serialization @JsonSerialize(keyUsing = BidSerializer.class) @NonNull /** * Set is sparsely filled. If a Bid is not a key, it is not better than any * other bid. */ private final Map<@NonNull Bid, @NonNull Set<@NonNull Bid>> better = new HashMap<>(); @JsonCreator /** * @param name the name for the profile * @param domain the {@link Domain} description * @param reservationBid the reservation {@link Bid} possibly null * @param better a map with keys = a better bid and value=a less * good bid. */ public DefaultPartialOrdering( final @NonNull @JsonProperty("name") String name, final @NonNull @JsonProperty("domain") Domain domain, final @JsonProperty("reservationBid") Bid reservationBid, final @NonNull @JsonProperty("better") Map<@NonNull Bid, @NonNull Set<@NonNull Bid>> better) { super(name, domain, reservationBid); this.better.putAll(better); } @Override public boolean isPreferredOrEqual(final @NonNull Bid bid1, final @NonNull Bid bid2) { if (!better.containsKey(bid1)) return false; return better.get(bid1).contains(bid2); } @NonNull /** * * @return a list with all the bids that are referred to, either as better * or as worse than another bid */ public List<@NonNull Bid> getBids() { // FIXME the iteration order may not be guaranteed! final @NonNull Set<@NonNull Bid> bids = new HashSet<>(); for (@NonNull Bid bid : better.keySet()) { bids.add(bid); bids.addAll(better.get(bid)); } return new ArrayList(bids); } @NonNull /** * * @return a list of tuples [bid1index, bid2index]. It indicates that * bids[bid1index] isbetterthan bids[bid2index]. */ public List<@NonNull List<@NonNull Integer>> getBetter() { final @NonNull List<@NonNull List<@NonNull Integer>> betterlist = new LinkedList<>(); final @NonNull List<@NonNull Bid> bidslist = getBids(); for (final @NonNull Bid bid : bidslist) { if (better.containsKey(bid)) { for (final @NonNull Bid worsebid : better.get(bid)) { betterlist.add(Arrays.asList(bidslist.indexOf(bid), bidslist.indexOf(worsebid))); } } } return betterlist; } @Override public @NonNull String toString() { return "DefaultPartialOrdering[" + getValuesString() + "," + better + "]"; } @Override public int hashCode() { final int prime = 31; int result = super.hashCode(); result = prime * result + ((better == null) ? 0 : better.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (!super.equals(obj)) return false; if (getClass() != obj.getClass()) return false; DefaultPartialOrdering other = (DefaultPartialOrdering) obj; if (better == null) { if (other.better != null) return false; } else if (!better.equals(other.better)) return false; return true; } }