source: events/src/main/java/geniusweb/actions/Votes.java

Last change on this file was 52, checked in by ruud, 20 months ago

Fixed small issues in domaineditor.

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