1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *
19 */
20 package org.apache.directory.api.ldap.model.filter;
21
22
23 import org.apache.directory.api.ldap.model.schema.AttributeType;
24
25
26 /**
27 * Abstract base class for leaf nodes within the expression filter tree.
28 *
29 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
30 */
31 public abstract class LeafNode extends AbstractExprNode
32 {
33 /** attributeType on which this leaf is based */
34 protected AttributeType attributeType;
35
36 /** attribute on which this leaf is based */
37 protected String attribute;
38
39
40 /**
41 * Creates a leaf node.
42 *
43 * @param attributeType the attribute this node is based on
44 * @param assertionType the type of this leaf node
45 */
46 protected LeafNode( AttributeType attributeType, AssertionType assertionType )
47 {
48 super( assertionType );
49 this.attributeType = attributeType;
50
51 if ( attributeType != null )
52 {
53 this.attribute = attributeType.getName();
54 isSchemaAware = true;
55 }
56 else
57 {
58 throw new NullPointerException( "Cannot create a Node with a null Attribute" );
59 }
60 }
61
62
63 /**
64 * Creates a leaf node.
65 *
66 * @param attribute the attribute this node is based on
67 * @param assertionType the type of this leaf node
68 */
69 protected LeafNode( String attribute, AssertionType assertionType )
70 {
71 super( assertionType );
72 this.attributeType = null;
73 this.attribute = attribute;
74 isSchemaAware = false;
75 }
76
77
78 /**
79 * Gets whether this node is a leaf - the answer is always true here.
80 *
81 * @return true always
82 */
83 @Override
84 public final boolean isLeaf()
85 {
86 return true;
87 }
88
89
90 /**
91 * Gets the attributeType this leaf node is based on.
92 *
93 * @return the attributeType asserted
94 */
95 public final AttributeType getAttributeType()
96 {
97 return attributeType;
98 }
99
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 }