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 */
020package org.apache.directory.api.ldap.codec.search;
021
022
023import java.nio.BufferOverflowException;
024import java.nio.ByteBuffer;
025import java.util.List;
026
027import org.apache.directory.api.asn1.EncoderException;
028import org.apache.directory.api.asn1.ber.tlv.TLV;
029import org.apache.directory.api.i18n.I18n;
030import org.apache.directory.api.ldap.codec.api.LdapCodecConstants;
031
032
033/**
034 * And Filter Object to store the And filter.
035 * 
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 */
038public class AndFilter extends ConnectorFilter
039{
040    /**
041     * The constructor. We wont initialize the ArrayList as they may not be
042     * used.
043     * 
044     * @param tlvId The TLV identifier
045     */
046    public AndFilter( int tlvId )
047    {
048        super( tlvId );
049    }
050
051
052    /**
053     * The constructor. We wont initialize the ArrayList as they may not be
054     * used.
055     */
056    public AndFilter()
057    {
058        super();
059    }
060
061
062    /**
063     * Get the AndFilter.
064     * 
065     * @return Returns the andFilter.
066     */
067    public List<Filter> getAndFilter()
068    {
069        return filterSet;
070    }
071
072
073    /**
074     * Compute the AndFilter length 
075     * <br>
076     * AndFilter :
077     * <pre> 
078     * 0xA0 L1 super.computeLength()
079     * 
080     * Length(AndFilter) = Length(0xA0) + Length(super.computeLength()) +
081     *          super.computeLength()
082     * </pre>
083     * 
084     * @return The encoded length
085     */
086    @Override
087    public int computeLength()
088    {
089        filtersLength = super.computeLength();
090
091        return 1 + TLV.getNbBytes( filtersLength ) + filtersLength;
092    }
093
094
095    /**
096     * Encode the AndFilter message to a PDU. 
097     * <br>
098     * AndFilter :
099     * <pre> 
100     * 0xA0 LL
101     *  filter.encode() ... filter.encode()
102     * </pre>
103     * 
104     * @param buffer The buffer where to put the PDU
105     * @return The PDU.
106     */
107    @Override
108    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
109    {
110        if ( buffer == null )
111        {
112            throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
113        }
114
115        try
116        {
117            // The AndFilter Tag
118            buffer.put( ( byte ) LdapCodecConstants.AND_FILTER_TAG );
119            buffer.put( TLV.getBytes( filtersLength ) );
120        }
121        catch ( BufferOverflowException boe )
122        {
123            throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe );
124        }
125
126        super.encode( buffer );
127
128        return buffer;
129    }
130
131
132    /**
133     * Return a string compliant with RFC 2254 representing an AND filter
134     * 
135     * @return The AND filter string
136     */
137    @Override
138    public String toString()
139    {
140        StringBuilder sb = new StringBuilder();
141
142        sb.append( '&' ).append( super.toString() );
143
144        return sb.toString();
145    }
146}