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