package geniusweb.protocol.session.mopac.phase; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import geniusweb.actions.Action; import geniusweb.actions.EndNegotiation; import geniusweb.actions.Offer; import geniusweb.actions.PartyId; import geniusweb.actions.Votes; import geniusweb.inform.Inform; import geniusweb.inform.Voting; import geniusweb.protocol.ProtocolException; import geniusweb.protocol.session.mopac.PartyStates; import geniusweb.voting.VotingEvaluator; public class VotingPhase extends DefaultPhase { /** * the offers received in the {@link OfferPhase} */ private final List offers; @JsonCreator protected VotingPhase(@JsonProperty("offers") List offers, @JsonProperty("partyStates") PartyStates partyStates, @JsonProperty("deadline") Long deadline, @JsonProperty("evaluator") VotingEvaluator evaluator) { super(partyStates, deadline, evaluator); this.offers = offers; if (offers == null) throw new IllegalArgumentException("offers must be not null"); } @Override public VotingPhase with(PartyId actor, Action action, long now) { try { checkAction(actor, action, now); } catch (ProtocolException ex) { return this.with(ex); } return new VotingPhase(offers, partyStates.with(action), deadline, evaluator); } @Override public Inform getInform() { return new Voting(offers, partyStates.getPowers()); } @Override public VotingPhase with(ProtocolException e) { return new VotingPhase(offers, partyStates.with(e), deadline, evaluator); } @Override public VotingPhase finish() { return new VotingPhase(offers, partyStates.finish(), deadline, evaluator); } @Override protected Phase checkedNext(long deadln) { return new OptInPhase(getVotes(), partyStates.flush(), deadln, evaluator); } @Override public List> getAllowedActions() { return Arrays.asList(Votes.class, EndNegotiation.class); } /** * @return all votes done in this phase. */ public List getVotes() { return partyStates.getActions().stream() .filter(act -> act instanceof Votes).map(act -> (Votes) act) .collect(Collectors.toList()); } /** * * @return the offers received in the {@link OfferPhase} */ public List getOffers() { return Collections.unmodifiableList(offers); } }