1 | package geniusweb.issuevalue;
|
---|
2 |
|
---|
3 | import java.util.Collections;
|
---|
4 | import java.util.HashMap;
|
---|
5 | import java.util.Map;
|
---|
6 |
|
---|
7 | import org.eclipse.jdt.annotation.NonNull;
|
---|
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 | * A dmain description.
|
---|
16 | */
|
---|
17 | @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
|
---|
18 | public class Domain {
|
---|
19 | private final @NonNull String name;
|
---|
20 | // we use HashMap for better serialization.
|
---|
21 | // key is issuename, value is the set of allowed issuevalues.
|
---|
22 | private final @NonNull HashMap<@NonNull String, @NonNull ValueSet> issuesValues;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | *
|
---|
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.
|
---|
31 | */
|
---|
32 | @SuppressWarnings("unused")
|
---|
33 | @JsonCreator
|
---|
34 | public Domain(@JsonProperty("name") @NonNull String name,
|
---|
35 | @JsonProperty("issuesValues") @NonNull Map<@NonNull String, @NonNull ValueSet> issuesValues) {
|
---|
36 | if (issuesValues == null) {
|
---|
37 | throw new NullPointerException("issuesValues=null");
|
---|
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 | }
|
---|
47 | if (issuesValues.isEmpty()) {
|
---|
48 | throw new IllegalArgumentException("issues is empty set");
|
---|
49 | }
|
---|
50 | this.name = name;
|
---|
51 | this.issuesValues = new HashMap<>(issuesValues);
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public @NonNull String toString() {
|
---|
56 | return "Domain[" + name + "," + issuesValues.toString() + "]";
|
---|
57 | }
|
---|
58 |
|
---|
59 | @NonNull
|
---|
60 | /**
|
---|
61 | *
|
---|
62 | * @return short name for this domain.
|
---|
63 | */
|
---|
64 | public String getName() {
|
---|
65 | return name;
|
---|
66 | }
|
---|
67 |
|
---|
68 | @NonNull
|
---|
69 | /**
|
---|
70 | *
|
---|
71 | * @return list of the issues in this domain.
|
---|
72 | */
|
---|
73 | public Map<@NonNull String, @NonNull ValueSet> getIssuesValues() {
|
---|
74 | return Collections.unmodifiableMap(issuesValues);
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | *
|
---|
79 | * @param bid the {@link Bid} to be checked (not null)
|
---|
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 | */
|
---|
84 | public String isFitting(@NonNull Bid bid) {
|
---|
85 | for (final @NonNull String issue : bid.getIssues()) {
|
---|
86 | if (!(issuesValues.containsKey(issue)))
|
---|
87 | return "bid " + bid.toString() + " refers to non-domain issue '"
|
---|
88 | + issue + "'";
|
---|
89 | if (!issuesValues.get(issue).contains(bid.getValue(issue))) {
|
---|
90 | return "issue '" + issue + "' in bid has illegal value "
|
---|
91 | + bid.getValue(issue).toString();
|
---|
92 | }
|
---|
93 | }
|
---|
94 | return null;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | *
|
---|
99 | * @param bid a Bid (not null)
|
---|
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 | */
|
---|
105 | public String isComplete(@NonNull Bid bid) {
|
---|
106 | if (!(issuesValues.keySet().equals(bid.getIssues()))) {
|
---|
107 | return "Issues in bid (" + bid.getIssues().toString()
|
---|
108 | + ") do not match issues in domain ("
|
---|
109 | + issuesValues.keySet().toString() + ")";
|
---|
110 | }
|
---|
111 | return isFitting(bid);
|
---|
112 | }
|
---|
113 |
|
---|
114 | @NonNull
|
---|
115 | /**
|
---|
116 | * @param issue the issue for which allowed values are needed (not null)
|
---|
117 | * @return set of allowed values for given issue, or null if there is no
|
---|
118 | * such an issue.
|
---|
119 | */
|
---|
120 | public ValueSet getValues(@NonNull String issue) {
|
---|
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 | }
|
---|