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