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