1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.ldaptemplate.support.filter;
18
19 import java.util.Iterator;
20 import java.util.LinkedList;
21 import java.util.List;
22
23 import org.apache.commons.lang.builder.EqualsBuilder;
24 import org.apache.commons.lang.builder.HashCodeBuilder;
25
26 /***
27 * Abstract superclass for binary logical operations, that is "and"
28 * and "or" operations.
29 *
30 * @author Mattias Arthursson
31 */
32 public abstract class BinaryLogicalFilter extends AbstractFilter {
33
34 protected List queryList = new LinkedList();
35
36 /***
37 * @see net.sf.ldaptemplate.support.filter.Filter#encode(java.lang.StringBuffer)
38 */
39 public StringBuffer encode(StringBuffer buff) {
40 if (queryList.size() <= 0) {
41
42
43 return buff;
44
45 } else if (queryList.size() == 1) {
46
47
48 Filter query = (Filter) queryList.get(0);
49 return query.encode(buff);
50
51 } else {
52 buff.append("(" + getLogicalOperator());
53
54 for (Iterator i = queryList.iterator(); i.hasNext();) {
55 Filter query = (Filter) i.next();
56 buff = query.encode(buff);
57 }
58
59 buff.append(")");
60
61 return buff;
62 }
63 }
64
65 /***
66 * Implement this in subclass to return the logical operator, for example
67 * &qout;&&qout;.
68 *
69 * @return the logical operator.
70 */
71 protected abstract String getLogicalOperator();
72
73 /***
74 * Compares each filter in turn
75 *
76 * @see net.sf.ldaptemplate.support.filter.Filter#equals(java.lang.Object)
77 */
78 public boolean equals(Object obj) {
79 if (obj instanceof BinaryLogicalFilter
80 && this.getClass() == obj.getClass()) {
81 return EqualsBuilder.reflectionEquals(this, obj);
82 } else {
83 return false;
84 }
85 }
86
87 /***
88 * Hashes all contained data
89 *
90 * @see net.sf.ldaptemplate.support.filter.Filter#hashCode()
91 */
92 public int hashCode() {
93 return HashCodeBuilder.reflectionHashCode(this);
94 }
95 }