1 | package geniusweb.protocol.session.saop;
|
---|
2 |
|
---|
3 | import java.util.Collections;
|
---|
4 | import java.util.LinkedList;
|
---|
5 | import java.util.List;
|
---|
6 | import java.util.stream.Collectors;
|
---|
7 |
|
---|
8 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
9 | import com.fasterxml.jackson.annotation.JsonProperty;
|
---|
10 |
|
---|
11 | import geniusweb.deadline.Deadline;
|
---|
12 | import geniusweb.protocol.session.SessionProtocol;
|
---|
13 | import geniusweb.protocol.session.SessionSettings;
|
---|
14 | import geniusweb.protocol.session.TeamInfo;
|
---|
15 | import geniusweb.references.PartyWithProfile;
|
---|
16 | import tudelft.utilities.logging.Reporter;
|
---|
17 |
|
---|
18 | public class SAOPSettings implements SessionSettings {
|
---|
19 | private final List<TeamInfo> participants;
|
---|
20 | private final Deadline deadline;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | *
|
---|
24 | * @param participants the list of {@link PartyWithProfile} in clockwise
|
---|
25 | * order. There must be at least 2 to run the SAOP
|
---|
26 | * protocol. But SAOP can be initialized with less, for
|
---|
27 | * use in TournamentSettings.
|
---|
28 | * @param deadline the deadline of the negotiation
|
---|
29 | */
|
---|
30 | @JsonCreator
|
---|
31 | public SAOPSettings(
|
---|
32 | @JsonProperty("participants") List<TeamInfo> participants,
|
---|
33 | @JsonProperty("deadline") Deadline deadline) {
|
---|
34 | this.participants = participants;
|
---|
35 | this.deadline = deadline;
|
---|
36 | if (participants == null) {
|
---|
37 | throw new IllegalArgumentException("participants must not be null");
|
---|
38 | }
|
---|
39 | if (deadline == null) {
|
---|
40 | throw new IllegalArgumentException("deadline must not be null");
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Override
|
---|
45 | public Double getMaxRunTime() {
|
---|
46 | return deadline.getDuration() / 1000d;
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | public SessionProtocol getProtocol(Reporter logger) {
|
---|
51 | return new SAOP(new SAOPState(this), logger);
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public List<TeamInfo> getTeams() {
|
---|
56 | return Collections.unmodifiableList(participants);
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * @return the deadline for this negotiation
|
---|
61 | */
|
---|
62 | public Deadline getDeadline() {
|
---|
63 | return deadline;
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Override
|
---|
67 | public List<PartyWithProfile> getAllParties() {
|
---|
68 | return participants.stream()
|
---|
69 | .map(particip -> particip.getParties().get(0))
|
---|
70 | .collect(Collectors.toList());
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | public Integer getTeamSize() {
|
---|
75 | return 1;
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Override
|
---|
79 | public String toString() {
|
---|
80 | return "SAOPSettings" + participants + "," + deadline + "]";
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Override
|
---|
84 | public int hashCode() {
|
---|
85 | final int prime = 31;
|
---|
86 | int result = 1;
|
---|
87 | result = prime * result
|
---|
88 | + ((deadline == null) ? 0 : deadline.hashCode());
|
---|
89 | result = prime * result
|
---|
90 | + ((participants == null) ? 0 : participants.hashCode());
|
---|
91 | return result;
|
---|
92 | }
|
---|
93 |
|
---|
94 | @Override
|
---|
95 | public boolean equals(Object obj) {
|
---|
96 | if (this == obj)
|
---|
97 | return true;
|
---|
98 | if (obj == null)
|
---|
99 | return false;
|
---|
100 | if (getClass() != obj.getClass())
|
---|
101 | return false;
|
---|
102 | SAOPSettings other = (SAOPSettings) obj;
|
---|
103 | if (deadline == null) {
|
---|
104 | if (other.deadline != null)
|
---|
105 | return false;
|
---|
106 | } else if (!deadline.equals(other.deadline))
|
---|
107 | return false;
|
---|
108 | if (participants == null) {
|
---|
109 | if (other.participants != null)
|
---|
110 | return false;
|
---|
111 | } else if (!participants.equals(other.participants))
|
---|
112 | return false;
|
---|
113 | return true;
|
---|
114 | }
|
---|
115 |
|
---|
116 | @Override
|
---|
117 | public SessionSettings with(TeamInfo team) {
|
---|
118 | if (team.getSize() != 1)
|
---|
119 | throw new IllegalArgumentException(
|
---|
120 | "Team must be size 1 but found " + team);
|
---|
121 | List<TeamInfo> newparts = new LinkedList<>(participants);
|
---|
122 | newparts.add(team);
|
---|
123 | return new SAOPSettings(newparts, deadline);
|
---|
124 | }
|
---|
125 |
|
---|
126 | }
|
---|