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