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