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    import org.apache.directory.shared.i18n.I18n;
024    import org.apache.directory.shared.ldap.constants.SchemaConstants;
025    import org.apache.directory.shared.ldap.entry.Value;
026    
027    
028    /**
029     * A simple assertion value node.
030     * 
031     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
032     * @version $Revision: 912436 $
033     */
034    public abstract class SimpleNode<T> extends LeafNode
035    {
036        /** the value */
037        protected Value<T> value;
038    
039        /* TODO - why are these here if not used? */
040        /** Constants for comparisons : > */
041        public static final boolean EVAL_GREATER = true;
042        
043        /* TODO - why are these here if not used? */
044        /** Constants for comparisons : < */
045        public static final boolean EVAL_LESSER = false;
046    
047    
048        /**
049         * Creates a new SimpleNode object.
050         * 
051         * @param attribute the attribute name
052         * @param value the value to test for
053         * @param assertionType the type of assertion represented by this ExprNode
054         */
055        protected SimpleNode( String attribute, Value<T> value, AssertionType assertionType )
056        {
057            super( attribute, assertionType );
058            this.value = value;
059        }
060    
061        
062        /**
063         * Clone the Node
064         */
065        @Override public ExprNode clone()
066        {
067            ExprNode clone = super.clone();
068            
069            // Clone the value
070            ((SimpleNode<T>)clone).value = value.clone(); 
071            
072            return clone;
073        }
074    
075    
076        /**
077         * Gets the value.
078         * 
079         * @return the value
080         */
081        public final Value<T> getValue()
082        {
083            return value;
084        }
085    
086        /** 
087         * @return representation of value, escaped for use in a filter if required 
088         */
089        public Value<?> getEscapedValue()
090        {
091            return AbstractExprNode.escapeFilterValue( value );
092        }
093    
094    
095        /**
096         * Sets the value of this node.
097         * 
098         * @param value the value for this node
099         */
100        public void setValue( Value<T> value )
101        {
102            this.value = value;
103        }
104    
105    
106        /**
107         * Pretty prints this expression node along with annotation information.
108         *
109         * TODO - perhaps this belong in some utility class?
110         *
111         * @param buf the buffer to print into
112         * @return the same buf argument returned for call chaining
113         */
114        public StringBuilder printToBuffer( StringBuilder buf )
115        {
116            if ( ( null != getAnnotations() ) && getAnnotations().containsKey( "count" ) )
117            {
118                buf.append( ":[" );
119                buf.append( getAnnotations().get( "count" ).toString() );
120                buf.append( "] " );
121            }
122    
123            buf.append( ')' );
124    
125            return buf;
126        }
127    
128    
129        /**
130         * @see ExprNode#printRefinementToBuffer(StringBuilder)
131         * @return The buffer in which the refinement has been appended
132         * @throws UnsupportedOperationException if this node isn't a part of a refinement.
133         */
134        public StringBuilder printRefinementToBuffer( StringBuilder buf )
135        {
136            if ( getAttribute() == null || !SchemaConstants.OBJECT_CLASS_AT.equalsIgnoreCase( getAttribute() ) )
137            {
138                throw new UnsupportedOperationException( I18n.err( I18n.ERR_04162, getAttribute() ) );
139            }
140    
141            buf.append( "item: " ).append( value );
142    
143            return buf;
144        }
145    
146    
147        /**
148         * @see Object#hashCode()
149         * @return the instance's hash code 
150         */
151        public int hashCode()
152        {
153            int h = 37;
154    
155            h = h * 17 + super.hashCode();
156            h = h * 17 + ( value == null ? 0 : value.hashCode() );
157    
158            return h;
159        }
160    
161    
162        /*
163         * (non-Javadoc)
164         * 
165         * @see java.lang.Object#equals(java.lang.Object)
166         */
167        public boolean equals( Object other )
168        {
169            if ( this == other )
170            {
171                return true;
172            }
173    
174            if ( !( other instanceof SimpleNode ) )
175            {
176                return false;
177            }
178    
179            if ( other.getClass() != this.getClass() )
180            {
181                return false;
182            }
183    
184            if ( !super.equals( other ) )
185            {
186                return false;
187            }
188    
189            SimpleNode<?> otherNode = ( SimpleNode<?> ) other;
190    
191            if ( value == null )
192            {
193                return otherNode.value == null;
194            }
195            else
196            {
197                return value.equals( otherNode.value );
198            }
199        }
200    }