1 | package geniusweb.inform;
|
---|
2 |
|
---|
3 | import java.util.Collections;
|
---|
4 | import java.util.HashMap;
|
---|
5 | import java.util.Map;
|
---|
6 | import java.util.Set;
|
---|
7 | import java.util.stream.Collectors;
|
---|
8 |
|
---|
9 | import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
---|
10 | import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
---|
11 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
12 | import com.fasterxml.jackson.annotation.JsonValue;
|
---|
13 |
|
---|
14 | import geniusweb.actions.PartyId;
|
---|
15 | import geniusweb.issuevalue.Bid;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * There can be multiple agreements from a single negotiation. This object
|
---|
19 | * contains the agreements. Each party can agree only on 1 bid and there are
|
---|
20 | * always at least 2 parties agreeing on a bid.
|
---|
21 | */
|
---|
22 | @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
|
---|
23 | public class Agreements {
|
---|
24 | @JsonValue
|
---|
25 | private final Map<PartyId, Bid> agreements;
|
---|
26 |
|
---|
27 | public Agreements() {
|
---|
28 | this.agreements = Collections.emptyMap();
|
---|
29 | }
|
---|
30 |
|
---|
31 | @JsonCreator
|
---|
32 | public Agreements(Map<PartyId, Bid> agrees) {
|
---|
33 | this.agreements = agrees;
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * @param bid a bid that all parties agreed on
|
---|
38 | * @param parties the {@link PartyId}s of the agreeing parties
|
---|
39 | */
|
---|
40 | public Agreements(Bid bid, Set<PartyId> parties) {
|
---|
41 | this(parties.stream()
|
---|
42 | .collect(Collectors.toMap(pid -> pid, pid -> bid)));
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * @param other {@link Agreements}
|
---|
47 | * @return a new Agreement containing this plus other agreements
|
---|
48 | * @throws IllegalArgumentException if parties in other are already in this
|
---|
49 | * agreement.
|
---|
50 | */
|
---|
51 | public Agreements with(Agreements other) {
|
---|
52 | Map<PartyId, Bid> newagrees = new HashMap<>(agreements);
|
---|
53 | for (PartyId pid : other.agreements.keySet()) {
|
---|
54 | if (newagrees.containsKey(pid))
|
---|
55 | throw new IllegalArgumentException(
|
---|
56 | "party " + pid + " already has agreement");
|
---|
57 | newagrees.put(pid, other.agreements.get(pid));
|
---|
58 | }
|
---|
59 | return new Agreements(newagrees);
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * @return actual agreemenets contained here
|
---|
64 | */
|
---|
65 | public Map<PartyId, Bid> getMap() {
|
---|
66 | return Collections.unmodifiableMap(agreements);
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | public String toString() {
|
---|
71 | return "Agreements" + agreements;
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public int hashCode() {
|
---|
76 | final int prime = 31;
|
---|
77 | int result = 1;
|
---|
78 | result = prime * result
|
---|
79 | + ((agreements == null) ? 0 : agreements.hashCode());
|
---|
80 | return result;
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Override
|
---|
84 | public boolean equals(Object obj) {
|
---|
85 | if (this == obj)
|
---|
86 | return true;
|
---|
87 | if (obj == null)
|
---|
88 | return false;
|
---|
89 | if (getClass() != obj.getClass())
|
---|
90 | return false;
|
---|
91 | Agreements other = (Agreements) obj;
|
---|
92 | if (agreements == null) {
|
---|
93 | if (other.agreements != null)
|
---|
94 | return false;
|
---|
95 | } else if (!agreements.equals(other.agreements))
|
---|
96 | return false;
|
---|
97 | return true;
|
---|
98 | }
|
---|
99 |
|
---|
100 | }
|
---|