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

Last change on this file since 21 was 21, checked in by bart, 4 years ago

Version 1.5.

File size: 3.1 KB
RevLine 
[21]1package geniusweb.actions;
2
3import java.util.Collections;
4import java.util.List;
5import java.util.Map;
6import java.util.Map.Entry;
7import java.util.Optional;
8import java.util.stream.Collectors;
9
10import com.fasterxml.jackson.annotation.JsonCreator;
11import com.fasterxml.jackson.annotation.JsonProperty;
12
13import geniusweb.issuevalue.Bid;
14
15/**
16 * Indicates that a party conditionally agrees with any of the {@link Vote}s
17 * provided.
18 */
19public class Votes extends AbstractAction {
20 private final List<Vote> 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. There can be at most 1
27 * {@link Vote} for each bid.
28 */
29 @JsonCreator
30 public Votes(@JsonProperty("actor") PartyId id,
31 @JsonProperty("votes") List<Vote> votes) {
32 super(id);
33 this.votes = votes;
34 if (votes == null)
35 throw new NullPointerException("votes must be not null");
36 for (Vote 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 Map<Bid, Long> counts = votes.stream().map(vote -> vote.getBid())
43 .collect(Collectors.groupingBy(bid -> bid,
44 Collectors.counting()));
45 Optional<Entry<Bid, Long>> nonunique = counts.entrySet().stream()
46 .filter(entry -> entry.getValue() > 1).findAny();
47 if (nonunique.isPresent())
48 throw new IllegalArgumentException(
49 "Votes contains multiple Vote's for "
50 + nonunique.get().getKey());
51
52 }
53
54 /**
55 * Test if Votes extends other votes. Extending means that for each vote on
56 * bid B with power P in othervotes, this contains also a vote for bid B
57 * with power at most P.
58 *
59 * @param otherVotes
60 * @return true iff this extends the otherVotes.
61 */
62 public boolean isExtending(Votes otherVotes) {
63 if (!otherVotes.getActor().equals(getActor()))
64 return false;
65 for (Vote vote : otherVotes.getVotes()) {
66 Vote myvote = getVote(vote.getBid());
67 if (myvote == null)
68 return false;
69 if (myvote.getMinPower() > vote.getMinPower())
70 return false;
71 }
72 return true;
73 }
74
75 /**
76 *
77 * @param bid the bid that we may have a vote for
78 * @return myvote for bid, or null if no vote for that bid;
79 */
80 public Vote getVote(Bid bid) {
81 for (Vote vote : votes) {
82 if (vote.getBid().equals(bid))
83 return vote;
84 }
85 return null;
86 }
87
88 public List<Vote> getVotes() {
89 return Collections.unmodifiableList(votes);
90 }
91
92 @Override
93 public int hashCode() {
94 final int prime = 31;
95 int result = super.hashCode();
96 result = prime * result + ((votes == null) ? 0 : votes.hashCode());
97 return result;
98 }
99
100 @Override
101 public boolean equals(Object obj) {
102 if (this == obj)
103 return true;
104 if (!super.equals(obj))
105 return false;
106 if (getClass() != obj.getClass())
107 return false;
108 Votes other = (Votes) obj;
109 if (votes == null) {
110 if (other.votes != null)
111 return false;
112 } else if (!votes.equals(other.votes))
113 return false;
114 return true;
115 }
116
117 @Override
118 public String toString() {
119 return "Votes[" + getActor() + "," + votes + "]";
120 }
121}
Note: See TracBrowser for help on using the repository browser.