package geniusweb.issuevalue; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; /** * A dmain description. */ @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE) public class Domain { private final String name; // we use HashMap for better serialization. // key is issuename, value is the set of allowed issuevalues. private final HashMap issuesValues; /** * * @param name a short name for display/toString. This name must contain * simple characters only (a-z, A-Z, 0-9). * @param issues the issues, Map with key: the issue name and value: * {@link ValueSet} with the allowed values for the issue. */ @JsonCreator public Domain(@JsonProperty("name") String name, @JsonProperty("issuesValues") Map issues) { if (issues == null) { throw new NullPointerException("issues=null"); } if (name == null) { throw new NullPointerException("name=null"); } if (!name.matches("[a-zA-Z0-9]+")) { throw new IllegalArgumentException( "domain name can have only simple characters but found " + name); } if (issues.isEmpty()) { throw new IllegalArgumentException("issues is empty set"); } this.name = name; this.issuesValues = new HashMap<>(issues); } @Override public String toString() { return "Domain[" + name + "," + issuesValues + "]"; } /** * * @return short name for this domain. */ public String getName() { return name; } /** * * @return list of the issues in this domain. */ public Set getIssues() { return new HashSet(issuesValues.keySet()); } /** * * @param bid the {@link Bid} to be checked * @return null if bid is fitting, or a string containing a message * explaining why not. A bid is fitting if all issues are in the * domain and all issue values are known values. */ public String isFitting(Bid bid) { for (String issue : bid.getIssues()) { if (!(issuesValues.containsKey(issue))) return "bid " + bid + " refers to non-domain issue '" + issue + "'"; if (!issuesValues.get(issue).contains(bid.getValue(issue))) { return "issue '" + issue + "' in bid has illegal value " + bid.getValue(issue); } } return null; } /** * * @param bid a Bid * @return null if this bid is complete, or an error message explaining why * the bid is not complete. Complete means that the bid contains a * valid value for each issue in the domain and no values for * unknown issues. */ public String isComplete(Bid bid) { if (!(issuesValues.keySet().equals(bid.getIssues()))) { return "Issues in bid (" + bid.getIssues() + ") do not match issues in domain (" + issuesValues.keySet() + ")"; } return isFitting(bid); } /** * @param issue the issue for which allowed values are needed * @return set of allowed values for given issue, or null if there is no * such an issue. */ public ValueSet getValues(String issue) { return issuesValues.get(issue); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((issuesValues == null) ? 0 : issuesValues.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Domain other = (Domain) obj; if (issuesValues == null) { if (other.issuesValues != null) return false; } else if (!issuesValues.equals(other.issuesValues)) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } }