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