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.dsmlv2.request;
021
022
023import org.apache.directory.api.asn1.DecoderException;
024import org.apache.directory.api.i18n.I18n;
025
026
027/**
028 * Not Filter Object to store the Not filter.
029 * 
030 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
031 */
032public class NotFilter extends ConnectorFilter
033{
034    /**
035     * Subclass the addFilterMethod, as this is specific for a NotFilter (we
036     * cannot have more than one elements).
037     * 
038     * @param filter The Filter to add
039     */
040    @Override
041    public void addFilter( Filter filter ) throws DecoderException
042    {
043        if ( filterSet != null )
044        {
045            throw new DecoderException( I18n.err( I18n.ERR_05501_MORE_THAN_ONE_FILTER_FOR_NOT_FILTER ) );
046        }
047
048        super.addFilter( filter );
049    }
050
051
052    /**
053     * Get the NotFilter
054     * 
055     * @return Returns the notFilter.
056     */
057    public Filter getNotFilter()
058    {
059        return filterSet.get( 0 );
060    }
061
062
063    /**
064     * Set the NotFilter
065     * 
066     * @param notFilter The notFilter to set.
067     * @throws DecoderException If the filter is invalid
068     */
069    public void setNotFilter( Filter notFilter ) throws DecoderException
070    {
071        if ( filterSet != null )
072        {
073            throw new DecoderException( I18n.err( I18n.ERR_05501_MORE_THAN_ONE_FILTER_FOR_NOT_FILTER ) );
074        }
075
076        super.addFilter( notFilter );
077    }
078
079
080    /**
081     * Return a string compliant with RFC 2254 representing a NOT filter
082     * 
083     * @return The NOT filter string
084     */
085    @Override
086    public String toString()
087    {
088        StringBuilder sb = new StringBuilder();
089
090        sb.append( '!' ).append( super.toString() );
091
092        return sb.toString();
093    }
094}