[52] | 1 | package geniusweb.protocol.session.mopac2.phase;
|
---|
| 2 |
|
---|
| 3 | import geniusweb.actions.Action;
|
---|
| 4 | import geniusweb.actions.PartyId;
|
---|
| 5 | import geniusweb.protocol.ProtocolException;
|
---|
| 6 | import geniusweb.protocol.session.mopac2.PartyStates;
|
---|
| 7 | import geniusweb.voting.VotingEvaluator;
|
---|
| 8 | import geniusweb.voting.VotingEvaluatorWithValue;
|
---|
| 9 |
|
---|
| 10 | public abstract class DefaultPhase implements Phase {
|
---|
| 11 | // deadline for this phase, ms since 1970
|
---|
| 12 | protected final Long deadline;
|
---|
| 13 | protected final PartyStates partyStates;
|
---|
| 14 | protected final VotingEvaluatorWithValue evaluator;
|
---|
| 15 |
|
---|
| 16 | /**
|
---|
| 17 | * @param partyStates the {@link PartyStates}
|
---|
| 18 | * @param deadline deadline for this phase, ms since 1970
|
---|
| 19 | * @param evaluator the {@link VotingEvaluator} to be used
|
---|
| 20 | */
|
---|
| 21 | public DefaultPhase(PartyStates partyStates, Long deadline,
|
---|
| 22 | VotingEvaluatorWithValue evaluator) {
|
---|
| 23 | this.partyStates = partyStates;
|
---|
| 24 | this.deadline = deadline;
|
---|
| 25 | this.evaluator = evaluator;
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | @Override
|
---|
| 29 | public boolean isFinal(long now) {
|
---|
| 30 | return now >= deadline || partyStates.getNotYetActed().isEmpty();
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | @Override
|
---|
| 34 | public long getDeadline() {
|
---|
| 35 | return deadline;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | /**
|
---|
| 39 | * Check if actor can do given action. Basic checks:
|
---|
| 40 | * <ul>
|
---|
| 41 | * <li>real actor does not match action's actor
|
---|
| 42 | * <li>deadline for this phase has passed
|
---|
| 43 | * <li>action is not allowed in this phase
|
---|
| 44 | * <li>actor already acted in this phase
|
---|
| 45 | * </ul>
|
---|
| 46 | *
|
---|
| 47 | * @param actor the actor that really acted
|
---|
| 48 | * @param action the action that was done
|
---|
| 49 | * @param now current time
|
---|
| 50 | * @throws ProtocolException if the action violates the protocol
|
---|
| 51 | */
|
---|
| 52 | protected void checkAction(PartyId actor, Action action, long now)
|
---|
| 53 | throws ProtocolException {
|
---|
| 54 | if (action == null)
|
---|
| 55 | throw new ProtocolException("Action=null", actor);
|
---|
| 56 | if (!(actor.equals(action.getActor())))
|
---|
| 57 | throw new ProtocolException(
|
---|
| 58 | "Incorrect actor info in action:" + action.getActor(),
|
---|
| 59 | actor);
|
---|
| 60 | if (isFinal(now))
|
---|
| 61 | throw new ProtocolException("passed deadline", actor);
|
---|
| 62 | if (!(getAllowedActions().contains(action.getClass())))
|
---|
| 63 | throw new ProtocolException("Action not allowed in "
|
---|
| 64 | + this.getClass().getSimpleName() + ": " + action, actor);
|
---|
| 65 | if (!partyStates.getNotYetActed().contains(actor))
|
---|
| 66 | throw new ProtocolException("Actor can not act anymore", actor);
|
---|
| 67 | return;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | /**
|
---|
| 71 | * @return current PartyStates
|
---|
| 72 | */
|
---|
| 73 | @Override
|
---|
| 74 | public PartyStates getPartyStates() {
|
---|
| 75 | return partyStates;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | @Override
|
---|
| 79 | public VotingEvaluatorWithValue getEvaluator() {
|
---|
| 80 | return evaluator;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | @Override
|
---|
| 84 | public Phase next(long now, long duration) {
|
---|
| 85 | if (duration < PHASE_MINTIME || duration > PHASE_MAXTIME)
|
---|
| 86 | throw new IllegalArgumentException("Bug, illegal duration");
|
---|
| 87 | if (!isFinal(now))
|
---|
| 88 | throw new IllegalStateException("phase is not final");
|
---|
| 89 | return checkedNext(now + duration);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | /**
|
---|
| 93 | * As {@link #next(long, long)} but DefaultPhase already checked that 1.
|
---|
| 94 | * there is enough time for a next phase 2. current state is final 3.
|
---|
| 95 | *
|
---|
| 96 | * @param dl the deadline for the next phase (ms since 1970).
|
---|
| 97 | * @return the next phase
|
---|
| 98 | */
|
---|
| 99 | protected abstract Phase checkedNext(long dl);
|
---|
| 100 |
|
---|
| 101 | @Override
|
---|
| 102 | public String toString() {
|
---|
| 103 | return getClass().getSimpleName() + "[" + partyStates + "," + deadline
|
---|
| 104 | + "," + evaluator + "]";
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | @Override
|
---|
| 108 | public int hashCode() {
|
---|
| 109 | final int prime = 31;
|
---|
| 110 | int result = 1;
|
---|
| 111 | result = prime * result
|
---|
| 112 | + ((deadline == null) ? 0 : deadline.hashCode());
|
---|
| 113 | result = prime * result
|
---|
| 114 | + ((evaluator == null) ? 0 : evaluator.hashCode());
|
---|
| 115 | result = prime * result
|
---|
| 116 | + ((partyStates == null) ? 0 : partyStates.hashCode());
|
---|
| 117 | return result;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | @Override
|
---|
| 121 | public boolean equals(Object obj) {
|
---|
| 122 | if (this == obj)
|
---|
| 123 | return true;
|
---|
| 124 | if (obj == null)
|
---|
| 125 | return false;
|
---|
| 126 | if (getClass() != obj.getClass())
|
---|
| 127 | return false;
|
---|
| 128 | DefaultPhase other = (DefaultPhase) obj;
|
---|
| 129 | if (deadline == null) {
|
---|
| 130 | if (other.deadline != null)
|
---|
| 131 | return false;
|
---|
| 132 | } else if (!deadline.equals(other.deadline))
|
---|
| 133 | return false;
|
---|
| 134 | if (evaluator == null) {
|
---|
| 135 | if (other.evaluator != null)
|
---|
| 136 | return false;
|
---|
| 137 | } else if (!evaluator.equals(other.evaluator))
|
---|
| 138 | return false;
|
---|
| 139 | if (partyStates == null) {
|
---|
| 140 | if (other.partyStates != null)
|
---|
| 141 | return false;
|
---|
| 142 | } else if (!partyStates.equals(other.partyStates))
|
---|
| 143 | return false;
|
---|
| 144 | return true;
|
---|
| 145 | }
|
---|
| 146 | }
|
---|