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 */ 020package org.apache.directory.api.ldap.model.filter; 021 022 023import org.apache.directory.api.ldap.model.schema.AttributeType; 024 025 026/** 027 * Abstract base class for leaf nodes within the expression filter tree. 028 * 029 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 030 */ 031public abstract class LeafNode extends AbstractExprNode 032{ 033 /** attributeType on which this leaf is based */ 034 protected AttributeType attributeType; 035 036 /** attribute on which this leaf is based */ 037 protected String attribute; 038 039 040 /** 041 * Creates a leaf node. 042 * 043 * @param attributeType the attribute this node is based on 044 * @param assertionType the type of this leaf node 045 */ 046 protected LeafNode( AttributeType attributeType, AssertionType assertionType ) 047 { 048 super( assertionType ); 049 this.attributeType = attributeType; 050 051 if ( attributeType != null ) 052 { 053 this.attribute = attributeType.getName(); 054 isSchemaAware = true; 055 } 056 else 057 { 058 throw new NullPointerException( "Cannot create a Node with a null Attribute" ); 059 } 060 } 061 062 063 /** 064 * Creates a leaf node. 065 * 066 * @param attribute the attribute this node is based on 067 * @param assertionType the type of this leaf node 068 */ 069 protected LeafNode( String attribute, AssertionType assertionType ) 070 { 071 super( assertionType ); 072 this.attributeType = null; 073 this.attribute = attribute; 074 isSchemaAware = false; 075 } 076 077 078 /** 079 * Gets whether this node is a leaf - the answer is always true here. 080 * 081 * @return true always 082 */ 083 @Override 084 public final boolean isLeaf() 085 { 086 return true; 087 } 088 089 090 /** 091 * Gets the attributeType this leaf node is based on. 092 * 093 * @return the attributeType asserted 094 */ 095 public final AttributeType getAttributeType() 096 { 097 return attributeType; 098 } 099 100 101 /** 102 * Gets the attribute this leaf node is based on. 103 * 104 * @return the attribute asserted 105 */ 106 public final String getAttribute() 107 { 108 return attribute; 109 } 110 111 112 /** 113 * Sets the attributeType this leaf node is based on. 114 * 115 * @param attributeType the attributeType that is asserted by this filter node 116 */ 117 public void setAttributeType( AttributeType attributeType ) 118 { 119 this.attributeType = attributeType; 120 121 if ( attributeType != null ) 122 { 123 attribute = attributeType.getName(); 124 isSchemaAware = true; 125 } 126 } 127 128 129 /** 130 * Sets the attribute this leaf node is based on. 131 * 132 * @param attribute the attribute that is asserted by this filter node 133 */ 134 public void setAttribute( String attribute ) 135 { 136 this.attribute = attribute; 137 isSchemaAware = false; 138 } 139 140 141 /** 142 * @see ExprNode#accept( 143 *FilterVisitor) 144 * 145 * @param visitor the filter expression tree structure visitor 146 * @return The modified element 147 */ 148 @Override 149 public final Object accept( FilterVisitor visitor ) 150 { 151 if ( visitor.canVisit( this ) ) 152 { 153 return visitor.visit( this ); 154 } 155 else 156 { 157 return null; 158 } 159 } 160 161 162 /** 163 * @see Object#hashCode() 164 * @return the instance's hash code 165 */ 166 @Override 167 public int hashCode() 168 { 169 int h = 37; 170 171 h = h * 17 + super.hashCode(); 172 173 if ( attributeType != null ) 174 { 175 h = h * 17 + attributeType.hashCode(); 176 } 177 else 178 { 179 h = h * 17 + attribute.hashCode(); 180 } 181 182 return h; 183 } 184 185 186 /** 187 * @see java.lang.Object#equals(java.lang.Object) 188 */ 189 @Override 190 public boolean equals( Object other ) 191 { 192 if ( this == other ) 193 { 194 return true; 195 } 196 197 if ( !( other instanceof LeafNode ) ) 198 { 199 return false; 200 } 201 202 LeafNode otherNode = ( LeafNode ) other; 203 204 if ( other.getClass() != this.getClass() ) 205 { 206 return false; 207 } 208 209 if ( attributeType != null ) 210 { 211 return attributeType.equals( otherNode.getAttributeType() ); 212 } 213 else 214 { 215 return attribute.equalsIgnoreCase( otherNode.getAttribute() ); 216 } 217 } 218}