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  
21  package org.apache.directory.api.ldap.model.filter;
22  
23  
24  import java.util.List;
25  
26  import org.apache.directory.api.i18n.I18n;
27  
28  
29  /**
30   * Node representing an Not connector in a filter operation
31   * 
32   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33   */
34  public class NotNode extends BranchNode
35  {
36      /**
37       * Creates a NotNode using a logical NOT operator and a list of children.
38       * 
39       * A Not node could contain only one child
40       * 
41       * @param childList the child nodes under this branch node.
42       */
43      public NotNode( List<ExprNode> childList )
44      {
45          super( AssertionType.NOT );
46  
47          if ( childList != null )
48          {
49              setChildren( childList );
50          }
51      }
52  
53  
54      /**
55       * Creates a NotNode using a logical NOT operator and the given child.
56       * 
57       * @param child the child node under this branch node.
58       */
59      public NotNode( ExprNode child )
60      {
61          super( AssertionType.NOT );
62  
63          if ( child != null )
64          {
65              addNode( child );
66          }
67      }
68  
69  
70      /**
71       * Creates an empty NotNode
72       */
73      public NotNode()
74      {
75          this( ( ExprNode ) null );
76      }
77  
78  
79      /**
80       * Adds a child node to this NOT node node
81       * 
82       * @param node the child expression to add to this NOT node
83       */
84      public void addNode( ExprNode node )
85      {
86          if ( ( children != null ) && ( children.size() >= 1 ) )
87          {
88              throw new IllegalStateException( I18n.err( I18n.ERR_04159 ) );
89          }
90  
91          children.add( node );
92      }
93  
94  
95      /**
96       * Adds a child node to this NOT node at the head rather than the tail. 
97       * 
98       * @param node the child expression to add to this branch node
99       */
100     public void addNodeToHead( ExprNode node )
101     {
102         if ( children.size() >= 1 )
103         {
104             throw new IllegalStateException( I18n.err( I18n.ERR_04159 ) );
105         }
106 
107         children.add( node );
108     }
109 
110 
111     /**
112      * Sets the list of children under this node.
113      * 
114      * @param childList the list of children to set.
115      */
116     public void setChildren( List<ExprNode> childList )
117     {
118         if ( ( childList != null ) && ( childList.size() > 1 ) )
119         {
120             throw new IllegalStateException( I18n.err( I18n.ERR_04159 ) );
121         }
122 
123         children = childList;
124     }
125 
126 
127     /**
128      * Gets the operator for this branch node.
129      * 
130      * @return the operator constant.
131      */
132     public AssertionType getOperator()
133     {
134         return AssertionType.NOT;
135     }
136 
137 
138     /**
139      * Tests whether or not this node is a disjunction (a OR'ed branch).
140      * 
141      * @return true if the operation is a OR, false otherwise.
142      */
143     public boolean isDisjunction()
144     {
145         return false;
146     }
147 
148 
149     /**
150      * Tests whether or not this node is a conjunction (a AND'ed branch).
151      * 
152      * @return true if the operation is a AND, false otherwise.
153      */
154     public boolean isConjunction()
155     {
156         return false;
157     }
158 
159 
160     /**
161      * Tests whether or not this node is a negation (a NOT'ed branch).
162      * 
163      * @return true if the operation is a NOT, false otherwise.
164      */
165     public boolean isNegation()
166     {
167         return true;
168     }
169 
170 
171     /**
172      * @see ExprNode#printRefinementToBuffer(StringBuffer)
173      * 
174      * @return The buffer in which the refinement has been appended
175      * @throws UnsupportedOperationException if this node isn't a part of a refinement.
176      */
177     public StringBuilder printRefinementToBuffer( StringBuilder buf )
178     {
179         buf.append( "not: " );
180 
181         // There is only one item for a not refinement
182         children.get( 0 ).printRefinementToBuffer( buf );
183 
184         return buf;
185     }
186 
187 
188     /**
189      * Gets the recursive prefix string represent of the filter from this node
190      * down.
191      * 
192      * @see java.lang.Object#toString()
193      * @return A string representing the AndNode
194      */
195     public String toString()
196     {
197         StringBuilder buf = new StringBuilder();
198         buf.append( "(!" );
199 
200         buf.append( super.toString() );
201 
202         buf.append( getFirstChild() );
203         buf.append( ')' );
204 
205         return buf.toString();
206     }
207 }