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 OR 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 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 * Clone the Node 058 */ 059 @Override public ExprNode clone() 060 { 061 return super.clone(); 062 } 063 064 065 /** 066 * Creates an empty OrNode 067 */ 068 public OrNode() 069 { 070 super( AssertionType.OR ); 071 } 072 073 074 /** 075 * Gets the operator for this branch node. 076 * 077 * @return the operator constant. 078 */ 079 public AssertionType getOperator() 080 { 081 return AssertionType.OR; 082 } 083 084 085 /** 086 * Tests whether or not this node is a disjunction (a OR'ed branch). 087 * 088 * @return true if the operation is a OR, false otherwise. 089 */ 090 public boolean isDisjunction() 091 { 092 return true; 093 } 094 095 096 /** 097 * Tests whether or not this node is a conjunction (a AND'ed branch). 098 * 099 * @return true if the operation is a AND, false otherwise. 100 */ 101 public boolean isConjunction() 102 { 103 return false; 104 } 105 106 107 /** 108 * Tests whether or not this node is a negation (a NOT'ed branch). 109 * 110 * @return true if the operation is a NOT, false otherwise. 111 */ 112 public boolean isNegation() 113 { 114 return false; 115 } 116 117 118 /** 119 * @see ExprNode#printRefinementToBuffer(StringBuffer) 120 * 121 * @return The buffer in which the refinement has been appended 122 * @throws UnsupportedOperationException if this node isn't a part of a refinement. 123 */ 124 public StringBuilder printRefinementToBuffer( StringBuilder buf ) 125 { 126 buf.append( "or: {" ); 127 boolean isFirst = true; 128 129 for ( ExprNode node:children ) 130 { 131 if ( isFirst ) 132 { 133 isFirst = false; 134 } 135 else 136 { 137 buf.append( ", " ); 138 } 139 140 node.printRefinementToBuffer( buf ); 141 } 142 143 buf.append( '}' ); 144 145 return buf; 146 } 147 148 /** 149 * Gets the recursive prefix string represent of the filter from this node 150 * down. 151 * 152 * @see java.lang.Object#toString() 153 * @return A string representing the AndNode 154 */ 155 public String toString() 156 { 157 StringBuilder buf = new StringBuilder(); 158 buf.append( "(|" ); 159 160 buf.append( super.toString() ); 161 162 for ( ExprNode child:getChildren() ) 163 { 164 buf.append( child ); 165 } 166 167 buf.append( ')' ); 168 169 return buf.toString(); 170 } 171 172 173 /** 174 * @see Object#hashCode() 175 * @return the instance's hash code 176 */ 177 public int hashCode() 178 { 179 int hash = 37; 180 hash = hash*17 + AssertionType.OR.hashCode(); 181 hash = hash*17 + ( annotations == null ? 0 : annotations.hashCode() ); 182 return hash; 183 } 184 185 186 /* 187 * (non-Javadoc) 188 * 189 * @see java.lang.Object#equals(java.lang.Object) 190 */ 191 public boolean equals( Object other ) 192 { 193 if ( this == other ) 194 { 195 return true; 196 } 197 198 if ( !( other instanceof OrNode ) ) 199 { 200 return false; 201 } 202 203 OrNode otherExprNode = ( OrNode ) other; 204 205 List<ExprNode> otherChildren = otherExprNode.getChildren(); 206 207 if ( otherChildren == children ) 208 { 209 return true; 210 } 211 212 if ( children.size() != otherChildren.size() ) 213 { 214 return false; 215 } 216 217 for ( int i = 0; i < children.size(); i++ ) 218 { 219 ExprNode child = children.get( i ); 220 ExprNode otherChild = otherChildren.get( i ); 221 222 if ( !child.equals( otherChild ) ) 223 { 224 return false; 225 } 226 } 227 228 return true; 229 } 230 }