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