source: protocol/src/main/java/geniusweb/protocol/session/mopac/phase/VotingPhase.java@ 52

Last change on this file since 52 was 52, checked in by ruud, 14 months ago

Fixed small issues in domaineditor.

File size: 2.4 KB
Line 
1package geniusweb.protocol.session.mopac.phase;
2
3import java.util.Arrays;
4import java.util.Collections;
5import java.util.List;
6import java.util.stream.Collectors;
7
8import com.fasterxml.jackson.annotation.JsonCreator;
9import com.fasterxml.jackson.annotation.JsonProperty;
10
11import geniusweb.actions.Action;
12import geniusweb.actions.EndNegotiation;
13import geniusweb.actions.Offer;
14import geniusweb.actions.PartyId;
15import geniusweb.actions.Votes;
16import geniusweb.inform.Inform;
17import geniusweb.inform.Voting;
18import geniusweb.protocol.ProtocolException;
19import geniusweb.protocol.session.mopac.PartyStates;
20import geniusweb.voting.VotingEvaluator;
21
22public 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}
Note: See TracBrowser for help on using the repository browser.