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