[52] | 1 | package geniusweb.actions;
|
---|
| 2 |
|
---|
| 3 | import java.util.Collections;
|
---|
| 4 | import java.util.Map;
|
---|
| 5 | import java.util.Map.Entry;
|
---|
| 6 | import java.util.Optional;
|
---|
| 7 | import java.util.Set;
|
---|
| 8 | import java.util.stream.Collectors;
|
---|
| 9 |
|
---|
| 10 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
| 11 | import com.fasterxml.jackson.annotation.JsonProperty;
|
---|
| 12 |
|
---|
| 13 | import geniusweb.issuevalue.Bid;
|
---|
| 14 |
|
---|
| 15 | /**
|
---|
| 16 | * Indicates that a party conditionally agrees with any of the
|
---|
| 17 | * {@link VoteWithValue}s provided.
|
---|
| 18 | */
|
---|
| 19 | public class VotesWithValue extends AbstractAction {
|
---|
| 20 | private final Set<VoteWithValue> votes;
|
---|
| 21 |
|
---|
| 22 | /**
|
---|
| 23 | *
|
---|
| 24 | * @param id party id
|
---|
| 25 | * @param votes the {@link Vote}s that the party can agree on, if the
|
---|
| 26 | * condition of the Vote holds. Every {@link Vote#getActor()}
|
---|
| 27 | * should equal the id. The sum of the values must =100.
|
---|
| 28 | */
|
---|
| 29 | @JsonCreator
|
---|
| 30 | public VotesWithValue(@JsonProperty("actor") PartyId id,
|
---|
| 31 | @JsonProperty("votes") Set<VoteWithValue> votes) {
|
---|
| 32 | super(id);
|
---|
| 33 | this.votes = votes;
|
---|
| 34 | if (votes == null)
|
---|
| 35 | throw new NullPointerException("votes must be not null");
|
---|
| 36 | for (VoteWithValue vote : votes) {
|
---|
| 37 | if (!vote.getActor().equals(id)) {
|
---|
| 38 | throw new IllegalArgumentException("All votes must come from "
|
---|
| 39 | + id + " but found " + vote);
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 | if (votes.stream().map(v -> v.getValue()).reduce(0,
|
---|
| 43 | Integer::sum) != 100)
|
---|
| 44 | throw new IllegalArgumentException(
|
---|
| 45 | "Sum of the placed votes must be 100");
|
---|
| 46 | // check for duplicate Vote's, possibly with different powers
|
---|
| 47 | Map<Bid, Long> counts = votes.stream().map(vote -> vote.getBid())
|
---|
| 48 | .collect(Collectors.groupingBy(bid -> bid,
|
---|
| 49 | Collectors.counting()));
|
---|
| 50 | Optional<Entry<Bid, Long>> nonunique = counts.entrySet().stream()
|
---|
| 51 | .filter(entry -> entry.getValue() > 1).findAny();
|
---|
| 52 | if (nonunique.isPresent())
|
---|
| 53 | throw new IllegalArgumentException(
|
---|
| 54 | "Votes contains multiple Vote's for "
|
---|
| 55 | + nonunique.get().getKey());
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | /**
|
---|
| 59 | * Test if Votes extends other votes. Extending means that for each vote on
|
---|
| 60 | * bid B with power P in othervotes, this contains also a vote for bid B
|
---|
| 61 | * with power at most P.
|
---|
| 62 | *
|
---|
| 63 | * @param otherVotes the {@link VotesWithValue}, usually from a previous
|
---|
| 64 | * round, that this should extend.
|
---|
| 65 | * @return true iff this extends the otherVotes.
|
---|
| 66 | */
|
---|
| 67 | public boolean isExtending(VotesWithValue otherVotes) {
|
---|
| 68 | if (!otherVotes.getActor().equals(getActor()))
|
---|
| 69 | return false;
|
---|
| 70 | for (VoteWithValue vote : otherVotes.getVotes()) {
|
---|
| 71 | VoteWithValue myvote = getVote(vote.getBid());
|
---|
| 72 | if (myvote == null || myvote.getMinPower() > vote.getMinPower()
|
---|
| 73 | || myvote.getMaxPower() < vote.getMaxPower())
|
---|
| 74 | return false;
|
---|
| 75 | }
|
---|
| 76 | return true;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | /**
|
---|
| 80 | *
|
---|
| 81 | * @param bid the bid that we may have a vote for
|
---|
| 82 | * @return myvote for bid, or null if no vote for that bid;
|
---|
| 83 | */
|
---|
| 84 | public VoteWithValue getVote(Bid bid) {
|
---|
| 85 | for (VoteWithValue vote : votes) {
|
---|
| 86 | if (vote.getBid().equals(bid))
|
---|
| 87 | return vote;
|
---|
| 88 | }
|
---|
| 89 | return null;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | public Set<VoteWithValue> getVotes() {
|
---|
| 93 | return Collections.unmodifiableSet(votes);
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | @Override
|
---|
| 97 | public int hashCode() {
|
---|
| 98 | final int prime = 31;
|
---|
| 99 | int result = super.hashCode();
|
---|
| 100 | result = prime * result + ((votes == null) ? 0 : votes.hashCode());
|
---|
| 101 | return result;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | @Override
|
---|
| 105 | public boolean equals(Object obj) {
|
---|
| 106 | if (this == obj)
|
---|
| 107 | return true;
|
---|
| 108 | if (!super.equals(obj))
|
---|
| 109 | return false;
|
---|
| 110 | if (getClass() != obj.getClass())
|
---|
| 111 | return false;
|
---|
| 112 | VotesWithValue other = (VotesWithValue) obj;
|
---|
| 113 | if (votes == null) {
|
---|
| 114 | if (other.votes != null)
|
---|
| 115 | return false;
|
---|
| 116 | } else if (!votes.equals(other.votes))
|
---|
| 117 | return false;
|
---|
| 118 | return true;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | @Override
|
---|
| 122 | public String toString() {
|
---|
| 123 | return "VotesWithValue[" + getActor() + "," + votes + "]";
|
---|
| 124 | }
|
---|
| 125 | }
|
---|