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 | @NonNull
|
---|
96 | Set<@NonNull String> ourissues = new HashSet<>(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 | @NonNull
|
---|
104 | HashMap<@NonNull String, @NonNull Value> newvalues = new HashMap<>(
|
---|
105 | issuevalues);
|
---|
106 | newvalues.putAll(otherbid.issuevalues);
|
---|
107 | return new Bid(newvalues);
|
---|
108 | }
|
---|
109 |
|
---|
110 | @NonNull
|
---|
111 | /**
|
---|
112 | *
|
---|
113 | * @return (unmodifyable) map with all issues and values in the bid.
|
---|
114 | */
|
---|
115 | public Map<@NonNull String, @NonNull Value> getIssueValues() {
|
---|
116 | return Collections.unmodifiableMap(issuevalues);
|
---|
117 | }
|
---|
118 |
|
---|
119 | @Override
|
---|
120 | public @NonNull String toString() {
|
---|
121 | return "Bid" + issuevalues.toString();
|
---|
122 | }
|
---|
123 |
|
---|
124 | @Override
|
---|
125 | public int hashCode() {
|
---|
126 | final int prime = 31;
|
---|
127 | int result = 1;
|
---|
128 | result = prime * result
|
---|
129 | + ((issuevalues == null) ? 0 : issuevalues.hashCode());
|
---|
130 | return result;
|
---|
131 | }
|
---|
132 |
|
---|
133 | @Override
|
---|
134 | public boolean equals(Object obj) {
|
---|
135 | if (this == obj)
|
---|
136 | return true;
|
---|
137 | if (obj == null)
|
---|
138 | return false;
|
---|
139 | if (getClass() != obj.getClass())
|
---|
140 | return false;
|
---|
141 | Bid other = (Bid) obj;
|
---|
142 | if (issuevalues == null) {
|
---|
143 | if (other.issuevalues != null)
|
---|
144 | return false;
|
---|
145 | } else if (!issuevalues.equals(other.issuevalues))
|
---|
146 | return false;
|
---|
147 | return true;
|
---|
148 | }
|
---|
149 |
|
---|
150 | } |
---|