]> git.argeo.org Git - lgpl/argeo-commons.git/blob - jcr/CollectionNodeIterator.java
Prepare next development cycle
[lgpl/argeo-commons.git] / jcr / CollectionNodeIterator.java
1 package org.argeo.jcr;
2
3 import java.util.Collection;
4 import java.util.Iterator;
5 import java.util.NoSuchElementException;
6
7 import javax.jcr.Node;
8 import javax.jcr.NodeIterator;
9
10 /** Wraps a collection of nodes in order to read it as a {@link NodeIterator} */
11 public class CollectionNodeIterator implements NodeIterator {
12 private final Long collectionSize;
13 private final Iterator<Node> iterator;
14 private Integer position = 0;
15
16 public CollectionNodeIterator(Collection<Node> nodes) {
17 super();
18 this.collectionSize = (long) nodes.size();
19 this.iterator = nodes.iterator();
20 }
21
22 public void skip(long skipNum) {
23 if (skipNum < 0)
24 throw new IllegalArgumentException(
25 "Skip count has to be positive: " + skipNum);
26
27 for (long i = 0; i < skipNum; i++) {
28 if (!hasNext())
29 throw new NoSuchElementException("Last element past (position="
30 + getPosition() + ")");
31 nextNode();
32 }
33 }
34
35 public long getSize() {
36 return collectionSize;
37 }
38
39 public long getPosition() {
40 return position;
41 }
42
43 public boolean hasNext() {
44 return iterator.hasNext();
45 }
46
47 public Object next() {
48 return nextNode();
49 }
50
51 public void remove() {
52 iterator.remove();
53 }
54
55 public Node nextNode() {
56 Node node = iterator.next();
57 position++;
58 return node;
59 }
60
61 }