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   * @param <T> The Value type
33   * 
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   */
36  public abstract class SimpleNode<T> extends LeafNode
37  {
38      /** the value */
39      protected Value<T> value;
40  
41      /** Constants for comparisons : &gt; */
42      public static final boolean EVAL_GREATER = true;
43  
44      /** Constants for comparisons : &lt; */
45      public static final boolean EVAL_LESSER = false;
46  
47  
48      /**
49       * Creates a new SimpleNode object.
50       * 
51       * @param attribute the attribute name
52       * @param value the value to test for
53       * @param assertionType the type of assertion represented by this ExprNode
54       */
55      protected SimpleNode( String attribute, Value<T> value, AssertionType assertionType )
56      {
57          super( attribute, assertionType );
58          this.value = value;
59      }
60  
61  
62      /**
63       * Creates a new SimpleNode object.
64       * 
65       * @param attributeType the attribute name
66       * @param value the value to test for
67       * @param assertionType the type of assertion represented by this ExprNode
68       */
69      protected SimpleNode( AttributeType attributeType, Value<T> value, AssertionType assertionType )
70      {
71          super( attributeType, assertionType );
72          this.value = value;
73      }
74  
75  
76      /**
77       * Makes a full clone in new memory space of the current node and children
78       */
79      @SuppressWarnings("unchecked")
80      @Override
81      public ExprNode clone()
82      {
83          ExprNode clone = super.clone();
84  
85          // Clone the value
86          ( ( SimpleNode<T> ) clone ).value = value.clone();
87  
88          return clone;
89      }
90  
91  
92      /**
93       * Gets the value.
94       * 
95       * @return the value
96       */
97      public final Value<T> getValue()
98      {
99          return value;
100     }
101 
102 
103     /** 
104      * @return representation of value, escaped for use in a filter if required 
105      */
106     public Value<?> getEscapedValue()
107     {
108         return escapeFilterValue( value );
109     }
110 
111 
112     /**
113      * Sets the value of this node.
114      * 
115      * @param value the value for this node
116      */
117     public void setValue( Value<T> value )
118     {
119         this.value = value;
120     }
121 
122 
123     /**
124      * Pretty prints this expression node along with annotation information.
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     @Override
150     public StringBuilder printRefinementToBuffer( StringBuilder buf )
151     {
152         if ( isSchemaAware )
153         {
154             if ( !attributeType.getOid().equals( SchemaConstants.OBJECT_CLASS_AT_OID ) )
155             {
156                 throw new UnsupportedOperationException( I18n.err( I18n.ERR_04162, attribute ) );
157             }
158         }
159         else
160         {
161             if ( ( attribute == null )
162                 || !( SchemaConstants.OBJECT_CLASS_AT.equalsIgnoreCase( attribute )
163                 || SchemaConstants.OBJECT_CLASS_AT_OID.equalsIgnoreCase( attribute ) ) )
164             {
165                 throw new UnsupportedOperationException( I18n.err( I18n.ERR_04162, attribute ) );
166             }
167         }
168 
169         buf.append( "item: " ).append( value );
170 
171         return buf;
172     }
173 
174 
175     /**
176      * @see Object#hashCode()
177      * @return the instance's hash code 
178      */
179     @Override
180     public int hashCode()
181     {
182         int h = 37;
183 
184         h = h * 17 + super.hashCode();
185         h = h * 17 + ( value == null ? 0 : value.hashCode() );
186 
187         return h;
188     }
189 
190 
191     /**
192      * @see java.lang.Object#equals(java.lang.Object)
193      */
194     @Override
195     public boolean equals( Object other )
196     {
197         if ( this == other )
198         {
199             return true;
200         }
201 
202         if ( !( other instanceof SimpleNode<?> ) )
203         {
204             return false;
205         }
206 
207         if ( other.getClass() != this.getClass() )
208         {
209             return false;
210         }
211 
212         if ( !super.equals( other ) )
213         {
214             return false;
215         }
216 
217         SimpleNode<?> otherNode = ( SimpleNode<?> ) other;
218 
219         if ( value == null )
220         {
221             return otherNode.value == null;
222         }
223         else
224         {
225             return value.equals( otherNode.value );
226         }
227     }
228 }