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

Last change on this file since 769 was 744, checked in by wouter, 11 months ago

#254 PyProgram now throws TranslationException. Ignore broken test in tudunit-t

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