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 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              // only output query if contains anything
43              return buff;
44  
45          } else if (queryList.size() == 1) {
46  
47              // don't add the &
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;&amp;&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  }