package geniusweb.protocol.session; import java.util.Collections; import java.util.HashMap; import java.util.Map; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonTypeInfo; import geniusweb.actions.PartyId; import geniusweb.inform.Agreements; import geniusweb.references.PartyWithProfile; /** * All results collected from running a session. Normally this does not contain * all the details, only the final outcome and other global notes. */ @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE) public class SessionResult { private final Map participants; private final Agreements agreements; private final Map penalties; // add more type info so that jackson can deserialize the actual class @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.WRAPPER_OBJECT) private final Throwable error; /** * * @param participants the list oof {@link PartyWithProfile}. Should never * be null. Some of them may have entered later of left * early. This list should contain them all. * @param agreements the final Agreements. The key is the party ID, value * is the bid that the party agreed on. Only parties * that reached an agreement are in this list. * @param penalties Key is the party ID, the value is the penalties in * [0,1] for that participant. A penalty only applies to * an agreement, not to a reservation bid. * @param error a fatal error that terminated the session. Non-fatal * errors (warnings) are not to be reported. Null if no * fatal error occured (session ended following the * protocol). Normally, a fatal error results in no * agreement but there might be protocols that allow a * session to end with an error but stick with a already * reached agreement as well. */ @JsonCreator public SessionResult( @JsonProperty("participants") Map participants, @JsonProperty("agreements") Agreements agreements, @JsonProperty("penalties") Map penalties, @JsonProperty("error") Throwable error) { this.participants = new HashMap<>(participants); this.agreements = agreements; this.penalties = new HashMap<>(penalties); this.error = error; } /** * * @return the map with for each {@link PartyId} the * {@link PartyWithProfile}. Should never be null. Some of them may * have entered later of left early. This list should contain them * all. */ public Map getParticipants() { return Collections.unmodifiableMap(participants); } /** * @return the final {@link Agreements} of the session. May be empty, not * null */ public Agreements getAgreements() { return agreements; }; /** * * @return Map of penalties, */ public Map getPenalties() { return Collections.unmodifiableMap(penalties); } /** * * @return a fatal error that terminated the session. Non-fatal errors * (warnings) are not to be reported. Null if no fatal error occured * (session ended following the protocol). Normally, a fatal error * results in no agreement but there might be protocols that allow a * session to end with an error but stick with a already reached * agreement as well. */ public Throwable getError() { return error; } @Override public String toString() { return "SessionResult[" + participants + "," + agreements + "," + penalties + "," + error + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((agreements == null) ? 0 : agreements.hashCode()); result = prime * result + ((error == null) ? 0 : error.hashCode()); result = prime * result + ((participants == null) ? 0 : participants.hashCode()); result = prime * result + ((penalties == null) ? 0 : penalties.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; SessionResult other = (SessionResult) obj; if (agreements == null) { if (other.agreements != null) return false; } else if (!agreements.equals(other.agreements)) return false; if (error == null) { if (other.error != null) return false; } else if (!error.equals(other.error)) return false; if (participants == null) { if (other.participants != null) return false; } else if (!participants.equals(other.participants)) return false; if (penalties == null) { if (other.penalties != null) return false; } else if (!penalties.equals(other.penalties)) return false; return true; } }