source: java2python/geniuswebtranslator/geniuswebsrc/geniusweb/issuevalue/Domain.java@ 829

Last change on this file since 829 was 825, checked in by wouter, 6 months ago

#291 move annotation to above the javadoc

File size: 4.5 KB
Line 
1package geniusweb.issuevalue;
2
3import java.util.HashMap;
4import java.util.HashSet;
5import java.util.Map;
6import java.util.Set;
7
8import org.eclipse.jdt.annotation.NonNull;
9
10import com.fasterxml.jackson.annotation.JsonAutoDetect;
11import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
12import com.fasterxml.jackson.annotation.JsonCreator;
13import com.fasterxml.jackson.annotation.JsonProperty;
14
15/**
16 * A dmain description.
17 */
18@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
19public 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 @NonNull
61 /**
62 *
63 * @return short name for this domain.
64 */
65 public String getName() {
66 return name;
67 }
68
69 @NonNull
70 /**
71 *
72 * @return list of the issues in this domain.
73 */
74 public Set<@NonNull String> getIssuesValues() {
75 return new HashSet<String>(issuesValues.keySet());
76 }
77
78 /**
79 *
80 * @param bid the {@link Bid} to be checked (not null)
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 */
85 public String isFitting(@NonNull Bid bid) {
86 for (final @NonNull String issue : bid.getIssues()) {
87 if (!(issuesValues.containsKey(issue)))
88 return "bid " + bid.toString() + " refers to non-domain issue '"
89 + issue + "'";
90 if (!issuesValues.get(issue).contains(bid.getValue(issue))) {
91 return "issue '" + issue + "' in bid has illegal value "
92 + bid.getValue(issue).toString();
93 }
94 }
95 return null;
96 }
97
98 /**
99 *
100 * @param bid a Bid (not null)
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 */
106 public String isComplete(@NonNull Bid bid) {
107 if (!(issuesValues.keySet().equals(bid.getIssues()))) {
108 return "Issues in bid (" + bid.getIssues().toString()
109 + ") do not match issues in domain ("
110 + issuesValues.keySet().toString() + ")";
111 }
112 return isFitting(bid);
113 }
114
115 @NonNull
116 /**
117 * @param issue the issue for which allowed values are needed (not null)
118 * @return set of allowed values for given issue, or null if there is no
119 * such an issue.
120 */
121 public ValueSet getValues(@NonNull String issue) {
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}
Note: See TracBrowser for help on using the repository browser.