[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) {
|
---|
[868] | 95 | final @NonNull Set<@NonNull String> ourissues = new HashSet<>(
|
---|
| 96 | issuevalues.keySet());
|
---|
[519] | 97 | ourissues.retainAll(otherbid.getIssues());
|
---|
| 98 | if (!ourissues.isEmpty()) {
|
---|
| 99 | throw new IllegalArgumentException(
|
---|
| 100 | "Otherbid contains issues that are already set:"
|
---|
[846] | 101 | + ourissues.toString());
|
---|
[519] | 102 | }
|
---|
[868] | 103 | final @NonNull HashMap<@NonNull String, @NonNull Value> newvalues = new HashMap<>(
|
---|
[804] | 104 | issuevalues);
|
---|
[519] | 105 | newvalues.putAll(otherbid.issuevalues);
|
---|
| 106 | return new Bid(newvalues);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[825] | 109 | @NonNull
|
---|
[519] | 110 | /**
|
---|
| 111 | *
|
---|
| 112 | * @return (unmodifyable) map with all issues and values in the bid.
|
---|
| 113 | */
|
---|
[825] | 114 | public Map<@NonNull String, @NonNull Value> getIssueValues() {
|
---|
[519] | 115 | return Collections.unmodifiableMap(issuevalues);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | @Override
|
---|
[804] | 119 | public @NonNull String toString() {
|
---|
[734] | 120 | return "Bid" + issuevalues.toString();
|
---|
[519] | 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 | } |
---|