View Javadoc
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.i18n.I18n;
24  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
25  import org.apache.directory.api.ldap.model.entry.Value;
26  import org.apache.directory.api.ldap.model.schema.AttributeType;
27  
28  
29  /**
30   * A simple assertion value node.
31   * 
32   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33   */
34  public abstract class SimpleNode<T> extends LeafNode
35  {
36      /** the value */
37      protected Value<T> value;
38  
39      /** Constants for comparisons : > */
40      public static final boolean EVAL_GREATER = true;
41  
42      /** Constants for comparisons : < */
43      public static final boolean EVAL_LESSER = false;
44  
45  
46      /**
47       * Creates a new SimpleNode object.
48       * 
49       * @param attribute the attribute name
50       * @param value the value to test for
51       * @param assertionType the type of assertion represented by this ExprNode
52       */
53      protected SimpleNode( String attribute, Value<T> value, AssertionType assertionType )
54      {
55          super( attribute, assertionType );
56          this.value = value;
57      }
58  
59  
60      /**
61       * Creates a new SimpleNode object.
62       * 
63       * @param attribute the attribute name
64       * @param value the value to test for
65       * @param assertionType the type of assertion represented by this ExprNode
66       */
67      protected SimpleNode( AttributeType attributeType, Value<T> value, AssertionType assertionType )
68      {
69          super( attributeType, assertionType );
70          this.value = value;
71      }
72  
73  
74      /**
75       * Makes a full clone in new memory space of the current node and children
76       */
77      @SuppressWarnings("unchecked")
78      @Override
79      public ExprNode clone()
80      {
81          ExprNode clone = super.clone();
82  
83          // Clone the value
84          ( ( SimpleNode<T> ) clone ).value = value.clone();
85  
86          return clone;
87      }
88  
89  
90      /**
91       * Gets the value.
92       * 
93       * @return the value
94       */
95      public final Value<T> getValue()
96      {
97          return value;
98      }
99  
100 
101     /** 
102      * @return representation of value, escaped for use in a filter if required 
103      */
104     public Value<?> getEscapedValue()
105     {
106         return escapeFilterValue( value );
107     }
108 
109 
110     /**
111      * Sets the value of this node.
112      * 
113      * @param value the value for this node
114      */
115     public void setValue( Value<T> value )
116     {
117         this.value = value;
118     }
119 
120 
121     /**
122      * Pretty prints this expression node along with annotation information.
123      *
124      * TODO - perhaps this belong in some utility class?
125      *
126      * @param buf the buffer to print into
127      * @return the same buf argument returned for call chaining
128      */
129     public StringBuilder printToBuffer( StringBuilder buf )
130     {
131         if ( ( null != getAnnotations() ) && getAnnotations().containsKey( "count" ) )
132         {
133             buf.append( ":[" );
134             buf.append( getAnnotations().get( "count" ).toString() );
135             buf.append( "] " );
136         }
137 
138         buf.append( ')' );
139 
140         return buf;
141     }
142 
143 
144     /**
145      * @see ExprNode#printRefinementToBuffer(StringBuilder)
146      * @return The buffer in which the refinement has been appended
147      * @throws UnsupportedOperationException if this node isn't a part of a refinement.
148      */
149     public StringBuilder printRefinementToBuffer( StringBuilder buf )
150     {
151         if ( isSchemaAware )
152         {
153             if ( !attributeType.getOid().equals( SchemaConstants.OBJECT_CLASS_AT_OID ) )
154             {
155                 throw new UnsupportedOperationException( I18n.err( I18n.ERR_04162, attribute ) );
156             }
157         }
158         else
159         {
160             if ( ( attribute == null )
161                 || !( SchemaConstants.OBJECT_CLASS_AT.equalsIgnoreCase( attribute )
162                 || SchemaConstants.OBJECT_CLASS_AT_OID.equalsIgnoreCase( attribute ) ) )
163             {
164                 throw new UnsupportedOperationException( I18n.err( I18n.ERR_04162, attribute ) );
165             }
166         }
167 
168         buf.append( "item: " ).append( value );
169 
170         return buf;
171     }
172 
173 
174     /**
175      * @see Object#hashCode()
176      * @return the instance's hash code 
177      */
178     public int hashCode()
179     {
180         int h = 37;
181 
182         h = h * 17 + super.hashCode();
183         h = h * 17 + ( value == null ? 0 : value.hashCode() );
184 
185         return h;
186     }
187 
188 
189     /**
190      * @see java.lang.Object#equals(java.lang.Object)
191      */
192     public boolean equals( Object other )
193     {
194         if ( this == other )
195         {
196             return true;
197         }
198 
199         if ( !( other instanceof SimpleNode<?> ) )
200         {
201             return false;
202         }
203 
204         if ( other.getClass() != this.getClass() )
205         {
206             return false;
207         }
208 
209         if ( !super.equals( other ) )
210         {
211             return false;
212         }
213 
214         SimpleNode<?> otherNode = ( SimpleNode<?> ) other;
215 
216         if ( value == null )
217         {
218             return otherNode.value == null;
219         }
220         else
221         {
222             return value.equals( otherNode.value );
223         }
224     }
225 }