[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 |
|
---|
[825] | 60 | @NonNull
|
---|
[519] | 61 | /**
|
---|
| 62 | *
|
---|
| 63 | * @return short name for this domain.
|
---|
| 64 | */
|
---|
[825] | 65 | public String getName() {
|
---|
[519] | 66 | return name;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[825] | 69 | @NonNull
|
---|
[519] | 70 | /**
|
---|
| 71 | *
|
---|
| 72 | * @return list of the issues in this domain.
|
---|
| 73 | */
|
---|
[825] | 74 | public Set<@NonNull String> getIssuesValues() {
|
---|
[519] | 75 | return new HashSet<String>(issuesValues.keySet());
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | /**
|
---|
| 79 | *
|
---|
[804] | 80 | * @param bid the {@link Bid} to be checked (not null)
|
---|
[519] | 81 | * @return null if bid is fitting, or a string containing a message
|
---|
| 82 | * explaining why not. A bid is fitting if all issues are in the
|
---|
| 83 | * domain and all issue values are known values.
|
---|
| 84 | */
|
---|
[804] | 85 | public String isFitting(@NonNull Bid bid) {
|
---|
[818] | 86 | for (final @NonNull String issue : bid.getIssues()) {
|
---|
[519] | 87 | if (!(issuesValues.containsKey(issue)))
|
---|
[736] | 88 | return "bid " + bid.toString() + " refers to non-domain issue '"
|
---|
| 89 | + issue + "'";
|
---|
[519] | 90 | if (!issuesValues.get(issue).contains(bid.getValue(issue))) {
|
---|
| 91 | return "issue '" + issue + "' in bid has illegal value "
|
---|
[736] | 92 | + bid.getValue(issue).toString();
|
---|
[519] | 93 | }
|
---|
| 94 | }
|
---|
| 95 | return null;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | /**
|
---|
| 99 | *
|
---|
[804] | 100 | * @param bid a Bid (not null)
|
---|
[519] | 101 | * @return null if this bid is complete, or an error message explaining why
|
---|
| 102 | * the bid is not complete. Complete means that the bid contains a
|
---|
| 103 | * valid value for each issue in the domain and no values for
|
---|
| 104 | * unknown issues.
|
---|
| 105 | */
|
---|
[804] | 106 | public String isComplete(@NonNull Bid bid) {
|
---|
[519] | 107 | if (!(issuesValues.keySet().equals(bid.getIssues()))) {
|
---|
[736] | 108 | return "Issues in bid (" + bid.getIssues().toString()
|
---|
[519] | 109 | + ") do not match issues in domain ("
|
---|
[736] | 110 | + issuesValues.keySet().toString() + ")";
|
---|
[519] | 111 | }
|
---|
| 112 | return isFitting(bid);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[825] | 115 | @NonNull
|
---|
[519] | 116 | /**
|
---|
[804] | 117 | * @param issue the issue for which allowed values are needed (not null)
|
---|
[519] | 118 | * @return set of allowed values for given issue, or null if there is no
|
---|
| 119 | * such an issue.
|
---|
| 120 | */
|
---|
[825] | 121 | public ValueSet getValues(@NonNull String issue) {
|
---|
[519] | 122 | return issuesValues.get(issue);
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | @Override
|
---|
| 126 | public int hashCode() {
|
---|
| 127 | final int prime = 31;
|
---|
| 128 | int result = 1;
|
---|
| 129 | result = prime * result
|
---|
| 130 | + ((issuesValues == null) ? 0 : issuesValues.hashCode());
|
---|
| 131 | result = prime * result + ((name == null) ? 0 : name.hashCode());
|
---|
| 132 | return result;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | @Override
|
---|
| 136 | public boolean equals(Object obj) {
|
---|
| 137 | if (this == obj)
|
---|
| 138 | return true;
|
---|
| 139 | if (obj == null)
|
---|
| 140 | return false;
|
---|
| 141 | if (getClass() != obj.getClass())
|
---|
| 142 | return false;
|
---|
| 143 | Domain other = (Domain) obj;
|
---|
| 144 | if (issuesValues == null) {
|
---|
| 145 | if (other.issuesValues != null)
|
---|
| 146 | return false;
|
---|
| 147 | } else if (!issuesValues.equals(other.issuesValues))
|
---|
| 148 | return false;
|
---|
| 149 | if (name == null) {
|
---|
| 150 | if (other.name != null)
|
---|
| 151 | return false;
|
---|
| 152 | } else if (!name.equals(other.name))
|
---|
| 153 | return false;
|
---|
| 154 | return true;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | }
|
---|