[519] | 1 | package geniusweb.issuevalue;
|
---|
| 2 |
|
---|
[854] | 3 | import java.util.Collections;
|
---|
[519] | 4 | import java.util.HashMap;
|
---|
| 5 | import java.util.Map;
|
---|
| 6 |
|
---|
[804] | 7 | import org.eclipse.jdt.annotation.NonNull;
|
---|
| 8 |
|
---|
[519] | 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 | * A dmain description.
|
---|
| 16 | */
|
---|
| 17 | @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
|
---|
| 18 | public class Domain {
|
---|
[804] | 19 | private final @NonNull String name;
|
---|
[519] | 20 | // we use HashMap for better serialization.
|
---|
| 21 | // key is issuename, value is the set of allowed issuevalues.
|
---|
[804] | 22 | private final @NonNull HashMap<@NonNull String, @NonNull ValueSet> issuesValues;
|
---|
[519] | 23 |
|
---|
| 24 | /**
|
---|
| 25 | *
|
---|
[742] | 26 | * @param name a short name for display/toString. This name must
|
---|
| 27 | * contain simple characters only (a-z, A-Z, 0-9).
|
---|
| 28 | * @param issuesValues the issue values, Map with key: the issue name and
|
---|
| 29 | * value: {@link ValueSet} with the allowed values for
|
---|
| 30 | * the issue.
|
---|
[519] | 31 | */
|
---|
[804] | 32 | @SuppressWarnings("unused")
|
---|
[519] | 33 | @JsonCreator
|
---|
[804] | 34 | public Domain(@JsonProperty("name") @NonNull String name,
|
---|
| 35 | @JsonProperty("issuesValues") @NonNull Map<@NonNull String, @NonNull ValueSet> issuesValues) {
|
---|
[742] | 36 | if (issuesValues == null) {
|
---|
| 37 | throw new NullPointerException("issuesValues=null");
|
---|
[519] | 38 | }
|
---|
| 39 | if (name == null) {
|
---|
| 40 | throw new NullPointerException("name=null");
|
---|
| 41 | }
|
---|
| 42 | if (!name.matches("[a-zA-Z0-9]+")) {
|
---|
| 43 | throw new IllegalArgumentException(
|
---|
| 44 | "domain name can have only simple characters but found "
|
---|
| 45 | + name);
|
---|
| 46 | }
|
---|
[742] | 47 | if (issuesValues.isEmpty()) {
|
---|
[519] | 48 | throw new IllegalArgumentException("issues is empty set");
|
---|
| 49 | }
|
---|
| 50 | this.name = name;
|
---|
[742] | 51 | this.issuesValues = new HashMap<>(issuesValues);
|
---|
[519] | 52 | }
|
---|
| 53 |
|
---|
| 54 | @Override
|
---|
[804] | 55 | public @NonNull String toString() {
|
---|
[734] | 56 | return "Domain[" + name + "," + issuesValues.toString() + "]";
|
---|
[519] | 57 | }
|
---|
| 58 |
|
---|
[825] | 59 | @NonNull
|
---|
[519] | 60 | /**
|
---|
| 61 | *
|
---|
| 62 | * @return short name for this domain.
|
---|
| 63 | */
|
---|
[825] | 64 | public String getName() {
|
---|
[519] | 65 | return name;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[825] | 68 | @NonNull
|
---|
[519] | 69 | /**
|
---|
| 70 | *
|
---|
| 71 | * @return list of the issues in this domain.
|
---|
| 72 | */
|
---|
[854] | 73 | public Map<@NonNull String, @NonNull ValueSet> getIssuesValues() {
|
---|
| 74 | return Collections.unmodifiableMap(issuesValues);
|
---|
[519] | 75 | }
|
---|
| 76 |
|
---|
| 77 | /**
|
---|
| 78 | *
|
---|
[804] | 79 | * @param bid the {@link Bid} to be checked (not null)
|
---|
[519] | 80 | * @return null if bid is fitting, or a string containing a message
|
---|
| 81 | * explaining why not. A bid is fitting if all issues are in the
|
---|
| 82 | * domain and all issue values are known values.
|
---|
| 83 | */
|
---|
[804] | 84 | public String isFitting(@NonNull Bid bid) {
|
---|
[818] | 85 | for (final @NonNull String issue : bid.getIssues()) {
|
---|
[519] | 86 | if (!(issuesValues.containsKey(issue)))
|
---|
[736] | 87 | return "bid " + bid.toString() + " refers to non-domain issue '"
|
---|
| 88 | + issue + "'";
|
---|
[519] | 89 | if (!issuesValues.get(issue).contains(bid.getValue(issue))) {
|
---|
| 90 | return "issue '" + issue + "' in bid has illegal value "
|
---|
[736] | 91 | + bid.getValue(issue).toString();
|
---|
[519] | 92 | }
|
---|
| 93 | }
|
---|
| 94 | return null;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | /**
|
---|
| 98 | *
|
---|
[804] | 99 | * @param bid a Bid (not null)
|
---|
[519] | 100 | * @return null if this bid is complete, or an error message explaining why
|
---|
| 101 | * the bid is not complete. Complete means that the bid contains a
|
---|
| 102 | * valid value for each issue in the domain and no values for
|
---|
| 103 | * unknown issues.
|
---|
| 104 | */
|
---|
[804] | 105 | public String isComplete(@NonNull Bid bid) {
|
---|
[519] | 106 | if (!(issuesValues.keySet().equals(bid.getIssues()))) {
|
---|
[736] | 107 | return "Issues in bid (" + bid.getIssues().toString()
|
---|
[519] | 108 | + ") do not match issues in domain ("
|
---|
[736] | 109 | + issuesValues.keySet().toString() + ")";
|
---|
[519] | 110 | }
|
---|
| 111 | return isFitting(bid);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[825] | 114 | @NonNull
|
---|
[519] | 115 | /**
|
---|
[804] | 116 | * @param issue the issue for which allowed values are needed (not null)
|
---|
[519] | 117 | * @return set of allowed values for given issue, or null if there is no
|
---|
| 118 | * such an issue.
|
---|
| 119 | */
|
---|
[825] | 120 | public ValueSet getValues(@NonNull String issue) {
|
---|
[519] | 121 | return issuesValues.get(issue);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | @Override
|
---|
| 125 | public int hashCode() {
|
---|
| 126 | final int prime = 31;
|
---|
| 127 | int result = 1;
|
---|
| 128 | result = prime * result
|
---|
| 129 | + ((issuesValues == null) ? 0 : issuesValues.hashCode());
|
---|
| 130 | result = prime * result + ((name == null) ? 0 : name.hashCode());
|
---|
| 131 | return result;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | @Override
|
---|
| 135 | public boolean equals(Object obj) {
|
---|
| 136 | if (this == obj)
|
---|
| 137 | return true;
|
---|
| 138 | if (obj == null)
|
---|
| 139 | return false;
|
---|
| 140 | if (getClass() != obj.getClass())
|
---|
| 141 | return false;
|
---|
| 142 | Domain other = (Domain) obj;
|
---|
| 143 | if (issuesValues == null) {
|
---|
| 144 | if (other.issuesValues != null)
|
---|
| 145 | return false;
|
---|
| 146 | } else if (!issuesValues.equals(other.issuesValues))
|
---|
| 147 | return false;
|
---|
| 148 | if (name == null) {
|
---|
| 149 | if (other.name != null)
|
---|
| 150 | return false;
|
---|
| 151 | } else if (!name.equals(other.name))
|
---|
| 152 | return false;
|
---|
| 153 | return true;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | }
|
---|