001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.shared.ldap.codec.search;
021    
022    
023    import java.nio.ByteBuffer;
024    import java.util.ArrayList;
025    import java.util.List;
026    
027    import org.apache.directory.shared.asn1.codec.DecoderException;
028    import org.apache.directory.shared.asn1.codec.EncoderException;
029    import org.apache.directory.shared.i18n.I18n;
030    
031    
032    /**
033     * This Filter abstract class is used to store a set of filters used by
034     * OR/AND/NOT filters.
035     * 
036     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037     * @version $Rev: 912399 $, $Date: 2010-02-21 21:52:31 +0100 (Sun, 21 Feb 2010) $, 
038     */
039    public abstract class ConnectorFilter extends Filter
040    {
041        // ~ Instance fields
042        // ----------------------------------------------------------------------------
043    
044        /** The set of filters used by And/Or filters */
045        protected List<Filter> filterSet;
046    
047        /** The filters length */
048        protected int filtersLength;
049    
050    
051        // ~ Constructors
052        // -------------------------------------------------------------------------------
053    
054        /**
055         * The constructor. We wont initialize the ArrayList as it may not be used.
056         */
057        public ConnectorFilter( int tlvId )
058        {
059            super( tlvId );
060        }
061    
062    
063        /**
064         * The constructor. We wont initialize the ArrayList as it may not be used.
065         */
066        public ConnectorFilter()
067        {
068            super();
069        }
070    
071    
072        // ~ Methods
073        // ------------------------------------------------------------------------------------
074    
075        /**
076         * Add a new Filter to the list.
077         * 
078         * @param filter The filter to add
079         */
080        public void addFilter( Filter filter ) throws DecoderException
081        {
082    
083            if ( filterSet == null )
084            {
085                filterSet = new ArrayList<Filter>();
086            }
087    
088            filterSet.add( filter );
089        }
090    
091    
092        /**
093         * Get the list of filters stored in the composite filter
094         * 
095         * @return And array of filters
096         */
097        public List<Filter> getFilterSet()
098        {
099            return filterSet;
100        }
101    
102    
103        /**
104         * Compute the ConnectorFilter length Length(ConnectorFilter) =
105         * sum(filterSet.computeLength())
106         */
107        public int computeLength()
108        {
109            int connectorFilterLength = 0;
110    
111            if ( ( filterSet != null ) && ( filterSet.size() != 0 ) )
112            {
113                for ( Filter filter:filterSet )
114                {
115                    connectorFilterLength += filter.computeLength();
116                }
117            }
118    
119            return connectorFilterLength;
120        }
121    
122    
123        /**
124         * Encode the ConnectorFilter message to a PDU. 
125         * 
126         * ConnectorFilter :
127         * filter.encode() ... filter.encode()
128         * 
129         * @param buffer The buffer where to put the PDU
130         * @return The PDU.
131         */
132        public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
133        {
134            if ( buffer == null )
135            {
136                throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
137            }
138    
139            // encode each filter
140            if ( ( filterSet != null ) && ( filterSet.size() != 0 ) )
141            {
142                for ( Filter filter:filterSet )
143                {
144                    filter.encode( buffer );
145                }
146            }
147    
148            return buffer;
149        }
150    
151    
152        /**
153         * Return a string compliant with RFC 2254 representing a composite filter,
154         * one of AND, OR and NOT
155         * 
156         * @return The composite filter string
157         */
158        public String toString()
159        {
160            StringBuffer sb = new StringBuffer();
161    
162            if ( ( filterSet != null ) && ( filterSet.size() != 0 ) )
163            {
164                for ( Filter filter : filterSet )
165                {
166                    sb.append( '(' ).append( filter ).append( ')' );
167                }
168            }
169    
170            return sb.toString();
171        }
172    }