1 | package geniusweb.actions;
|
---|
2 |
|
---|
3 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
4 | import com.fasterxml.jackson.annotation.JsonProperty;
|
---|
5 | import com.fasterxml.jackson.annotation.JsonTypeName;
|
---|
6 |
|
---|
7 | import geniusweb.issuevalue.Bid;
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * A vote is an indication by a party that it conditionally accepts a bid. It's
|
---|
11 | * then up to the protocol to determine the outcome.
|
---|
12 | */
|
---|
13 | @JsonTypeName("votewithvalue")
|
---|
14 | public class VoteWithValue extends ActionWithBid {
|
---|
15 | private final Integer minPower;
|
---|
16 | private final Integer maxPower;
|
---|
17 | private final Integer value;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * @param id the {@link PartyId} that does the action
|
---|
21 | * @param bid the bid that is voted on
|
---|
22 | * @param minPower the minimum power this bid must get in order for the vote
|
---|
23 | * to be valid. Power is the sum of the powers of the
|
---|
24 | * parties that are in the deal. If power=1 for all
|
---|
25 | * participants (usually the default) power can be
|
---|
26 | * interpreted as number of votes
|
---|
27 | * @param maxPower the maximum power this bid must get in order for the vote
|
---|
28 | * to be valid. See {@link #minPower}
|
---|
29 | * @param value the value for this vote. An integer in [1,100]. Higher
|
---|
30 | * means that the actor likes this bid better. Note: value 0
|
---|
31 | * is reserved for "unacceptable" and therefore can not be
|
---|
32 | * used as a valid value.
|
---|
33 | */
|
---|
34 | @JsonCreator
|
---|
35 | public VoteWithValue(@JsonProperty("actor") PartyId id,
|
---|
36 | @JsonProperty("bid") Bid bid,
|
---|
37 | @JsonProperty("minPower") Integer minPower,
|
---|
38 | @JsonProperty("maxPower") Integer maxPower,
|
---|
39 | @JsonProperty("value") Integer value) {
|
---|
40 | super(id, bid);
|
---|
41 | this.minPower = minPower;
|
---|
42 | this.maxPower = maxPower;
|
---|
43 | this.value = value;
|
---|
44 | if (value < 1 || value > 100)
|
---|
45 | throw new IllegalArgumentException(
|
---|
46 | "value must be in [1,100] but got " + value);
|
---|
47 | if (bid == null || minPower == null || minPower < 1 || maxPower == null
|
---|
48 | || maxPower < minPower) {
|
---|
49 | throw new IllegalArgumentException(
|
---|
50 | "Vote must have non-null bid and minVotes, and minPower must be >=1 and maxPower must be >=minPower");
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | *
|
---|
56 | * @return the minimum power this bid must get in order for the vote to be
|
---|
57 | * valid.
|
---|
58 | */
|
---|
59 | public Integer getMinPower() {
|
---|
60 | return minPower;
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | *
|
---|
65 | * @return the max power this bid must get in order for the vote to be
|
---|
66 | * valid.
|
---|
67 | */
|
---|
68 | public Integer getMaxPower() {
|
---|
69 | return maxPower;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | *
|
---|
74 | * @return the value for this vote. An integer in [1,100]. Higher means that
|
---|
75 | * the actor likes this bid better.
|
---|
76 | *
|
---|
77 | */
|
---|
78 | public Integer getValue() {
|
---|
79 | return value;
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | public String toString() {
|
---|
84 | return "VoteWithValue[" + getActor() + "," + getBid() + "," + minPower
|
---|
85 | + "," + maxPower + "," + value + "]";
|
---|
86 | }
|
---|
87 |
|
---|
88 | @Override
|
---|
89 | public int hashCode() {
|
---|
90 | final int prime = 31;
|
---|
91 | int result = super.hashCode();
|
---|
92 | result = prime * result
|
---|
93 | + ((maxPower == null) ? 0 : maxPower.hashCode());
|
---|
94 | result = prime * result
|
---|
95 | + ((minPower == null) ? 0 : minPower.hashCode());
|
---|
96 | result = prime * result + ((value == null) ? 0 : value.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 | VoteWithValue other = (VoteWithValue) obj;
|
---|
109 | if (maxPower == null) {
|
---|
110 | if (other.maxPower != null)
|
---|
111 | return false;
|
---|
112 | } else if (!maxPower.equals(other.maxPower))
|
---|
113 | return false;
|
---|
114 | if (minPower == null) {
|
---|
115 | if (other.minPower != null)
|
---|
116 | return false;
|
---|
117 | } else if (!minPower.equals(other.minPower))
|
---|
118 | return false;
|
---|
119 | if (value == null) {
|
---|
120 | if (other.value != null)
|
---|
121 | return false;
|
---|
122 | } else if (!value.equals(other.value))
|
---|
123 | return false;
|
---|
124 | return true;
|
---|
125 | }
|
---|
126 |
|
---|
127 | }
|
---|