View Javadoc

1   /*
2    * Copyright 2002-2005 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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(&quot;cn&quot;, &quot;foo&quot;);
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  }