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 org.apache.commons.lang.Validate;
20
21 /***
22 * Decorator filter for NOT. (!<i>filter</i> )
23 *
24 * <pre>
25 * Filter filter = new NotFilter(new EqualsFilter("cn", "foo");
26 * System.out.println(filter.ecode());
27 * </pre>
28 *
29 * would resut in: <code>(!(cn=foo))</code>
30 *
31 * @author Adam Skogman
32 */
33 public class NotFilter extends AbstractFilter {
34
35 private final Filter filter;
36
37 static private final int HASH = "!".hashCode();
38
39 /***
40 *
41 */
42 public NotFilter(Filter filter) {
43 Validate.notNull(filter);
44 this.filter = filter;
45 }
46
47 /***
48 * @see net.sf.ldaptemplate.support.filter.Filter#encode(java.lang.StringBuffer)
49 */
50 public StringBuffer encode(StringBuffer buff) {
51
52 buff.append("(!");
53 filter.encode(buff);
54 buff.append(')');
55
56 return buff;
57
58 }
59
60 /***
61 * Compares key and value before encoding
62 *
63 * @see net.sf.ldaptemplate.support.filter.Filter#equals(java.lang.Object)
64 */
65 public boolean equals(Object o) {
66
67 if (o instanceof NotFilter && o.getClass() == this.getClass()) {
68 NotFilter f = (NotFilter) o;
69 return this.filter.equals(f.filter);
70 }
71
72 return false;
73 }
74
75 /***
76 * hash attribute and value
77 *
78 * @see net.sf.ldaptemplate.support.filter.Filter#hashCode()
79 */
80 public int hashCode() {
81 return HASH ^ filter.hashCode();
82 }
83
84 }