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