1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.oness.common.model.util;
17
18 import java.util.Iterator;
19 import java.util.NoSuchElementException;
20
21 import net.sf.oness.common.all.BaseObject;
22
23 /***
24 * @author Carlos Sanchez
25 * @version $Revision: 1.2 $
26 */
27 public class PaginatedListIterator extends BaseObject implements Iterator {
28
29 private PaginatedList list;
30 private int i = 0;
31 private Iterator iterator;
32
33 /***
34 * @param list
35 */
36 public PaginatedListIterator(PaginatedList list) {
37 this.list = list;
38 }
39
40 /***
41 * @see java.util.Iterator#hasNext()
42 */
43 public boolean hasNext() {
44 return i < list.size();
45 }
46
47 /***
48 * This method follows the rules of Iterator.next() except
49 * that it returns null when requesting an element that it's
50 * not in the current page.
51 * @see java.util.Iterator#next()
52 */
53 public Object next() {
54 if (i == list.getFirstElement()) {
55 iterator = list.getList().iterator();
56 }
57
58 if ((i >= list.getFirstElement())
59 && (i < list.getFirstElement() + list.getMaxElements())) {
60 i++;
61 return iterator.next();
62 }
63
64 if (hasNext()) {
65 i++;
66 return null;
67 } else {
68 throw new NoSuchElementException();
69 }
70 }
71
72 /***
73 * Unsupported operation
74 * @see java.util.Iterator#remove()
75 * @throws UnsupportedOperationException
76 */
77 public void remove() {
78 throw new UnsupportedOperationException();
79 }
80
81 }