01: /*
02: * SimpleConstraint.java
03: *
04: * Created on January 25, 2004, 5:52 PM
05: */
06:
07: package org.netbeans.actions.simple;
08:
09: import java.util.Arrays;
10: import java.util.Map;
11: import org.xml.sax.SAXException;
12:
13: /**
14: *
15: * @author Tim Boudreau
16: */
17: class SimpleConstraint {
18: private String name;
19: private SimpleKey[] includeKeys;
20: private SimpleKey[] excludeKeys;
21: private boolean enabledType;
22:
23: /** Creates a new instance of SimpleConstraint */
24: public SimpleConstraint(String name, SimpleKey[] includeKeys,
25: SimpleKey[] excludeKeys, boolean enabledType)
26: throws SAXException {
27: this .name = name;
28: this .includeKeys = includeKeys;
29: this .excludeKeys = excludeKeys;
30: this .enabledType = enabledType;
31: if (name == null) {
32: throw new SAXException("Name may not be null");
33: }
34: if (includeKeys.length == 0 && excludeKeys.length == 0) {
35: throw new SAXException("Constraint has no keys");
36: }
37: System.err.println("Constraint: " + name + " includeKeys "
38: + Arrays.asList(includeKeys) + " excludeKeys: "
39: + Arrays.asList(excludeKeys) + " enabledType: "
40: + enabledType);
41: }
42:
43: public boolean isEnabledType() {
44: return enabledType;
45: }
46:
47: public boolean test(Map context) {
48: boolean result = true;
49: // System.err.println("Test constraint " + name);
50: for (int i = 0; i < excludeKeys.length; i++) {
51: result &= !context.containsKey(excludeKeys[i]);
52: // System.err.println(" check must not contain " + excludeKeys[i]);
53: if (result && excludeKeys[i].mustTest()) {
54: result &= excludeKeys[i].test(context);
55: // System.err.println(" Must test: " + result);
56: }
57: if (!result) {
58: // System.err.println(" FOUND AN INCLUDED KEY IN EXCLUSION SET FOR " + name + " returning false");
59: return result;
60: }
61: }
62: for (int i = 0; i < includeKeys.length; i++) {
63: result &= context.containsKey(includeKeys[i]);
64: // System.err.println(" check must contain " + includeKeys[i] + " contained? " + result);
65: if (result && includeKeys[i].mustTest()) {
66: result &= includeKeys[i].test(context);
67: // System.err.println(" must test it - got " + result);
68: }
69: if (!result) {
70: return result;
71: }
72: }
73: return result;
74: }
75:
76: public String getName() {
77: return name;
78: }
79:
80: public String toString() {
81: return getName();
82: }
83:
84: public int hashCode() {
85: return getName().hashCode();
86: }
87:
88: public boolean equals(Object o) {
89: boolean result = false;
90: if (o.getClass() == SimpleConstraint.class) {
91: result = o.toString().equals(toString());
92: }
93: return result;
94: }
95: }
|