01: package org.drools.repository;
02:
03: import java.util.Iterator;
04: import java.util.NoSuchElementException;
05:
06: import org.apache.log4j.Logger;
07:
08: /**
09: * Implements the Iterator interface, allowing iteration over the version history of versionableItem
10: * nodes
11: *
12: * @author btruitt
13: */
14: class ItemVersionIterator implements Iterator {
15: private static final Logger log = Logger
16: .getLogger(ItemVersionIterator.class);
17:
18: private VersionableItem currentVersionableItem;
19: private int iterationType;
20:
21: public static final int ITERATION_TYPE_SUCCESSOR = 1;
22: public static final int ITERATION_TYPE_PREDECESSOR = 2;
23:
24: public ItemVersionIterator(VersionableItem versionableItem,
25: int iterationType) {
26: this .currentVersionableItem = versionableItem;
27: this .iterationType = iterationType;
28: }
29:
30: public boolean hasNext() {
31: if (this .currentVersionableItem == null) {
32: return false;
33: }
34:
35: if (this .iterationType == ITERATION_TYPE_SUCCESSOR) {
36: return (this .currentVersionableItem.getSucceedingVersion() != null);
37: } else if (this .iterationType == ITERATION_TYPE_PREDECESSOR) {
38: return (this .currentVersionableItem.getPrecedingVersion() != null);
39: } else {
40: //shouldn't reach this block
41: log
42: .error("Reached unexpected path of execution because iterationType is set to: "
43: + this .iterationType);
44: return false;
45: }
46: }
47:
48: public Object next() {
49: if (this .iterationType == ITERATION_TYPE_SUCCESSOR) {
50: this .currentVersionableItem = this .currentVersionableItem
51: .getSucceedingVersion();
52: } else if (this .iterationType == ITERATION_TYPE_PREDECESSOR) {
53: this .currentVersionableItem = this .currentVersionableItem
54: .getPrecedingVersion();
55: } else {
56: //shouldn't reach this block
57: log
58: .error("Reached unexpected path of execution because iterationType is set to: "
59: + this .iterationType);
60: return null;
61: }
62:
63: if (this .currentVersionableItem == null) {
64: throw new NoSuchElementException();
65: }
66: return this .currentVersionableItem;
67: }
68:
69: public void remove() {
70: throw new UnsupportedOperationException();
71: }
72: }
|