1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
31
32
33
34
35
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
48
49
50
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
113
114
115
116
117
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 }