View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.codec.search;
21  
22  
23  import java.nio.ByteBuffer;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.directory.api.asn1.DecoderException;
28  import org.apache.directory.api.asn1.EncoderException;
29  import org.apache.directory.api.i18n.I18n;
30  
31  
32  /**
33   * This Filter abstract class is used to store a set of filters used by
34   * OR/AND/NOT filters.
35   * 
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  public abstract class ConnectorFilter extends Filter
39  {
40      /** The set of filters used by And/Or filters */
41      protected List<Filter> filterSet;
42  
43      /** The filters length */
44      protected int filtersLength;
45  
46  
47      /**
48       * The constructor. We wont initialize the ArrayList as it may not be used.
49       * 
50       * @param tlvId The TLV identifier
51       */
52      public ConnectorFilter( int tlvId )
53      {
54          super( tlvId );
55      }
56  
57  
58      /**
59       * The constructor. We wont initialize the ArrayList as it may not be used.
60       */
61      public ConnectorFilter()
62      {
63          super();
64      }
65  
66  
67      /**
68       * Add a new Filter to the list.
69       * 
70       * @param filter The filter to add
71       * @throws DecoderException If the decoding failed
72       */
73      public void addFilter( Filter filter ) throws DecoderException
74      {
75  
76          if ( filterSet == null )
77          {
78              filterSet = new ArrayList<>();
79          }
80  
81          filterSet.add( filter );
82      }
83  
84  
85      /**
86       * Get the list of filters stored in the composite filter
87       * 
88       * @return And array of filters
89       */
90      public List<Filter> getFilterSet()
91      {
92          return filterSet;
93      }
94  
95  
96      /**
97       * Compute the ConnectorFilter length Length(ConnectorFilter) =
98       * sum(filterSet.computeLength())
99       * 
100      * @return The encoded length
101      */
102     @Override
103     public int computeLength()
104     {
105         int connectorFilterLength = 0;
106 
107         if ( ( filterSet != null ) && ( !filterSet.isEmpty() ) )
108         {
109             for ( Filter filter : filterSet )
110             {
111                 connectorFilterLength += filter.computeLength();
112             }
113         }
114 
115         return connectorFilterLength;
116     }
117 
118 
119     /**
120      * Encode the ConnectorFilter message to a PDU. 
121      * <pre>
122      * ConnectorFilter :
123      * filter.encode() ... filter.encode()
124      * </pre>
125      * 
126      * @param buffer The buffer where to put the PDU
127      * @return The PDU.
128      * @throws EncoderException If the encoding failed
129      */
130     @Override
131     public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
132     {
133         if ( buffer == null )
134         {
135             throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
136         }
137 
138         // encode each filter
139         if ( ( filterSet != null ) && ( !filterSet.isEmpty() ) )
140         {
141             for ( Filter filter : filterSet )
142             {
143                 filter.encode( buffer );
144             }
145         }
146 
147         return buffer;
148     }
149 
150 
151     /**
152      * Return a string compliant with RFC 2254 representing a composite filter,
153      * one of AND, OR and NOT
154      * 
155      * @return The composite filter string
156      */
157     @Override
158     public String toString()
159     {
160         StringBuilder sb = new StringBuilder();
161 
162         if ( ( filterSet != null ) && ( !filterSet.isEmpty() ) )
163         {
164             for ( Filter filter : filterSet )
165             {
166                 sb.append( '(' ).append( filter ).append( ')' );
167             }
168         }
169 
170         return sb.toString();
171     }
172 }