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