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