01: package org.drools.lang.descr;
02:
03: import java.util.ArrayList;
04: import java.util.Collections;
05: import java.util.Iterator;
06: import java.util.List;
07:
08: /**
09: * This is used to connect restrictions together for a single field
10: * eg:
11: * age < 40 & > 30
12: *
13: */
14: public class RestrictionConnectiveDescr extends RestrictionDescr {
15:
16: private static final long serialVersionUID = 400L;
17: public final static int AND = 0;
18: public final static int OR = 1;
19:
20: private int connective;
21: private List restrictions;
22:
23: public RestrictionConnectiveDescr(final int connective) {
24: super ();
25: this .connective = connective;
26: this .restrictions = Collections.EMPTY_LIST;
27: }
28:
29: public int getConnective() {
30: return this .connective;
31: }
32:
33: public void addRestriction(RestrictionDescr restriction) {
34: if (this .restrictions == Collections.EMPTY_LIST) {
35: this .restrictions = new ArrayList();
36: }
37: this .restrictions.add(restriction);
38: }
39:
40: public void addOrMerge(RestrictionDescr restriction) {
41: if ((restriction instanceof RestrictionConnectiveDescr)
42: && ((RestrictionConnectiveDescr) restriction).connective == this .connective) {
43: if (this .restrictions == Collections.EMPTY_LIST) {
44: this .restrictions = new ArrayList();
45: }
46: this .restrictions
47: .addAll(((RestrictionConnectiveDescr) restriction)
48: .getRestrictions());
49: } else {
50: this .addRestriction(restriction);
51: }
52: }
53:
54: public List getRestrictions() {
55: return this .restrictions;
56: }
57:
58: public String toString() {
59: final String connectiveStr = this .connective == OR ? " || "
60: : " && ";
61: final StringBuffer buf = new StringBuffer();
62: buf.append("( ");
63: for (Iterator it = this .restrictions.iterator(); it.hasNext();) {
64: buf.append(it.next().toString());
65: if (it.hasNext()) {
66: buf.append(connectiveStr);
67: }
68: }
69: buf.append(" )");
70: return buf.toString();
71: }
72: }
|