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 attributeType 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 public final boolean isLeaf()
84 {
85 return true;
86 }
87
88
89 /**
90 * Gets the attributeType this leaf node is based on.
91 *
92 * @return the attributeType asserted
93 */
94 public final AttributeType getAttributeType()
95 {
96 return attributeType;
97 }
98
99
100 /**
101 * Gets the attribute this leaf node is based on.
102 *
103 * @return the attribute asserted
104 */
105 public final String getAttribute()
106 {
107 return attribute;
108 }
109
110
111 /**
112 * Sets the attributeType this leaf node is based on.
113 *
114 * @param attributeType the attributeType that is asserted by this filter node
115 */
116 public void setAttributeType( AttributeType attributeType )
117 {
118 this.attributeType = attributeType;
119
120 if ( attributeType != null )
121 {
122 attribute = attributeType.getName();
123 isSchemaAware = true;
124 }
125 }
126
127
128 /**
129 * Sets the attribute this leaf node is based on.
130 *
131 * @param attribute the attribute that is asserted by this filter node
132 */
133 public void setAttribute( String attribute )
134 {
135 this.attribute = attribute;
136 isSchemaAware = false;
137 }
138
139
140 /**
141 * @see ExprNode#accept(
142 *FilterVisitor)
143 *
144 * @param visitor the filter expression tree structure visitor
145 * @return The modified element
146 */
147 public final Object accept( FilterVisitor visitor )
148 {
149 if ( visitor.canVisit( this ) )
150 {
151 return visitor.visit( this );
152 }
153 else
154 {
155 return null;
156 }
157 }
158
159
160 /**
161 * @see Object#hashCode()
162 * @return the instance's hash code
163 */
164 public int hashCode()
165 {
166 int h = 37;
167
168 h = h * 17 + super.hashCode();
169
170 if ( attributeType != null )
171 {
172 h = h * 17 + attributeType.hashCode();
173 }
174 else
175 {
176 h = h * 17 + attribute.hashCode();
177 }
178
179 return h;
180 }
181
182
183 /**
184 * @see java.lang.Object#equals(java.lang.Object)
185 */
186 public boolean equals( Object other )
187 {
188 if ( this == other )
189 {
190 return true;
191 }
192
193 if ( !( other instanceof LeafNode ) )
194 {
195 return false;
196 }
197
198 LeafNode otherNode = ( LeafNode ) other;
199
200 if ( other.getClass() != this.getClass() )
201 {
202 return false;
203 }
204
205 if ( attributeType != null )
206 {
207 return attributeType.equals( otherNode.getAttributeType() );
208 }
209 else
210 {
211 return attribute.equalsIgnoreCase( otherNode.getAttribute() );
212 }
213 }
214 }