1 | package geniusweb.issuevalue;
|
---|
2 |
|
---|
3 | import java.util.Collections;
|
---|
4 | import java.util.HashMap;
|
---|
5 | import java.util.HashSet;
|
---|
6 | import java.util.Map;
|
---|
7 | import java.util.Set;
|
---|
8 |
|
---|
9 | import org.eclipse.jdt.annotation.NonNull;
|
---|
10 |
|
---|
11 | import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
---|
12 | import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
---|
13 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
14 | import com.fasterxml.jackson.annotation.JsonProperty;
|
---|
15 |
|
---|
16 | @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
|
---|
17 | /**
|
---|
18 | * Contains a (possibly partial) bid as a set of issue-value pairs. Both issue
|
---|
19 | * and value are never null.Immutable, thread safe.
|
---|
20 | */
|
---|
21 | public class Bid {
|
---|
22 | private final @NonNull Map<@NonNull String, @NonNull Value> issuevalues;
|
---|
23 |
|
---|
24 | @SuppressWarnings("unused")
|
---|
25 | @JsonCreator
|
---|
26 |
|
---|
27 | /**
|
---|
28 | *
|
---|
29 | * @param issuevalues a map of issue, Value pairs. The String is the issue
|
---|
30 | * name. All issues in the map must have non-null values.
|
---|
31 | * A bid may be partial: there may be known issues that
|
---|
32 | * are not in the map.
|
---|
33 | */
|
---|
34 | public Bid(
|
---|
35 | @JsonProperty("issuevalues") @NonNull Map<@NonNull String, @NonNull Value> issuevalues) {
|
---|
36 | if (issuevalues == null) {
|
---|
37 | throw new NullPointerException("issuevalues=null");
|
---|
38 | }
|
---|
39 | for (final @NonNull String issue : issuevalues.keySet()) {
|
---|
40 | if (issuevalues.get(issue) == null) {
|
---|
41 | throw new IllegalArgumentException(
|
---|
42 | "value of issue " + issue + " can not be null");
|
---|
43 | }
|
---|
44 | }
|
---|
45 | this.issuevalues = new HashMap<>(issuevalues);
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Makes partial bid with just 1 issue and value.
|
---|
50 | *
|
---|
51 | * @param issue the issue name
|
---|
52 | * @param value the {@link Value}
|
---|
53 | */
|
---|
54 | public Bid(@NonNull String issue, @NonNull Value value) {
|
---|
55 | if (issue == null || value == null) {
|
---|
56 | throw new NullPointerException("issue and value must be not =null");
|
---|
57 | }
|
---|
58 |
|
---|
59 | issuevalues = new HashMap<>();
|
---|
60 | issuevalues.put(issue, value);
|
---|
61 | }
|
---|
62 |
|
---|
63 | @NonNull
|
---|
64 | /**
|
---|
65 | * @param issue name of the issue
|
---|
66 | * @return the value for the given issue, or null if there is no value for
|
---|
67 | * the given issue.
|
---|
68 | */
|
---|
69 | public Value getValue(@NonNull String issue) {
|
---|
70 | return issuevalues.get(issue);
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * @param issue name of the issue (not null)
|
---|
75 | * @return true iff the bid contains a value for the given issue.
|
---|
76 | */
|
---|
77 | public boolean containsIssue(@NonNull String issue) {
|
---|
78 | return issuevalues.containsKey(issue);
|
---|
79 | }
|
---|
80 |
|
---|
81 | @NonNull
|
---|
82 | public Set<@NonNull String> getIssues() {
|
---|
83 | return Collections.unmodifiableSet(issuevalues.keySet());
|
---|
84 | }
|
---|
85 |
|
---|
86 | @NonNull
|
---|
87 | /**
|
---|
88 | * Merges this partial bid with another partial bid.
|
---|
89 | *
|
---|
90 | * @param otherbid another partial bid (not null).
|
---|
91 | * @return a bid with the combined values of both partial bids.
|
---|
92 | * @throws IllegalArgumentException if issues overlap.
|
---|
93 | */
|
---|
94 | public Bid merge(final @NonNull Bid otherbid) {
|
---|
95 | final @NonNull Set<@NonNull String> ourissues = new HashSet<>(
|
---|
96 | issuevalues.keySet());
|
---|
97 | ourissues.retainAll(otherbid.getIssues());
|
---|
98 | if (!ourissues.isEmpty()) {
|
---|
99 | throw new IllegalArgumentException(
|
---|
100 | "Otherbid contains issues that are already set:"
|
---|
101 | + ourissues.toString());
|
---|
102 | }
|
---|
103 | final @NonNull HashMap<@NonNull String, @NonNull Value> newvalues = new HashMap<>(
|
---|
104 | issuevalues);
|
---|
105 | newvalues.putAll(otherbid.issuevalues);
|
---|
106 | return new Bid(newvalues);
|
---|
107 | }
|
---|
108 |
|
---|
109 | @NonNull
|
---|
110 | /**
|
---|
111 | *
|
---|
112 | * @return (unmodifyable) map with all issues and values in the bid.
|
---|
113 | */
|
---|
114 | public Map<@NonNull String, @NonNull Value> getIssueValues() {
|
---|
115 | return Collections.unmodifiableMap(issuevalues);
|
---|
116 | }
|
---|
117 |
|
---|
118 | @Override
|
---|
119 | public @NonNull String toString() {
|
---|
120 | return "Bid" + issuevalues.toString();
|
---|
121 | }
|
---|
122 |
|
---|
123 | @Override
|
---|
124 | public int hashCode() {
|
---|
125 | final int prime = 31;
|
---|
126 | int result = 1;
|
---|
127 | result = prime * result
|
---|
128 | + ((issuevalues == null) ? 0 : issuevalues.hashCode());
|
---|
129 | return result;
|
---|
130 | }
|
---|
131 |
|
---|
132 | @Override
|
---|
133 | public boolean equals(Object obj) {
|
---|
134 | if (this == obj)
|
---|
135 | return true;
|
---|
136 | if (obj == null)
|
---|
137 | return false;
|
---|
138 | if (getClass() != obj.getClass())
|
---|
139 | return false;
|
---|
140 | Bid other = (Bid) obj;
|
---|
141 | if (issuevalues == null) {
|
---|
142 | if (other.issuevalues != null)
|
---|
143 | return false;
|
---|
144 | } else if (!issuevalues.equals(other.issuevalues))
|
---|
145 | return false;
|
---|
146 | return true;
|
---|
147 | }
|
---|
148 |
|
---|
149 | } |
---|