package geniusweb.issuevalue; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import org.eclipse.jdt.annotation.NonNull; 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 @NonNull String name; // we use HashMap for better serialization. // key is issuename, value is the set of allowed issuevalues. private final @NonNull HashMap<@NonNull String, @NonNull ValueSet> issuesValues; /** * * @param name a short name for display/toString. This name must * contain simple characters only (a-z, A-Z, 0-9). * @param issuesValues the issue values, Map with key: the issue name and * value: {@link ValueSet} with the allowed values for * the issue. */ @SuppressWarnings("unused") @JsonCreator public Domain(@JsonProperty("name") @NonNull String name, @JsonProperty("issuesValues") @NonNull Map<@NonNull String, @NonNull ValueSet> issuesValues) { if (issuesValues == null) { throw new NullPointerException("issuesValues=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 (issuesValues.isEmpty()) { throw new IllegalArgumentException("issues is empty set"); } this.name = name; this.issuesValues = new HashMap<>(issuesValues); } @Override public @NonNull String toString() { return "Domain[" + name + "," + issuesValues.toString() + "]"; } /** * * @return short name for this domain. */ public @NonNull String getName() { return name; } /** * * @return list of the issues in this domain. */ public @NonNull Set<@NonNull String> getIssuesValues() { return new HashSet(issuesValues.keySet()); } /** * * @param bid the {@link Bid} to be checked (not null) * @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(@NonNull Bid bid) { for (String issue : bid.getIssues()) { if (!(issuesValues.containsKey(issue))) return "bid " + bid.toString() + " 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).toString(); } } return null; } /** * * @param bid a Bid (not null) * @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(@NonNull Bid bid) { if (!(issuesValues.keySet().equals(bid.getIssues()))) { return "Issues in bid (" + bid.getIssues().toString() + ") do not match issues in domain (" + issuesValues.keySet().toString() + ")"; } return isFitting(bid); } /** * @param issue the issue for which allowed values are needed (not null) * @return set of allowed values for given issue, or null if there is no * such an issue. */ public ValueSet getValues(@NonNull 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; } }