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  
21  package org.apache.directory.api.ldap.extras.controls.vlv;
22  
23  
24  import java.util.Arrays;
25  
26  import org.apache.directory.api.ldap.model.message.controls.AbstractControl;
27  import org.apache.directory.api.util.Strings;
28  
29  
30  /**
31   * Virtual List View control as specified in draft-ietf-ldapext-ldapv3-vlv-09.
32   * 
33   *  VirtualListViewRequest ::= SEQUENCE {
34   *         beforeCount    INTEGER (0..maxInt),
35   *         afterCount     INTEGER (0..maxInt),
36   *         target       CHOICE {
37   *                        byOffset        [0] SEQUENCE {
38   *                             offset          INTEGER (1 .. maxInt),
39   *                             contentCount    INTEGER (0 .. maxInt) },
40   *                        greaterThanOrEqual [1] AssertionValue },
41   *         contextID     OCTET STRING OPTIONAL }
42   * 
43   * Simplistic implementation that only supports byOffset choice.
44   *
45   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
46   */
47  public class VirtualListViewRequestImpl extends AbstractControl implements VirtualListViewRequest
48  {
49      private int beforeCount;
50      private int afterCount;
51      private int offset;
52      private int contentCount;
53      private byte[] contextId;
54  
55      /** The assertionValue */
56      private byte[] assertionValue;
57  
58      /** A flag used for the target. It default to OFFSET */
59      private boolean targetType = OFFSET;
60  
61      private static final boolean OFFSET = true;
62      private static final boolean ASSERTION_VALUE = false;
63  
64  
65      /**
66       * Creates a new instance of VirtualListViewRequestImpl.
67       */
68      public VirtualListViewRequestImpl()
69      {
70          super( OID );
71      }
72  
73  
74      /**
75       * {@inheritDoc}
76       */
77      @Override
78      public int getBeforeCount()
79      {
80          return beforeCount;
81      }
82  
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      public void setBeforeCount( int beforeCount )
89      {
90          this.beforeCount = beforeCount;
91      }
92  
93  
94      /**
95       * {@inheritDoc}
96       */
97      @Override
98      public int getAfterCount()
99      {
100         return afterCount;
101     }
102 
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public void setAfterCount( int afterCount )
109     {
110         this.afterCount = afterCount;
111     }
112 
113 
114     /**
115      * {@inheritDoc}
116      */
117     @Override
118     public int getOffset()
119     {
120         return offset;
121     }
122 
123 
124     /**
125      * {@inheritDoc}
126      */
127     @Override
128     public void setOffset( int offset )
129     {
130         this.offset = offset;
131         targetType = OFFSET;
132     }
133 
134 
135     /**
136      * {@inheritDoc}
137      */
138     @Override
139     public int getContentCount()
140     {
141         return contentCount;
142     }
143 
144 
145     /**
146      * {@inheritDoc}
147      */
148     @Override
149     public void setContentCount( int contentCount )
150     {
151         this.contentCount = contentCount;
152     }
153 
154 
155     /**
156      * {@inheritDoc}
157      */
158     @Override
159     public byte[] getAssertionValue()
160     {
161         return assertionValue;
162     }
163 
164 
165     /**
166      * {@inheritDoc}
167      */
168     @Override
169     public void setAssertionValue( byte[] assertionValue )
170     {
171         this.assertionValue = assertionValue;
172         targetType = ASSERTION_VALUE;
173     }
174 
175 
176     /**
177      * {@inheritDoc}
178      */
179     @Override
180     public byte[] getContextId()
181     {
182         return contextId;
183     }
184 
185 
186     /**
187      * {@inheritDoc}
188      */
189     @Override
190     public void setContextId( byte[] contextId )
191     {
192         this.contextId = contextId;
193     }
194 
195 
196     /**
197      * {@inheritDoc}
198      */
199     @Override
200     public boolean hasOffset()
201     {
202         return targetType == OFFSET;
203     }
204 
205 
206     /**
207      * {@inheritDoc}
208      */
209     @Override
210     public boolean hasAssertionValue()
211     {
212         return targetType == ASSERTION_VALUE;
213     }
214 
215 
216     /**
217      * @see Object#hashCode()
218      */
219     @Override
220     public int hashCode()
221     {
222         int h = super.hashCode();
223 
224         h = h * 37 + beforeCount;
225         h = h * 37 + afterCount;
226         h = h * 37 + offset;
227         h = h * 37 + contentCount;
228 
229         if ( contextId != null )
230         {
231             for ( byte b : contextId )
232             {
233                 h = h * 17 + b;
234             }
235         }
236 
237         return h;
238     }
239 
240 
241     /**
242      * @see Object#equals(Object)
243      */
244     @Override
245     public boolean equals( Object o )
246     {
247         if ( !super.equals( o ) )
248         {
249             return false;
250         }
251 
252         VirtualListViewRequestImpl otherControl = ( VirtualListViewRequestImpl ) o;
253 
254         return ( beforeCount == otherControl.getBeforeCount() ) &&
255             ( afterCount == otherControl.getAfterCount() ) &&
256             ( offset == otherControl.getOffset() ) &&
257             ( contentCount == otherControl.getContentCount() ) &&
258             Arrays.equals( contextId, otherControl.getContextId() );
259     }
260 
261 
262     /**
263      * Return a String representing this VirtualListViewRequestImpl.
264      */
265     public String toString()
266     {
267         StringBuffer sb = new StringBuffer();
268 
269         sb.append( "    Virtual List View Request Control\n" );
270         sb.append( "        oid : " ).append( getOid() ).append( '\n' );
271         sb.append( "        critical : " ).append( isCritical() ).append( '\n' );
272         sb.append( "        beforeCount   : '" ).append( beforeCount ).append( "'\n" );
273         sb.append( "        afterCount   : '" ).append( afterCount ).append( "'\n" );
274         sb.append( "        target : \n" );
275 
276         if ( targetType == OFFSET )
277         {
278             sb.append( "            offset   : '" ).append( offset ).append( "'\n" );
279             sb.append( "            contentCount   : '" ).append( contentCount ).append( "'\n" );
280         }
281         else
282         {
283             sb.append( "            assertionValue : '" ).append( Strings.utf8ToString( assertionValue ) )
284                 .append( "'\n" );
285 
286         }
287 
288         if ( contextId != null )
289         {
290             sb.append( "        contextID   : '" ).append( Strings.dumpBytes( contextId ) ).append( "'\n" );
291         }
292 
293         return sb.toString();
294     }
295 }