source: protocol/src/main/java/geniusweb/protocol/session/SessionResult.java@ 46

Last change on this file since 46 was 46, checked in by bart, 2 years ago

refactor to help reusing partiesserver

File size: 4.8 KB
Line 
1package geniusweb.protocol.session;
2
3import java.util.Collections;
4import java.util.HashMap;
5import java.util.Map;
6
7import com.fasterxml.jackson.annotation.JsonAutoDetect;
8import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
9import com.fasterxml.jackson.annotation.JsonCreator;
10import com.fasterxml.jackson.annotation.JsonProperty;
11import com.fasterxml.jackson.annotation.JsonTypeInfo;
12
13import geniusweb.actions.PartyId;
14import geniusweb.inform.Agreements;
15import geniusweb.references.PartyWithProfile;
16
17/**
18 * All results collected from running a session. Normally this does not contain
19 * all the details, only the final outcome and other global notes.
20 */
21@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
22public class SessionResult {
23 private final Map<PartyId, PartyWithProfile> participants;
24 private final Agreements agreements;
25 private final Map<PartyId, Double> penalties;
26
27 // add more type info so that jackson can deserialize the actual class
28 @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.WRAPPER_OBJECT)
29 private final Throwable error;
30
31 /**
32 *
33 * @param participants the list oof {@link PartyWithProfile}. Should never
34 * be null. Some of them may have entered later of left
35 * early. This list should contain them all.
36 * @param agreements the final Agreements.
37 * @param penalties the penalties in [0,1] for each participant.
38 * @param error a fatal error that terminated the session. Non-fatal
39 * errors (warnings) are not to be reported. Null if no
40 * fatal error occured (session ended following the
41 * protocol). Normally, a fatal error results in no
42 * agreement but there might be protocols that allow a
43 * session to end with an error but stick with a already
44 * reached agreement as well.
45 */
46 @JsonCreator
47 public SessionResult(
48 @JsonProperty("participants") Map<PartyId, PartyWithProfile> participants,
49 @JsonProperty("agreements") Agreements agreements,
50 @JsonProperty("penalties") Map<PartyId, Double> penalties,
51 @JsonProperty("error") Throwable error) {
52 this.participants = new HashMap<>(participants);
53 this.agreements = agreements;
54 this.penalties = new HashMap<>(penalties);
55 this.error = error;
56 }
57
58 /**
59 *
60 * @return the map with for each {@link PartyId} the
61 * {@link PartyWithProfile}. Should never be null. Some of them may
62 * have entered later of left early. This list should contain them
63 * all.
64 */
65 public Map<PartyId, PartyWithProfile> getParticipants() {
66 return Collections.unmodifiableMap(participants);
67 }
68
69 /**
70 * @return the final {@link Agreements} of the session. May be empty, not
71 * null
72 */
73 public Agreements getAgreements() {
74 return agreements;
75 };
76
77 /**
78 *
79 * @return Map of penalties,
80 */
81 public Map<PartyId, Double> getPenalties() {
82 return Collections.unmodifiableMap(penalties);
83 }
84
85 /**
86 *
87 * @return a fatal error that terminated the session. Non-fatal errors
88 * (warnings) are not to be reported. Null if no fatal error occured
89 * (session ended following the protocol). Normally, a fatal error
90 * results in no agreement but there might be protocols that allow a
91 * session to end with an error but stick with a already reached
92 * agreement as well.
93 */
94 public Throwable getError() {
95 return error;
96 }
97
98 @Override
99 public String toString() {
100 return "SessionResult[" + participants + "," + agreements + ","
101 + penalties + "," + error + "]";
102 }
103
104 @Override
105 public int hashCode() {
106 final int prime = 31;
107 int result = 1;
108 result = prime * result
109 + ((agreements == null) ? 0 : agreements.hashCode());
110 result = prime * result + ((error == null) ? 0 : error.hashCode());
111 result = prime * result
112 + ((participants == null) ? 0 : participants.hashCode());
113 result = prime * result
114 + ((penalties == null) ? 0 : penalties.hashCode());
115 return result;
116 }
117
118 @Override
119 public boolean equals(Object obj) {
120 if (this == obj)
121 return true;
122 if (obj == null)
123 return false;
124 if (getClass() != obj.getClass())
125 return false;
126 SessionResult other = (SessionResult) obj;
127 if (agreements == null) {
128 if (other.agreements != null)
129 return false;
130 } else if (!agreements.equals(other.agreements))
131 return false;
132 if (error == null) {
133 if (other.error != null)
134 return false;
135 } else if (!error.equals(other.error))
136 return false;
137 if (participants == null) {
138 if (other.participants != null)
139 return false;
140 } else if (!participants.equals(other.participants))
141 return false;
142 if (penalties == null) {
143 if (other.penalties != null)
144 return false;
145 } else if (!penalties.equals(other.penalties))
146 return false;
147 return true;
148 }
149
150}
Note: See TracBrowser for help on using the repository browser.