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.cursor;
20
21
22 import java.util.Comparator;
23
24
25
26
27
28
29
30
31 public class SingletonCursor<E> extends AbstractCursor<E>
32 {
33 private boolean beforeFirst = true;
34 private boolean afterLast;
35 private boolean onSingleton;
36 private final Comparator<E> comparator;
37 private final E singleton;
38
39
40 public SingletonCursor( E singleton )
41 {
42 this( singleton, null );
43 }
44
45
46 public SingletonCursor( E singleton, Comparator<E> comparator )
47 {
48 this.singleton = singleton;
49 this.comparator = comparator;
50 }
51
52
53 public boolean available()
54 {
55 return onSingleton;
56 }
57
58
59 public void before( E element ) throws Exception
60 {
61 checkNotClosed( "before()" );
62
63 if ( comparator == null )
64 {
65 throw new UnsupportedOperationException(
66 "Without a comparator I cannot advance to just before the specified element." );
67 }
68
69 int comparison = comparator.compare( singleton, element );
70
71 if ( comparison < 0 )
72 {
73 first();
74 }
75 else
76 {
77 beforeFirst();
78 }
79 }
80
81
82 public void after( E element ) throws Exception
83 {
84 checkNotClosed( "after()" );
85
86 if ( comparator == null )
87 {
88 throw new UnsupportedOperationException(
89 "Without a comparator I cannot advance to just after the specified element." );
90 }
91
92 int comparison = comparator.compare( singleton, element );
93
94 if ( comparison > 0 )
95 {
96 first();
97 }
98 else
99 {
100 afterLast();
101 }
102 }
103
104
105 public void beforeFirst() throws Exception
106 {
107 checkNotClosed( "()" );
108 beforeFirst = true;
109 afterLast = false;
110 onSingleton = false;
111 }
112
113
114 public void afterLast() throws Exception
115 {
116 checkNotClosed( "()" );
117 beforeFirst = false;
118 afterLast = true;
119 onSingleton = false;
120 }
121
122
123 public boolean first() throws Exception
124 {
125 checkNotClosed( "()" );
126 beforeFirst = false;
127 onSingleton = true;
128 afterLast = false;
129 return true;
130 }
131
132
133 public boolean last() throws Exception
134 {
135 checkNotClosed( "()" );
136 beforeFirst = false;
137 onSingleton = true;
138 afterLast = false;
139 return true;
140 }
141
142
143 public boolean isFirst() throws Exception
144 {
145 checkNotClosed( "()" );
146 return onSingleton;
147 }
148
149
150 public boolean isLast() throws Exception
151 {
152 checkNotClosed( "()" );
153 return onSingleton;
154 }
155
156
157 public boolean isAfterLast() throws Exception
158 {
159 checkNotClosed( "()" );
160 return afterLast;
161 }
162
163
164 public boolean isBeforeFirst() throws Exception
165 {
166 checkNotClosed( "()" );
167 return beforeFirst;
168 }
169
170
171 public boolean previous() throws Exception
172 {
173 checkNotClosed( "()" );
174 if ( beforeFirst )
175 {
176 return false;
177 }
178
179 if ( afterLast )
180 {
181 beforeFirst = false;
182 onSingleton = true;
183 afterLast = false;
184 return true;
185 }
186
187
188 beforeFirst = true;
189 onSingleton = false;
190 afterLast = false;
191 return false;
192 }
193
194
195 public boolean next() throws Exception
196 {
197 checkNotClosed( "()" );
198 if ( beforeFirst )
199 {
200 beforeFirst = false;
201 onSingleton = true;
202 afterLast = false;
203 return true;
204 }
205
206 if ( afterLast )
207 {
208 return false;
209 }
210
211
212 beforeFirst = false;
213 onSingleton = false;
214 afterLast = true;
215 return false;
216 }
217
218
219 public E get() throws Exception
220 {
221 checkNotClosed( "()" );
222 if ( onSingleton )
223 {
224 return singleton;
225 }
226
227 if ( beforeFirst )
228 {
229 throw new InvalidCursorPositionException( "Cannot access element if positioned before first." );
230 }
231 else
232 {
233 throw new InvalidCursorPositionException( "Cannot access element if positioned after last." );
234 }
235 }
236
237
238 public boolean isElementReused()
239 {
240 return true;
241 }
242 }