package geniusweb.inform; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonValue; import geniusweb.actions.PartyId; import geniusweb.issuevalue.Bid; /** * There can be multiple agreements from a single negotiation. This object * contains the agreements. Each party can agree only on 1 bid and there are * always at least 2 parties agreeing on a bid. */ @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE) public class Agreements { @JsonValue private final Map agreements; public Agreements() { this.agreements = Collections.emptyMap(); } @JsonCreator public Agreements(Map agrees) { this.agreements = agrees; } /** * @param bid a bid that all parties agreed on * @param parties the {@link PartyId}s of the agreeing parties */ public Agreements(Bid bid, Set parties) { this(parties.stream() .collect(Collectors.toMap(pid -> pid, pid -> bid))); } /** * @param other {@link Agreements} * @return a new Agreement containing this plus other agreements * @throws IllegalArgumentException if parties in other are already in this * agreement. */ public Agreements with(Agreements other) { Map newagrees = new HashMap<>(agreements); for (PartyId pid : other.agreements.keySet()) { if (newagrees.containsKey(pid)) throw new IllegalArgumentException( "party " + pid + " already has agreement"); newagrees.put(pid, other.agreements.get(pid)); } return new Agreements(newagrees); } /** * @return actual agreemenets contained here */ public Map getMap() { return Collections.unmodifiableMap(agreements); } @Override public String toString() { return "Agreements" + agreements; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((agreements == null) ? 0 : agreements.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Agreements other = (Agreements) obj; if (agreements == null) { if (other.agreements != null) return false; } else if (!agreements.equals(other.agreements)) return false; return true; } }