1 | package geniusweb.inform;
|
---|
2 |
|
---|
3 | import java.util.Collections;
|
---|
4 | import java.util.List;
|
---|
5 | import java.util.Map;
|
---|
6 | import java.util.Map.Entry;
|
---|
7 | import java.util.Optional;
|
---|
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.actions.PartyId;
|
---|
14 | import geniusweb.actions.VotesWithValue;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Informs party that it's time to Opt-in.
|
---|
18 | */
|
---|
19 | public class OptInWithValue implements Inform {
|
---|
20 |
|
---|
21 | private final List<VotesWithValue> votes;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * @param votes a list of votes. There may be only one votes per party.
|
---|
25 | */
|
---|
26 | @JsonCreator
|
---|
27 | public OptInWithValue(@JsonProperty("votes") List<VotesWithValue> votes) {
|
---|
28 | this.votes = votes;
|
---|
29 |
|
---|
30 | Map<PartyId, Long> counts = votes.stream().map(vote -> vote.getActor())
|
---|
31 | .collect(Collectors.groupingBy(actor -> actor,
|
---|
32 | Collectors.counting()));
|
---|
33 | Optional<Entry<PartyId, Long>> nonunique = counts.entrySet().stream()
|
---|
34 | .filter(entry -> entry.getValue() > 1).findAny();
|
---|
35 | if (nonunique.isPresent())
|
---|
36 | throw new IllegalArgumentException(
|
---|
37 | "OptIn contains multiple Votes for party "
|
---|
38 | + nonunique.get().getKey());
|
---|
39 |
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * @return list of votes that can be opted in to
|
---|
44 | */
|
---|
45 | public List<VotesWithValue> getVotes() {
|
---|
46 | return Collections.unmodifiableList(votes);
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | public int hashCode() {
|
---|
51 | final int prime = 31;
|
---|
52 | int result = 1;
|
---|
53 | result = prime * result + ((votes == null) ? 0 : votes.hashCode());
|
---|
54 | return result;
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | public boolean equals(Object obj) {
|
---|
59 | if (this == obj)
|
---|
60 | return true;
|
---|
61 | if (obj == null)
|
---|
62 | return false;
|
---|
63 | if (getClass() != obj.getClass())
|
---|
64 | return false;
|
---|
65 | OptInWithValue other = (OptInWithValue) obj;
|
---|
66 | if (votes == null) {
|
---|
67 | if (other.votes != null)
|
---|
68 | return false;
|
---|
69 | } else if (!votes.equals(other.votes))
|
---|
70 | return false;
|
---|
71 | return true;
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public String toString() {
|
---|
76 | return "OptInWithValue[" + votes + "]";
|
---|
77 | }
|
---|
78 |
|
---|
79 | }
|
---|