[519] | 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 |
|
---|
[804] | 9 | import org.eclipse.jdt.annotation.NonNull;
|
---|
| 10 |
|
---|
[519] | 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 |
|
---|
[825] | 16 | @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
|
---|
[519] | 17 | /**
|
---|
[825] | 18 | * Contains a (possibly partial) bid as a set of issue-value pairs. Both issue
|
---|
| 19 | * and value are never null.Immutable, thread safe.
|
---|
[519] | 20 | */
|
---|
| 21 | public class Bid {
|
---|
[805] | 22 | private final @NonNull Map<@NonNull String, @NonNull Value> issuevalues;
|
---|
[519] | 23 |
|
---|
[825] | 24 | @SuppressWarnings("unused")
|
---|
| 25 | @JsonCreator
|
---|
| 26 |
|
---|
[519] | 27 | /**
|
---|
| 28 | *
|
---|
| 29 | * @param issuevalues a map of issue, Value pairs. The String is the issue
|
---|
[814] | 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.
|
---|
[519] | 33 | */
|
---|
[804] | 34 | public Bid(
|
---|
| 35 | @JsonProperty("issuevalues") @NonNull Map<@NonNull String, @NonNull Value> issuevalues) {
|
---|
[519] | 36 | if (issuevalues == null) {
|
---|
| 37 | throw new NullPointerException("issuevalues=null");
|
---|
| 38 | }
|
---|
[805] | 39 | for (final @NonNull String issue : issuevalues.keySet()) {
|
---|
[519] | 40 | if (issuevalues.get(issue) == null) {
|
---|
| 41 | throw new IllegalArgumentException(
|
---|
| 42 | "value of issue " + issue + " can not be null");
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
[814] | 45 | this.issuevalues = new HashMap<>(issuevalues);
|
---|
[519] | 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 | */
|
---|
[804] | 54 | public Bid(@NonNull String issue, @NonNull Value value) {
|
---|
[519] | 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 |
|
---|
[825] | 63 | @NonNull
|
---|
[519] | 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 | */
|
---|
[825] | 69 | public Value getValue(@NonNull String issue) {
|
---|
[519] | 70 | return issuevalues.get(issue);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | /**
|
---|
[804] | 74 | * @param issue name of the issue (not null)
|
---|
[519] | 75 | * @return true iff the bid contains a value for the given issue.
|
---|
| 76 | */
|
---|
[804] | 77 | public boolean containsIssue(@NonNull String issue) {
|
---|
[519] | 78 | return issuevalues.containsKey(issue);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[825] | 81 | @NonNull
|
---|
| 82 | public Set<@NonNull String> getIssues() {
|
---|
[519] | 83 | return Collections.unmodifiableSet(issuevalues.keySet());
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[825] | 86 | @NonNull
|
---|
[519] | 87 | /**
|
---|
| 88 | * Merges this partial bid with another partial bid.
|
---|
| 89 | *
|
---|
[804] | 90 | * @param otherbid another partial bid (not null).
|
---|
[519] | 91 | * @return a bid with the combined values of both partial bids.
|
---|
| 92 | * @throws IllegalArgumentException if issues overlap.
|
---|
| 93 | */
|
---|
[825] | 94 | public Bid merge(final @NonNull Bid otherbid) {
|
---|
[804] | 95 | @NonNull
|
---|
| 96 | Set<@NonNull String> ourissues = new HashSet<>(issuevalues.keySet());
|
---|
[519] | 97 | ourissues.retainAll(otherbid.getIssues());
|
---|
| 98 | if (!ourissues.isEmpty()) {
|
---|
| 99 | throw new IllegalArgumentException(
|
---|
| 100 | "Otherbid contains issues that are already set:"
|
---|
| 101 | + ourissues);
|
---|
| 102 | }
|
---|
[804] | 103 | @NonNull
|
---|
| 104 | HashMap<@NonNull String, @NonNull Value> newvalues = new HashMap<>(
|
---|
| 105 | issuevalues);
|
---|
[519] | 106 | newvalues.putAll(otherbid.issuevalues);
|
---|
| 107 | return new Bid(newvalues);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[825] | 110 | @NonNull
|
---|
[519] | 111 | /**
|
---|
| 112 | *
|
---|
| 113 | * @return (unmodifyable) map with all issues and values in the bid.
|
---|
| 114 | */
|
---|
[825] | 115 | public Map<@NonNull String, @NonNull Value> getIssueValues() {
|
---|
[519] | 116 | return Collections.unmodifiableMap(issuevalues);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | @Override
|
---|
[804] | 120 | public @NonNull String toString() {
|
---|
[734] | 121 | return "Bid" + issuevalues.toString();
|
---|
[519] | 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 | } |
---|