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