1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.domain.dao;
17
18 import org.acegisecurity.domain.PersistableEntity;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 import java.util.Collection;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.ListIterator;
27 import java.util.NoSuchElementException;
28 import java.util.Vector;
29
30
31 /***
32 * <p>
33 * JDK1.5 compatible paginated <code>List</code>.
34 * </p>
35 *
36 * <p>
37 * Elements in the internal <code>List</code> (see {@link #getList()} represent
38 * only part of a larger resultset.
39 * </p>
40 *
41 * <p>
42 * Note that firstElement starts at zero. Any attempt to access other than the
43 * current page will cause an error.
44 * </p>
45 *
46 * <p>
47 * This is a read only implementation and many of the <code>List</code>
48 * methods are not implemented.
49 * </p>
50 *
51 * @author Carlos Sanchez
52 * @author Ben Alex
53 * @version $Id: PaginatedList.java,v 1.4 2005/11/17 00:55:47 benalex Exp $
54 */
55 public class PaginatedList<E extends PersistableEntity> implements List<E> {
56
57
58 protected final transient Log logger = LogFactory.getLog(getClass());
59 private List<E> list;
60 private int firstElement;
61 private int maxElements;
62 private int size;
63
64
65
66
67 public PaginatedList() {}
68
69 /***
70 * Used to construct a <code>PaginatedList</code> which contains only the
71 * given entity.
72 *
73 * @param entity the entity to include (can be <code>null</code>, which
74 * indicates an empty <code>PaginatedList</code> should be created)
75 */
76 public PaginatedList(E entity) {
77 if (entity == null) {
78 this.list = new Vector<E>();
79 this.firstElement = 0;
80 this.maxElements = Integer.MAX_VALUE;
81 this.size = 0;
82 } else {
83 List<E> myList = new Vector<E>();
84 myList.add(entity);
85 this.list = myList;
86 this.firstElement = 0;
87 this.maxElements = Integer.MAX_VALUE;
88 this.size = 1;
89 }
90 }
91
92 public PaginatedList(List<E> list, int firstElement, int maxElements, int size) {
93 this.list = list;
94 this.firstElement = firstElement;
95 this.maxElements = maxElements;
96 this.size = size;
97 }
98
99
100
101 /***
102 * Unsupported operation
103 *
104 * @return DOCUMENT ME!
105 *
106 * @throws UnsupportedOperationException
107 *
108 * @see java.util.Collection#isEmpty()
109 */
110 public boolean isEmpty() {
111 throw new UnsupportedOperationException();
112 }
113
114 public void setFirstElement(int firstElement) {
115 this.firstElement = firstElement;
116 }
117
118 /***
119 * First element of this page, starting at zero.
120 *
121 * @return
122 */
123 public int getFirstElement() {
124 return firstElement;
125 }
126
127 /***
128 * Calculate the last page number, starting at 0
129 *
130 * @return
131 */
132 public int getLastPageNumber() {
133 return (size() - 1) / getMaxElements();
134 }
135
136 public void setList(List<E> list) {
137 this.list = list;
138 }
139
140 /***
141 * Get list with the elements of this page.
142 *
143 * @return this page of the results
144 */
145 public List<E> getList() {
146 return list;
147 }
148
149 public void setMaxElements(int maxElements) {
150 this.maxElements = maxElements;
151 }
152
153 /***
154 * Max number of elements in the page
155 *
156 * @return
157 */
158 public int getMaxElements() {
159 return maxElements;
160 }
161
162 /***
163 * Calculate the page number, starting at 0
164 *
165 * @return
166 */
167 public int getPageNumber() {
168 return getFirstElement() / getMaxElements();
169 }
170
171 /***
172 * Number of elements in this page
173 *
174 * @return
175 */
176 public int getPageSize() {
177 return list.size();
178 }
179
180 /***
181 * Set the number of elements in all the pages
182 *
183 * @param size DOCUMENT ME!
184 */
185 public void setSize(int size) {
186 this.size = size;
187 }
188
189 /***
190 * Unsupported operation
191 *
192 * @param arg0 DOCUMENT ME!
193 * @param arg1 DOCUMENT ME!
194 *
195 * @throws UnsupportedOperationException
196 *
197 * @see java.util.List#add(int, java.lang.Object)
198 */
199 public void add(int arg0, E arg1) {
200 throw new UnsupportedOperationException();
201 }
202
203 /***
204 * Unsupported operation
205 *
206 * @param arg0 DOCUMENT ME!
207 *
208 * @return DOCUMENT ME!
209 *
210 * @throws UnsupportedOperationException
211 *
212 * @see java.util.Collection#add(java.lang.Object)
213 */
214 public boolean add(E arg0) {
215 throw new UnsupportedOperationException();
216 }
217
218 /***
219 * Unsupported operation
220 *
221 * @param arg0 DOCUMENT ME!
222 *
223 * @return DOCUMENT ME!
224 *
225 * @throws UnsupportedOperationException
226 *
227 * @see java.util.Collection#addAll(java.util.Collection)
228 */
229 public boolean addAll(Collection<? extends E> arg0) {
230 throw new UnsupportedOperationException();
231 }
232
233 /***
234 * Unsupported operation
235 *
236 * @param arg0 DOCUMENT ME!
237 * @param arg1 DOCUMENT ME!
238 *
239 * @return DOCUMENT ME!
240 *
241 * @throws UnsupportedOperationException
242 *
243 * @see java.util.List#addAll(int, java.util.Collection)
244 */
245 public boolean addAll(int arg0, Collection<? extends E> arg1) {
246 throw new UnsupportedOperationException();
247 }
248
249 /***
250 * Unsupported operation
251 *
252 * @throws UnsupportedOperationException
253 *
254 * @see java.util.Collection#clear()
255 */
256 public void clear() {
257 throw new UnsupportedOperationException();
258 }
259
260 /***
261 * Unsupported operation
262 *
263 * @param arg0 DOCUMENT ME!
264 *
265 * @return DOCUMENT ME!
266 *
267 * @throws UnsupportedOperationException
268 *
269 * @see java.util.Collection#contains(java.lang.Object)
270 */
271 public boolean contains(Object arg0) {
272 throw new UnsupportedOperationException();
273 }
274
275 /***
276 * Unsupported operation
277 *
278 * @param arg0 DOCUMENT ME!
279 *
280 * @return DOCUMENT ME!
281 *
282 * @throws UnsupportedOperationException
283 *
284 * @see java.util.Collection#containsAll(java.util.Collection)
285 */
286 public boolean containsAll(Collection<?> arg0) {
287 throw new UnsupportedOperationException();
288 }
289
290 /***
291 * Unsupported operation
292 *
293 * @param arg0 DOCUMENT ME!
294 *
295 * @return DOCUMENT ME!
296 *
297 * @see java.util.List#get(int)
298 */
299 public E get(int arg0) {
300 return list.get(arg0);
301 }
302
303 /***
304 * Unsupported operation
305 *
306 * @param arg0 DOCUMENT ME!
307 *
308 * @return DOCUMENT ME!
309 *
310 * @throws UnsupportedOperationException
311 *
312 * @see java.util.List#indexOf(java.lang.Object)
313 */
314 public int indexOf(Object arg0) {
315 throw new UnsupportedOperationException();
316 }
317
318 public Iterator<E> iterator() {
319 return new PaginatedListIterator();
320 }
321
322 /***
323 * Unsupported operation
324 *
325 * @param arg0 DOCUMENT ME!
326 *
327 * @return DOCUMENT ME!
328 *
329 * @throws UnsupportedOperationException
330 *
331 * @see java.util.List#lastIndexOf(java.lang.Object)
332 */
333 public int lastIndexOf(Object arg0) {
334 throw new UnsupportedOperationException();
335 }
336
337 /***
338 * Unsupported operation
339 *
340 * @return DOCUMENT ME!
341 *
342 * @throws UnsupportedOperationException
343 *
344 * @see java.util.List#listIterator()
345 */
346 public ListIterator<E> listIterator() {
347 throw new UnsupportedOperationException();
348 }
349
350 /***
351 * Unsupported operation
352 *
353 * @param arg0 DOCUMENT ME!
354 *
355 * @return DOCUMENT ME!
356 *
357 * @throws UnsupportedOperationException
358 *
359 * @see java.util.List#listIterator(int)
360 */
361 public ListIterator<E> listIterator(int arg0) {
362 throw new UnsupportedOperationException();
363 }
364
365 /***
366 * Unsupported operation
367 *
368 * @param arg0 DOCUMENT ME!
369 *
370 * @return DOCUMENT ME!
371 *
372 * @throws UnsupportedOperationException
373 *
374 * @see java.util.List#remove(int)
375 */
376 public E remove(int arg0) {
377 throw new UnsupportedOperationException();
378 }
379
380 /***
381 * Unsupported operation
382 *
383 * @param arg0 DOCUMENT ME!
384 *
385 * @return DOCUMENT ME!
386 *
387 * @throws UnsupportedOperationException
388 *
389 * @see java.util.Collection#remove(java.lang.Object)
390 */
391 public boolean remove(Object arg0) {
392 throw new UnsupportedOperationException();
393 }
394
395 /***
396 * Unsupported operation
397 *
398 * @param arg0 DOCUMENT ME!
399 *
400 * @return DOCUMENT ME!
401 *
402 * @throws UnsupportedOperationException
403 *
404 * @see java.util.Collection#removeAll(java.util.Collection)
405 */
406 public boolean removeAll(Collection arg0) {
407 throw new UnsupportedOperationException();
408 }
409
410 /***
411 * Unsupported operation
412 *
413 * @param arg0 DOCUMENT ME!
414 *
415 * @return DOCUMENT ME!
416 *
417 * @throws UnsupportedOperationException
418 *
419 * @see java.util.Collection#retainAll(java.util.Collection)
420 */
421 public boolean retainAll(Collection<?> arg0) {
422 throw new UnsupportedOperationException();
423 }
424
425 /***
426 * Unsupported operation
427 *
428 * @param arg0 DOCUMENT ME!
429 * @param arg1 DOCUMENT ME!
430 *
431 * @return DOCUMENT ME!
432 *
433 * @throws UnsupportedOperationException
434 *
435 * @see java.util.List#set(int, java.lang.Object)
436 */
437 public E set(int arg0, E arg1) {
438 throw new UnsupportedOperationException();
439 }
440
441 /***
442 * Number of elements in all the pages
443 *
444 * @see java.util.Collection#size()
445 */
446 public int size() {
447 return size;
448 }
449
450 /***
451 * Unsupported operation
452 *
453 * @param arg0 DOCUMENT ME!
454 * @param arg1 DOCUMENT ME!
455 *
456 * @return DOCUMENT ME!
457 *
458 * @throws UnsupportedOperationException
459 *
460 * @see java.util.List#subList(int, int)
461 */
462 public List<E> subList(int arg0, int arg1) {
463 throw new UnsupportedOperationException();
464 }
465
466 public Object[] toArray() {
467 return list.toArray();
468 }
469
470 public <T> T[] toArray(T[] arg0) {
471 if (logger.isDebugEnabled()) {
472 logger.debug("List size when convert to array "
473 + list.toArray().length);
474 }
475
476 return list.toArray(arg0);
477 }
478
479
480 private class PaginatedListIterator implements Iterator<E> {
481
482
483 private Iterator<E> iterator;
484 private int i = 0;
485
486 /***
487 * @see java.util.Iterator#hasNext()
488 */
489 public boolean hasNext() {
490 return i < size();
491 }
492
493 /***
494 * This method follows the rules of Iterator.next() except that it returns
495 * null when requesting an element that it's not in the current page.
496 *
497 * @see java.util.Iterator#next()
498 */
499 public E next() {
500 if (i == getFirstElement()) {
501 iterator = getList().iterator();
502 }
503
504 if ((i >= getFirstElement())
505 && (i < (getFirstElement() + getMaxElements()))) {
506 i++;
507
508 return iterator.next();
509 }
510
511 if (hasNext()) {
512 i++;
513
514 return null;
515 } else {
516 throw new NoSuchElementException();
517 }
518 }
519
520 /***
521 * Unsupported operation
522 *
523 * @throws UnsupportedOperationException
524 *
525 * @see java.util.Iterator#remove()
526 */
527 public void remove() {
528 throw new UnsupportedOperationException();
529 }
530 }
531
532
533
534
535 }