1 | package geniusweb.protocol.session.mopac.phase;
|
---|
2 |
|
---|
3 | import java.util.Arrays;
|
---|
4 | import java.util.Collections;
|
---|
5 | import java.util.List;
|
---|
6 | import java.util.stream.Collectors;
|
---|
7 |
|
---|
8 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
9 | import com.fasterxml.jackson.annotation.JsonProperty;
|
---|
10 |
|
---|
11 | import geniusweb.actions.Action;
|
---|
12 | import geniusweb.actions.EndNegotiation;
|
---|
13 | import geniusweb.actions.Offer;
|
---|
14 | import geniusweb.actions.PartyId;
|
---|
15 | import geniusweb.actions.Votes;
|
---|
16 | import geniusweb.inform.Inform;
|
---|
17 | import geniusweb.inform.Voting;
|
---|
18 | import geniusweb.protocol.ProtocolException;
|
---|
19 | import geniusweb.protocol.session.mopac.PartyStates;
|
---|
20 | import geniusweb.voting.VotingEvaluator;
|
---|
21 |
|
---|
22 | public class VotingPhase extends DefaultPhase {
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * the offers received in the {@link OfferPhase}
|
---|
26 | */
|
---|
27 | private final List<Offer> offers;
|
---|
28 |
|
---|
29 | @JsonCreator
|
---|
30 | protected VotingPhase(@JsonProperty("offers") List<Offer> offers,
|
---|
31 | @JsonProperty("partyStates") PartyStates partyStates,
|
---|
32 | @JsonProperty("deadline") Long deadline,
|
---|
33 | @JsonProperty("evaluator") VotingEvaluator evaluator) {
|
---|
34 | super(partyStates, deadline, evaluator);
|
---|
35 | this.offers = offers;
|
---|
36 | if (offers == null)
|
---|
37 | throw new IllegalArgumentException("offers must be not null");
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Override
|
---|
41 | public VotingPhase with(PartyId actor, Action action, long now) {
|
---|
42 | try {
|
---|
43 | checkAction(actor, action, now);
|
---|
44 | } catch (ProtocolException ex) {
|
---|
45 | return this.with(ex);
|
---|
46 | }
|
---|
47 | return new VotingPhase(offers, partyStates.with(action), deadline,
|
---|
48 | evaluator);
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | public Inform getInform() {
|
---|
53 | return new Voting(offers, partyStates.getPowers());
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Override
|
---|
57 | public VotingPhase with(ProtocolException e) {
|
---|
58 | return new VotingPhase(offers, partyStates.with(e), deadline,
|
---|
59 | evaluator);
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public VotingPhase finish() {
|
---|
64 | return new VotingPhase(offers, partyStates.finish(), deadline,
|
---|
65 | evaluator);
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | protected Phase checkedNext(long deadln) {
|
---|
70 | return new OptInPhase(getVotes(), partyStates.flush(), deadln,
|
---|
71 | evaluator);
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public List<Class<? extends Action>> getAllowedActions() {
|
---|
76 | return Arrays.asList(Votes.class, EndNegotiation.class);
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * @return all votes done in this phase.
|
---|
81 | */
|
---|
82 | public List<Votes> getVotes() {
|
---|
83 | return partyStates.getActions().stream()
|
---|
84 | .filter(act -> act instanceof Votes).map(act -> (Votes) act)
|
---|
85 | .collect(Collectors.toList());
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | *
|
---|
90 | * @return the offers received in the {@link OfferPhase}
|
---|
91 | */
|
---|
92 | public List<Offer> getOffers() {
|
---|
93 | return Collections.unmodifiableList(offers);
|
---|
94 | }
|
---|
95 | }
|
---|