package geniusweb.protocol.session.shaop; import java.util.ArrayList; import java.util.Collection; import java.util.LinkedList; import java.util.List; import java.util.stream.Collectors; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import geniusweb.deadline.Deadline; import geniusweb.protocol.session.SessionProtocol; import geniusweb.protocol.session.SessionSettings; import geniusweb.protocol.session.TeamInfo; import geniusweb.references.PartyWithProfile; import tudelft.utilities.logging.Reporter; public class SHAOPSettings implements SessionSettings { private static final int SHAOP = 0; private static final int COB = 1; private final List participants; private final Deadline deadline; private final transient List allParties = new LinkedList<>(); /** * @param teams the {@link TeamInfo}s each containing the shaop parties * and their partner cob, in that order. There must be at * least 2 {@link TeamInfo}s to run the SHAOP protocol. But * SHAOP can be initialized with less, for use in * TournamentSettings. * @param deadline the deadline settings for the session */ @JsonCreator public SHAOPSettings(@JsonProperty("participants") List teams, @JsonProperty("deadline") Deadline deadline) { if (teams == null) throw new IllegalArgumentException("participants must not be null"); if (deadline == null) throw new IllegalArgumentException("deadline must not be null"); this.participants = teams; this.deadline = deadline; // notice, order here is crucial because of way #isShaopParty works. for (TeamInfo team : teams) { if (team.getSize() != 2) throw new IllegalArgumentException( "Team must be size 2 but found " + team); allParties.add(team.getParties().get(SHAOP)); allParties.add(team.getParties().get(COB)); } } @Override public Double getMaxRunTime() { return deadline.getDuration() / 1000d; } @Override public SessionProtocol getProtocol(Reporter logger) { return new SHAOP(new SHAOPState(this), logger); } @Override public List getTeams() { // cast needed... return new ArrayList(participants); } /** * @return the deadline for this negotiation */ public Deadline getDeadline() { return deadline; } /** * * @return list of all parties, the even parties (0=first element in list) * being the SHAOP party and the next in the list the partner COB * party. */ @Override public List getAllParties() { return participants.stream().map(team -> team.getParties()) .flatMap(Collection::stream).collect(Collectors.toList()); } @Override public SessionSettings with(TeamInfo team) { List newparts = new LinkedList<>(participants); newparts.add(team); return new SHAOPSettings(newparts, deadline); } @Override public Integer getTeamSize() { return 2; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((deadline == null) ? 0 : deadline.hashCode()); result = prime * result + ((participants == null) ? 0 : participants.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; SHAOPSettings other = (SHAOPSettings) obj; if (deadline == null) { if (other.deadline != null) return false; } else if (!deadline.equals(other.deadline)) return false; if (participants == null) { if (other.participants != null) return false; } else if (!participants.equals(other.participants)) return false; return true; } @Override public String toString() { return "SHAOPSettings" + participants + "," + deadline + "]"; } }