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