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