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;
20
21
22 import org.apache.directory.server.core.cursor.InvalidCursorPositionException;
23 import org.apache.directory.server.core.cursor.AbstractCursor;
24 import org.apache.directory.server.xdbm.Tuple;
25 import org.apache.directory.shared.ldap.NotImplementedException;
26
27 import java.util.Arrays;
28 import java.util.List;
29
30
31
32
33
34
35
36
37 public class ValueArrayCursor<K,V> extends AbstractCursor<Tuple>
38 {
39 private static final int BEFORE_FIRST = -1;
40
41 private final K key;
42 private final List<V> values;
43 private final Tuple<K,V> tuple = new Tuple<K,V>();
44
45 private int pos = BEFORE_FIRST;
46
47
48 public ValueArrayCursor( final K key, final V[] values )
49 {
50 this.key = key;
51 this.tuple.setKey( key );
52 this.values = Arrays.asList( values );
53 }
54
55
56 public ValueArrayCursor( final K key, final List<V> values )
57 {
58 this.key = key;
59 this.tuple.setKey( key );
60 this.values = values;
61 }
62
63
64 public boolean available()
65 {
66 return inRangeOnValue();
67 }
68
69
70 public void before( Tuple element ) throws Exception
71 {
72 throw new NotImplementedException();
73 }
74
75
76 public void after( Tuple element ) throws Exception
77 {
78 throw new NotImplementedException();
79 }
80
81
82 public void beforeFirst() throws Exception
83 {
84 checkNotClosed( "beforeFirst()" );
85 pos = BEFORE_FIRST;
86 }
87
88
89 public void afterLast() throws Exception
90 {
91 checkNotClosed( "afterLast()" );
92 pos = values.size();
93 }
94
95
96 public boolean absolute( int absolutePosition ) throws Exception
97 {
98 checkNotClosed( "absolute()" );
99 if ( absolutePosition >= values.size() )
100 {
101 pos = values.size();
102 return false;
103 }
104
105 if ( absolutePosition < 0 )
106 {
107 pos = BEFORE_FIRST;
108 return false;
109 }
110
111 pos = absolutePosition;
112 return true;
113 }
114
115
116 public boolean first() throws Exception
117 {
118 checkNotClosed( "first()" );
119 pos = 0;
120 return true;
121 }
122
123
124 public boolean last() throws Exception
125 {
126 checkNotClosed( "last()" );
127 pos = values.size() - 1;
128 return true;
129 }
130
131
132 public boolean isFirst() throws Exception
133 {
134 checkNotClosed( "isFirst()" );
135 return pos == 0;
136 }
137
138
139 public boolean isLast() throws Exception
140 {
141 checkNotClosed( "isLast()" );
142 return pos == values.size() - 1;
143 }
144
145
146 public boolean isAfterLast() throws Exception
147 {
148 checkNotClosed( "isAfterLast()" );
149 return pos == values.size();
150 }
151
152
153 public boolean isBeforeFirst() throws Exception
154 {
155 checkNotClosed( "isBeforeFirst()" );
156 return pos == BEFORE_FIRST;
157 }
158
159
160 public boolean previous() throws Exception
161 {
162 checkNotClosed( "previous()" );
163 if ( pos <= BEFORE_FIRST )
164 {
165 return false;
166 }
167
168 pos--;
169 return inRangeOnValue();
170 }
171
172
173 private boolean inRangeOnValue()
174 {
175 return pos > BEFORE_FIRST && pos < values.size();
176 }
177
178
179 public boolean next() throws Exception
180 {
181 checkNotClosed( "next()" );
182 if ( pos >= values.size() )
183 {
184 return false;
185 }
186
187 pos++;
188 return inRangeOnValue();
189 }
190
191
192 public Tuple get() throws Exception
193 {
194 checkNotClosed( "get()" );
195 if ( inRangeOnValue() )
196 {
197 return tuple.setBoth( key, values.get( pos ) );
198 }
199
200 throw new InvalidCursorPositionException( "Cursor pos (" + pos
201 + ") not in value range [0-" + ( values.size() - 1 )+ "]" );
202 }
203
204
205 public boolean isElementReused()
206 {
207 return true;
208 }
209 }