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    
021    package org.apache.directory.shared.ldap.filter;
022    
023    
024    import java.util.List;
025    
026    /**
027     * Node representing an Not connector in a filter operation
028     * 
029     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
030     * @version $Rev: 517453 $
031     */
032    public class NotNode extends BranchNode
033    {
034        /**
035         * Creates a NotNode using a logical NOT operator and a list of children.
036         * 
037         * A Not node could contain only one child
038         * 
039         * @param childList the child nodes under this branch node.
040         */
041        public NotNode( List<ExprNode> childList )
042        {
043            super( AssertionType.NOT );
044    
045            if ( childList != null )
046            {
047                setChildren( childList );
048            }
049        }
050    
051    
052        /**
053         * Creates a NotNode using a logical NOT operator and the given child.
054         * 
055         * @param child the child node under this branch node.
056         */
057        public NotNode( ExprNode child )
058        {
059            super( AssertionType.NOT );
060    
061            if ( child != null )
062            {
063                addNode( child );
064            }
065        }
066    
067        
068        /**
069         * Clone the Node
070         */
071        @Override public ExprNode clone()
072        {
073            return super.clone();
074        }
075        
076    
077    
078        /**
079         * Creates an empty NotNode
080         */
081        public NotNode()
082        {
083            this( (ExprNode)null );
084        }
085    
086        /**
087         * Adds a child node to this NOT node node
088         * 
089         * @param node the child expression to add to this NOT node
090         */
091        public void addNode( ExprNode node )
092        {
093            if ( ( children != null ) && ( children.size() >= 1 ) )
094            {
095                throw new IllegalStateException( "Cannot add more than one element to a negation node." );
096            }
097            
098            children.add( node );
099        }
100    
101    
102        /**
103         * Adds a child node to this NOT node at the head rather than the tail. 
104         * 
105         * @param node the child expression to add to this branch node
106         */
107        public void addNodeToHead( ExprNode node )
108        {
109            if ( children.size() >= 1 )
110            {
111                throw new IllegalStateException( "Cannot add more than one element to a negation node." );            
112            }
113            
114            children.add( node );
115        }
116    
117    
118        /**
119         * Sets the list of children under this node.
120         * 
121         * @param childList the list of children to set.
122         */
123        public void setChildren( List<ExprNode> childList )
124        {
125            if ( ( childList != null ) && ( childList.size() > 1 ) )
126            {
127                throw new IllegalStateException( "Cannot add more than one element to a negation node." );            
128            }
129    
130            children = childList;
131        }
132    
133        
134        /**
135         * Gets the operator for this branch node.
136         * 
137         * @return the operator constant.
138         */
139        public AssertionType getOperator()
140        {
141            return AssertionType.NOT;
142        }
143    
144    
145        /**
146         * Tests whether or not this node is a disjunction (a OR'ed branch).
147         * 
148         * @return true if the operation is a OR, false otherwise.
149         */
150        public boolean isDisjunction()
151        {
152            return false;
153        }
154    
155    
156        /**
157         * Tests whether or not this node is a conjunction (a AND'ed branch).
158         * 
159         * @return true if the operation is a AND, false otherwise.
160         */
161        public boolean isConjunction()
162        {
163            return false;
164        }
165    
166    
167        /**
168         * Tests whether or not this node is a negation (a NOT'ed branch).
169         * 
170         * @return true if the operation is a NOT, false otherwise.
171         */
172        public boolean isNegation()
173        {
174            return true;
175        }
176    
177    
178        /**
179         * @see ExprNode#printRefinementToBuffer(StringBuffer)
180         * 
181         * @return The buffer in which the refinement has been appended
182         * @throws UnsupportedOperationException if this node isn't a part of a refinement.
183         */
184        public StringBuilder printRefinementToBuffer( StringBuilder buf )
185        {
186            buf.append( "not: {" );
187            boolean isFirst = true;
188            
189            for ( ExprNode node:children )
190            {
191                if ( isFirst )
192                {
193                    isFirst = false;
194                }
195                else
196                {
197                    buf.append( ", " );
198                }
199                
200                node.printRefinementToBuffer( buf );
201            }
202            
203            buf.append( '}' );
204            
205            return buf;
206        }
207    
208        /**
209         * Gets the recursive prefix string represent of the filter from this node
210         * down.
211         * 
212         * @see java.lang.Object#toString()
213         * @return A string representing the AndNode
214         */
215        public String toString()
216        {
217            StringBuilder buf = new StringBuilder();
218            buf.append( "(!" );
219            
220            buf.append( super.toString() );
221    
222            buf.append( getFirstChild() );
223            buf.append( ')' );
224            
225            return buf.toString();
226        }
227    }