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