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  package org.apache.directory.server.core.partition.impl.btree.jdbm;
20  
21  
22  import org.apache.directory.server.core.cursor.InvalidCursorPositionException;
23  import org.apache.directory.server.xdbm.Tuple;
24  import org.apache.directory.server.xdbm.AbstractTupleCursor;
25  import org.apache.directory.server.core.avltree.AvlTree;
26  import org.apache.directory.server.core.avltree.AvlTreeCursor;
27  
28  
29  /**
30   * Cursor over a set of values for the same key which are store in an in
31   * memory AvlTree.  This Cursor is limited to the same key and it's tuples
32   * will always return the same key.
33   *
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   * @version $Rev$, $Date$
36   */
37  public class KeyTupleAvlCursor<K,V> extends AbstractTupleCursor<K,V>
38  {
39      private final AvlTreeCursor<V> wrapped;
40      private final K key;
41  
42      private Tuple<K,V> returnedTuple = new Tuple<K,V>();
43      private boolean valueAvailable;
44  
45  
46      /**
47       * Creates a Cursor over the tuples of an AvlTree.
48       *
49       * @param avlTree the AvlTree to build a Tuple returning Cursor over
50       * @param key the constant key for which values are returned
51       */
52      public KeyTupleAvlCursor( AvlTree<V> avlTree, K key )
53      {
54          this.key = key;
55          this.wrapped = new AvlTreeCursor<V>( avlTree );
56      }
57  
58  
59      private void clearValue()
60      {
61          returnedTuple.setKey( key );
62          returnedTuple.setValue( null );
63          valueAvailable = false;
64      }
65  
66  
67      public boolean available()
68      {
69          return valueAvailable;
70      }
71  
72  
73      public void beforeKey( K key ) throws Exception
74      {
75          throw new UnsupportedOperationException( "This cursor locks down the key so keywise advances are not allowed." );
76      }
77  
78  
79      public void afterKey( K key ) throws Exception
80      {
81          throw new UnsupportedOperationException( "This cursor locks down the key so keywise advances are not allowed." );
82      }
83  
84  
85      public void beforeValue( K key, V value ) throws Exception
86      {
87          checkNotClosed( "beforeValue()" );
88          if ( key != null && ! key.equals( this.key ) )
89          {
90              throw new UnsupportedOperationException( "This cursor locks down the key so keywise advances are not allowed." );
91          }
92  
93          wrapped.before( value );
94          clearValue();
95      }
96  
97  
98      public void afterValue( K key, V value ) throws Exception
99      {
100         checkNotClosed( "afterValue()" );
101         if ( key != null && ! key.equals( this.key ) )
102         {
103             throw new UnsupportedOperationException( "This cursor locks down the key so keywise advances are not allowed." );
104         }
105 
106         wrapped.after( value );
107         clearValue();
108     }
109 
110 
111     /**
112      * Positions this Cursor over the same keys before the value of the
113      * supplied element Tuple.  The supplied element Tuple's key is not
114      * considered at all.
115      *
116      * @param element the valueTuple who's value is used to position this Cursor
117      * @throws Exception if there are failures to position the Cursor
118      */
119     public void before( Tuple<K,V> element ) throws Exception
120     {
121         checkNotClosed( "before()" );
122         wrapped.before( element.getValue() );
123         clearValue();
124     }
125 
126 
127     public void after( Tuple<K,V> element ) throws Exception
128     {
129         checkNotClosed( "after()" );
130         wrapped.after( element.getValue() );
131         clearValue();
132     }
133 
134 
135     public void beforeFirst() throws Exception
136     {
137         checkNotClosed( "beforeFirst()" );
138         wrapped.beforeFirst();
139         clearValue();
140     }
141 
142 
143     public void afterLast() throws Exception
144     {
145         checkNotClosed( "afterLast()" );
146         wrapped.afterLast();
147         clearValue();
148     }
149 
150 
151     public boolean first() throws Exception
152     {
153         beforeFirst();
154         return next();
155     }
156 
157 
158     public boolean last() throws Exception
159     {
160         afterLast();
161         return previous();
162     }
163 
164 
165     public boolean previous() throws Exception
166     {
167         checkNotClosed( "previous()" );
168         if ( wrapped.previous() )
169         {
170             returnedTuple.setKey( key );
171             returnedTuple.setValue( wrapped.get() );
172             return valueAvailable = true;
173         }
174         else
175         {
176             clearValue();
177             return false;
178         }
179     }
180 
181 
182     public boolean next() throws Exception
183     {
184         checkNotClosed( "next()" );
185         if ( wrapped.next() )
186         {
187             returnedTuple.setKey( key );
188             returnedTuple.setValue( wrapped.get() );
189             return valueAvailable = true;
190         }
191         else
192         {
193             clearValue();
194             return false;
195         }
196     }
197 
198 
199     public Tuple<K,V> get() throws Exception
200     {
201         checkNotClosed( "get()" );
202         if ( valueAvailable )
203         {
204             return returnedTuple;
205         }
206 
207         throw new InvalidCursorPositionException();
208     }
209 
210 
211     public boolean isElementReused()
212     {
213         return true;
214     }
215 }