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