source: protocol/src/main/java/geniusweb/protocol/session/shaop/BareSHAOPState.java@ 21

Last change on this file since 21 was 21, checked in by bart, 4 years ago

Version 1.5.

File size: 7.0 KB
Line 
1package geniusweb.protocol.session.shaop;
2
3import java.util.Arrays;
4import java.util.Collections;
5import java.util.LinkedList;
6import java.util.List;
7import java.util.Map;
8
9import com.fasterxml.jackson.annotation.JsonCreator;
10
11import geniusweb.actions.Action;
12import geniusweb.actions.PartyId;
13import geniusweb.progress.Progress;
14import geniusweb.protocol.ProtocolException;
15import geniusweb.protocol.partyconnection.ProtocolToPartyConnections;
16import geniusweb.protocol.session.SessionResult;
17import geniusweb.protocol.session.SessionSettings;
18import geniusweb.protocol.session.SessionState;
19import geniusweb.references.PartyWithProfile;
20
21/**
22 * The state without all the "with" functioality to make this more readable.
23 * immutable. json-serializable.
24 */
25public abstract class BareSHAOPState implements SessionState {
26
27 protected final List<Action> actions;
28
29 protected final transient ProtocolToPartyConnections connections;
30
31 protected final Progress progress;
32
33 protected final SHAOPSettings settings;
34
35 protected final ProtocolException error;
36
37 protected final int teamNr;
38
39 protected final Map<PartyId, Integer> partyNumbers;
40
41 protected final Map<PartyId, Double> totalSpent;
42
43 /**
44 *
45 * @param actions value for actions done so far. null equals to empty
46 * list
47 * @param conns the currently existing connections. Can be empty/null.
48 * Each connection represents another party. Normally the
49 * connections are in the order SHAOP1,COB1,SHAOP2,COB2,..
50 * so 2 parties for each team. The protocol should check
51 * this.
52 * @param progr the {@link Progress} that governs this session. Can be
53 * null if session did not yet start.
54 * @param settings the settings used for the session
55 * @param e the exception that occured, usually null. All errors
56 * occuring due to faulty {@link Action}s translate to
57 * {@link ProtocolException}s. All errors in our own code
58 * are bugs (not ProtocolExceptions) and should result in
59 * a throw.
60 * @param teamNr the teamnr (0,1,2..) that has the turn. 2* gives index
61 * {@link #connections} and settings
62 * @param partyNrs a map for each known PartyId to a number. The number is
63 * the index in both {@link SHAOPSettings#getTeams()} and
64 * in {@link #connections}. null is empyt map.
65 * @param totalSpent total accumulated elicitation costs so far. only SHAOP
66 * parties accumulate costs. null = empty map
67 */
68 @JsonCreator
69 public BareSHAOPState(List<Action> actions,
70 ProtocolToPartyConnections conns, Progress progr,
71 SHAOPSettings settings, ProtocolException e, int teamNr,
72 Map<PartyId, Integer> partyNrs, Map<PartyId, Double> spent) {
73 if (conns == null) {
74 this.connections = new ProtocolToPartyConnections(
75 Collections.emptyList());
76 } else {
77 this.connections = conns;
78 }
79 if (actions == null) {
80 this.actions = new LinkedList<>();
81 } else {
82 this.actions = actions;
83 }
84 if (partyNrs == null) {
85 this.partyNumbers = Collections.emptyMap();
86 } else {
87 this.partyNumbers = partyNrs;
88 }
89 if (spent == null) {
90 this.totalSpent = Collections.emptyMap();
91 } else {
92 this.totalSpent = spent;
93 }
94
95 if (!connections.allunique()) {
96 throw new IllegalArgumentException(
97 "There can not be multiple connections for a party:"
98 + conns);
99 }
100
101 this.progress = progr;
102 this.settings = settings;
103 this.error = e;
104 this.teamNr = teamNr;
105 }
106
107 public ProtocolToPartyConnections getConnections() {
108 return connections;
109 }
110
111 /**
112 *
113 * @return party ID of current team leader (SHAOP party)
114 */
115 public PartyId getCurrentTeam() {
116 return connections.get(2 * teamNr).getParty();
117 }
118
119 /**
120 * @param id the {@link PartyId}
121 * @return the PartyWithProfile associated with that id.
122 */
123 public PartyWithProfile getPartyProfile(PartyId id) {
124 return settings.getAllParties().get(partyNumbers.get(id));
125 }
126
127 /**
128 *
129 * @return the index of each party
130 */
131 public Map<PartyId, Integer> getPartyNumbers() {
132 return Collections.unmodifiableMap(partyNumbers);
133 }
134
135 /**
136 *
137 * @return unmodifyable list of actions done so far.
138 */
139 @Override
140 public List<Action> getActions() {
141 return Collections.unmodifiableList(actions);
142 }
143
144 @Override
145 public Progress getProgress() {
146 return progress;
147 }
148
149 @Override
150 public SessionSettings getSettings() {
151 return settings;
152 }
153
154 @Override
155 public boolean isFinal(long currentTimeMs) {
156 return error != null
157 || (progress != null && progress.isPastDeadline(currentTimeMs));
158 }
159
160 public ProtocolException getError() {
161 return error;
162 }
163
164 @Override
165 public String toString() {
166 return this.getClass().getSimpleName() + "[" + actions + ","
167 + connections + "," + progress + "," + settings + "," + error
168 + "]";
169 }
170
171 @Override
172 public SessionResult getResult() {
173 Double[] penalties = new Double[partyNumbers.size()];
174 for (PartyId party : partyNumbers.keySet()) {
175 Double spent = totalSpent.get(party);
176 if (spent == null) {
177 spent = 0d;
178 } else {
179 spent = Math.max(0, Math.min(1.0, spent));
180 }
181 penalties[partyNumbers.get(party)] = spent;
182 }
183 return new SessionResult(getSettings().getAllParties(), getAgreements(),
184 Arrays.asList(penalties), getError());
185
186 }
187
188 @Override
189 public int hashCode() {
190 final int prime = 31;
191 int result = 1;
192 result = prime * result + ((actions == null) ? 0 : actions.hashCode());
193 result = prime * result + ((error == null) ? 0 : error.hashCode());
194 result = prime * result
195 + ((partyNumbers == null) ? 0 : partyNumbers.hashCode());
196 result = prime * result
197 + ((progress == null) ? 0 : progress.hashCode());
198 result = prime * result
199 + ((settings == null) ? 0 : settings.hashCode());
200 result = prime * result + teamNr;
201 result = prime * result
202 + ((totalSpent == null) ? 0 : totalSpent.hashCode());
203 return result;
204 }
205
206 @Override
207 public boolean equals(Object obj) {
208 if (this == obj)
209 return true;
210 if (obj == null)
211 return false;
212 if (getClass() != obj.getClass())
213 return false;
214 BareSHAOPState other = (BareSHAOPState) obj;
215 if (actions == null) {
216 if (other.actions != null)
217 return false;
218 } else if (!actions.equals(other.actions))
219 return false;
220 if (error == null) {
221 if (other.error != null)
222 return false;
223 } else if (!error.equals(other.error))
224 return false;
225 if (partyNumbers == null) {
226 if (other.partyNumbers != null)
227 return false;
228 } else if (!partyNumbers.equals(other.partyNumbers))
229 return false;
230 if (progress == null) {
231 if (other.progress != null)
232 return false;
233 } else if (!progress.equals(other.progress))
234 return false;
235 if (settings == null) {
236 if (other.settings != null)
237 return false;
238 } else if (!settings.equals(other.settings))
239 return false;
240 if (teamNr != other.teamNr)
241 return false;
242 if (totalSpent == null) {
243 if (other.totalSpent != null)
244 return false;
245 } else if (!totalSpent.equals(other.totalSpent))
246 return false;
247 return true;
248 }
249
250}
Note: See TracBrowser for help on using the repository browser.