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.i18n.I18n;
023
024
025/**
026 * Node used for the application of arbitrary predicates on return candidates.
027 * Applies dynamic and programatic criteria for the selection of candidates for
028 * return. Nodes of this type may be introduced into the filter expression to
029 * provided the opportunity to constrain the search further without altering the
030 * search algorithm.
031 * 
032 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
033 */
034public abstract class AssertionNode extends AbstractExprNode
035{
036    /** The assertion or predicate to apply */
037    private final Assertion assertion;
038
039    /** Description of assertion for polish printouts */
040    private final String desc;
041
042
043    // ------------------------------------------------------------------------
044    // C O N S T R U C T O R S
045    // ------------------------------------------------------------------------
046
047
048    /**
049     * Creates an AssertionNode using an arbitrary candidate assertion.
050     * 
051     * @param assertion the arbitrary selection logic.
052     */
053    public AssertionNode( Assertion assertion )
054    {
055        this( assertion, "ASSERTION" );
056        
057        isSchemaAware = true;
058    }
059
060
061    /**
062     * Creates an AssertionNode using an arbitrary candidate assertion with a
063     * descriptions used for filter AST walker dumps.
064     * 
065     * @param assertion the arbitrary selection logic.
066     * @param desc the printout representation for filter prints.
067     */
068    public AssertionNode( Assertion assertion, String desc )
069    {
070        super( AssertionType.ASSERTION );
071        this.desc = desc;
072        this.assertion = assertion;
073
074        /*
075         * We never want this node to ever make it to the point of becoming a
076         * candidate for use in an enumeration so we set the scan count to the
077         * maximum value.
078         */
079        set( "count", Long.MAX_VALUE );
080    }
081
082    /**
083     * Makes a full clone in new memory space of the current node and children
084     * 
085     * @return the clone
086     */
087    @Override public ExprNode clone()
088    {
089        return (ExprNode)super.clone();
090    }
091    
092
093
094    /**
095     * Gets the Assertion used by this assertion node.
096     * 
097     * @return the assertion used by this node
098     */
099    public Assertion getAssertion()
100    {
101        return assertion;
102    }
103
104
105    // ------------------------------------------------------------------------
106    // A B S T R A C T M E T H O D I M P L E M E N T A T I O N S
107    // ------------------------------------------------------------------------
108
109    
110    /**
111     * Always returns true since an AssertionNode has no children.
112     * 
113     * @see ExprNode#isLeaf()
114     * @return true if the node is a leaf,false otherwise
115     */
116    public boolean isLeaf()
117    {
118        return true;
119    }
120
121
122    /**
123     * @see ExprNode#printRefinementToBuffer(StringBuilder) 
124     */
125    public StringBuilder printRefinementToBuffer( StringBuilder buf ) throws UnsupportedOperationException
126    {
127        throw new UnsupportedOperationException( I18n.err( I18n.ERR_04145 ) );
128    }
129
130
131    /**
132     * {@inheritDoc}
133     */
134    @Override
135    public boolean equals( Object obj )
136    {
137        if ( obj == this )
138        {
139            return true;
140        }
141
142        if ( ( obj == null ) || !( obj instanceof AssertionNode ) )
143        {
144            return false;
145        }
146        AssertionNode that = ( AssertionNode ) obj;
147        if ( assertion == null ) {
148            if ( that.assertion != null)
149            {
150                return false;
151            }
152        }
153        else
154        {
155            if ( !assertion.equals( that.assertion ) )
156            {
157                return false;
158            }
159        }
160        if ( desc == null ) {
161            if ( that.desc != null)
162            {
163                return false;
164            }
165        }
166        else
167        {
168            if ( !desc.equals( that.desc ) )
169            {
170                return false;
171            }
172        }
173        return super.equals( obj );
174    }
175
176
177    /**
178     * @see Object#hashCode()
179     * @return the instance's hash code 
180     */
181    @Override
182    public int hashCode()
183    {
184        int h = 37;
185        
186        h = h*17 + super.hashCode();
187        h = h*17 + ( assertion != null ? assertion.hashCode() : 0 );
188        h = h*17 + ( desc != null ? desc.hashCode() : 0 );
189        
190        return h;
191    }
192
193
194    /**
195     * @see ExprNode#accept(
196     *FilterVisitor)
197     */
198    public Object accept( FilterVisitor visitor )
199    {
200        return visitor.visit( this );
201    }
202
203
204    /**
205     * @see Object#toString
206     * @return A string representing the AndNode
207     */
208    public String toString()
209    {
210        StringBuilder buf = new StringBuilder();
211        
212        buf.append( "(@" );
213        buf.append( desc );
214        buf.append( super.toString() );
215        buf.append( ')' );
216        
217        return buf.toString();
218    }
219}