package geniusweb.inform; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Optional; import java.util.stream.Collectors; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import geniusweb.actions.PartyId; import geniusweb.actions.VotesWithValue; /** * Informs party that it's time to Opt-in. */ public class OptInWithValue implements Inform { private final List votes; /** * @param votes a list of votes. There may be only one votes per party. */ @JsonCreator public OptInWithValue(@JsonProperty("votes") List votes) { this.votes = votes; Map counts = votes.stream().map(vote -> vote.getActor()) .collect(Collectors.groupingBy(actor -> actor, Collectors.counting())); Optional> nonunique = counts.entrySet().stream() .filter(entry -> entry.getValue() > 1).findAny(); if (nonunique.isPresent()) throw new IllegalArgumentException( "OptIn contains multiple Votes for party " + nonunique.get().getKey()); } /** * @return list of votes that can be opted in to */ public List getVotes() { return Collections.unmodifiableList(votes); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((votes == null) ? 0 : votes.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; OptInWithValue other = (OptInWithValue) 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 "OptInWithValue[" + votes + "]"; } }