01: package org.drools.repository;
02:
03: import java.util.Iterator;
04:
05: import javax.jcr.Node;
06: import javax.jcr.RepositoryException;
07: import javax.jcr.version.Version;
08: import javax.jcr.version.VersionIterator;
09:
10: /**
11: * A lazy iterator for walking back through history.
12: * Wraps the version iterator from JCR and allows skipping.
13: *
14: * @author Michael Neale
15: *
16: */
17: public class AssetHistoryIterator implements Iterator {
18:
19: private Node head;
20: private VersionIterator versionIterator;
21: private RulesRepository repo;
22:
23: public AssetHistoryIterator(RulesRepository repo, Node head) {
24: this .head = head;
25: this .repo = repo;
26: try {
27: this .versionIterator = this .head.getVersionHistory()
28: .getAllVersions();
29: } catch (RepositoryException e) {
30: throw new RulesRepositoryException(e);
31: }
32: }
33:
34: public boolean hasNext() {
35: return versionIterator.hasNext();
36: }
37:
38: public Object next() {
39: return new AssetItem(this .repo, (Version) versionIterator
40: .next());
41:
42: }
43:
44: /**
45: * You can't do this with this sort of iterator.
46: * It makes no sense to remove a history item.
47: * Removing history is a administrative function only (and in
48: * any case, it may have to be archived for legal reasons).
49: *
50: * @throws UnsupportedOperationException when called.
51: */
52: public void remove() {
53: throw new UnsupportedOperationException();
54: }
55:
56: /**
57: * Skip the specified number of items. As this is a lazy iterator this
58: * means less work in pulling it from the database etc.
59: */
60: public void skip(int i) {
61: this.versionIterator.skip(i);
62: }
63:
64: }
|