package geniusweb.actions; import java.util.Collections; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Set; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import geniusweb.issuevalue.Bid; /** * Indicates that a party conditionally agrees with any of the {@link Vote}s * provided. */ public class Votes extends AbstractAction { private final Set votes; /** * * @param actor party id * @param votes the {@link Vote}s that the party can agree on, if the * condition of the Vote holds. Every {@link Vote#getActor()} * should equal the id. There may be at most 1 vote per * {@link Bid}. */ @JsonCreator public Votes(@JsonProperty("actor") PartyId actor, @JsonProperty("votes") Set votes) { super(actor); this.votes = new HashSet<>(votes); if (votes == null) throw new NullPointerException("votes must be not null"); List votedBids = new LinkedList<>(); for (Vote vote : votes) { if (!vote.getActor().equals(actor)) throw new IllegalArgumentException("All votes must come from " + actor + " but found " + vote); Bid bid = vote.getBid(); if (votedBids.contains(bid)) throw new IllegalArgumentException( "Votes contains multiple Vote's for " + bid); votedBids.add(bid); } } /** * Test if Votes extends other votes. Extending means that for each vote on * bid B with power P in othervotes, this contains also a vote for bid B * with power at most P. * * @param otherVotes the {@link Votes}, usually from a previous round, that * this should extend. * @return true iff this extends the otherVotes. */ public boolean isExtending(Votes otherVotes) { if (!otherVotes.getActor().equals(getActor())) return false; for (Vote vote : otherVotes.getVotes()) { Vote myvote = getVote(vote.getBid()); if (myvote == null || myvote.getMinPower() > vote.getMinPower() || myvote.getMaxPower() < vote.getMaxPower()) return false; } return true; } /** * * @param bid the bid that we may have a vote for * @return myvote for bid, or null if no vote for that bid; */ public Vote getVote(Bid bid) { for (Vote vote : votes) { if (vote.getBid().equals(bid)) return vote; } return null; } public Set getVotes() { return Collections.unmodifiableSet(votes); } @Override public int hashCode() { final int prime = 31; int result = super.hashCode(); result = prime * result + ((votes == null) ? 0 : votes.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; Votes other = (Votes) obj; if (votes == null) { if (other.votes != null) return false; } else if (!votes.equals(other.votes)) return false; return true; } @Override public String toString() { return "Votes[" + getActor() + "," + votes + "]"; } }