1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.util;
21
22 import java.util.Iterator;
23 import java.util.LinkedList;
24
25
26
27
28
29
30 public abstract class TreeIterator<T, U> implements Iterator<T> {
31 private final LinkedList<U> queue = new LinkedList<U>();
32
33 private final Order order;
34
35 private Iterator<T> currentIterator = null;
36
37 private U currentNode = null;
38
39 public enum Order {
40 BreadthFirst,
41 DepthFirst
42 };
43
44
45
46
47
48
49
50
51
52 public TreeIterator(U root, Order order) {
53 this.queue.add(root);
54 this.order = order;
55 }
56
57
58
59
60
61
62 public boolean hasNext() {
63 return getCurrentIterator().hasNext();
64 }
65
66
67
68
69 private Iterator<T> getCurrentIterator() {
70 if (this.currentIterator != null) {
71 if (this.currentIterator.hasNext()) {
72 return this.currentIterator;
73 }
74 this.currentIterator = null;
75 }
76 do {
77 do {
78 if (this.queue.isEmpty()) {
79 return new NoElementsIterator<T>();
80 }
81 this.currentNode = this.queue.removeFirst();
82 if (this.currentNode != null) {
83 this.currentIterator = getIterator(this.currentNode);
84 }
85 } while (this.currentIterator == null);
86
87 Iterator<U> iter = getChildren(this.currentNode);
88 while (iter.hasNext()) {
89 if (this.order == Order.BreadthFirst) {
90 this.queue.add(iter.next());
91 } else {
92 this.queue.push(iter.next());
93 }
94 }
95 } while (!this.currentIterator.hasNext());
96
97 return this.currentIterator;
98 }
99
100
101
102
103
104
105 public T next() {
106 return getCurrentIterator().next();
107 }
108
109
110
111
112
113
114 public void remove() {
115 getCurrentIterator().remove();
116 }
117
118 protected abstract Iterator<U> getChildren(U obj);
119
120 protected abstract Iterator<T> getIterator(U obj);
121 }
122
123