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

Last change on this file since 32 was 32, checked in by bart, 3 years ago

Multiple learns with repeated tournament, maven use https.

File size: 3.2 KB
Line 
1package geniusweb.actions;
2
3import java.util.Collections;
4import java.util.Map;
5import java.util.Map.Entry;
6import java.util.Optional;
7import java.util.Set;
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 Set<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. Every {@link Vote#getActor()}
27 * should equal the id
28 */
29 @JsonCreator
30 public Votes(@JsonProperty("actor") PartyId id,
31 @JsonProperty("votes") Set<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 // check for duplicate Vote's, possibly with different powers
43 Map<Bid, Long> counts = votes.stream().map(vote -> vote.getBid())
44 .collect(Collectors.groupingBy(bid -> bid,
45 Collectors.counting()));
46 Optional<Entry<Bid, Long>> nonunique = counts.entrySet().stream()
47 .filter(entry -> entry.getValue() > 1).findAny();
48 if (nonunique.isPresent())
49 throw new IllegalArgumentException(
50 "Votes contains multiple Vote's for "
51 + nonunique.get().getKey());
52
53 }
54
55 /**
56 * Test if Votes extends other votes. Extending means that for each vote on
57 * bid B with power P in othervotes, this contains also a vote for bid B
58 * with power at most P.
59 *
60 * @param otherVotes the {@link Votes}, usually from a previous round, that
61 * this should extend.
62 * @return true iff this extends the otherVotes.
63 */
64 public boolean isExtending(Votes otherVotes) {
65 if (!otherVotes.getActor().equals(getActor()))
66 return false;
67 for (Vote vote : otherVotes.getVotes()) {
68 Vote myvote = getVote(vote.getBid());
69 if (myvote == null || myvote.getMinPower() > vote.getMinPower()
70 || myvote.getMaxPower() < vote.getMaxPower())
71 return false;
72 }
73 return true;
74 }
75
76 /**
77 *
78 * @param bid the bid that we may have a vote for
79 * @return myvote for bid, or null if no vote for that bid;
80 */
81 public Vote getVote(Bid bid) {
82 for (Vote vote : votes) {
83 if (vote.getBid().equals(bid))
84 return vote;
85 }
86 return null;
87 }
88
89 public Set<Vote> getVotes() {
90 return Collections.unmodifiableSet(votes);
91 }
92
93 @Override
94 public int hashCode() {
95 final int prime = 31;
96 int result = super.hashCode();
97 result = prime * result + ((votes == null) ? 0 : votes.hashCode());
98 return result;
99 }
100
101 @Override
102 public boolean equals(Object obj) {
103 if (this == obj)
104 return true;
105 if (!super.equals(obj))
106 return false;
107 if (getClass() != obj.getClass())
108 return false;
109 Votes other = (Votes) obj;
110 if (votes == null) {
111 if (other.votes != null)
112 return false;
113 } else if (!votes.equals(other.votes))
114 return false;
115 return true;
116 }
117
118 @Override
119 public String toString() {
120 return "Votes[" + getActor() + "," + votes + "]";
121 }
122}
Note: See TracBrowser for help on using the repository browser.