0
|
1 package nabble.model;
|
|
2
|
|
3 import java.util.Iterator;
|
|
4 import java.util.NoSuchElementException;
|
|
5 import java.util.List;
|
|
6 import java.util.ArrayList;
|
|
7 import java.util.Collection;
|
|
8
|
|
9
|
|
10 public abstract class NodeIterator<C extends Node> implements Iterator<C>, Iterable<C> {
|
|
11 public abstract boolean hasNext();
|
|
12 public abstract C next() throws NoSuchElementException;
|
|
13 public abstract void close();
|
|
14
|
|
15 public final void remove() {
|
|
16 throw new UnsupportedOperationException();
|
|
17 }
|
|
18
|
|
19 public final Iterator<C> iterator() {
|
|
20 return this;
|
|
21 }
|
|
22
|
|
23 public void skip(int n) {
|
|
24 while( hasNext() && n-- > 0 ) {
|
|
25 next();
|
|
26 }
|
|
27 }
|
|
28
|
|
29 // calls close()
|
|
30 public final boolean isEmpty() {
|
|
31 try {
|
|
32 return !hasNext();
|
|
33 } finally {
|
|
34 close();
|
|
35 }
|
|
36 }
|
|
37
|
|
38 public final boolean contains(Node node) {
|
|
39 try {
|
|
40 while( hasNext() ) {
|
|
41 if( next().equals(node) )
|
|
42 return true;
|
|
43 }
|
|
44 return false;
|
|
45 } finally {
|
|
46 close();
|
|
47 }
|
|
48 }
|
|
49
|
|
50 public final void addTo(Collection<Node> col) {
|
|
51 while( hasNext() ) {
|
|
52 col.add( next() );
|
|
53 }
|
|
54 close();
|
|
55 }
|
|
56
|
|
57 public List<Node> get(int i,int n) {
|
|
58 List<Node> list = new ArrayList<Node>();
|
|
59 skip(i);
|
|
60 while( hasNext() && n-- > 0 ) {
|
|
61 list.add( next() );
|
|
62 }
|
|
63 close();
|
|
64 return list;
|
|
65 }
|
|
66
|
|
67 public List<C> asList() {
|
|
68 List<C> list = new ArrayList<C>();
|
|
69 while( hasNext()) {
|
|
70 list.add( next() );
|
|
71 }
|
|
72 return list;
|
|
73 }
|
|
74
|
|
75 public static final <T extends Node> NodeIterator<T> empty() {
|
|
76 return new NodeIterator<T>() {
|
|
77
|
|
78 @Override public boolean hasNext() {
|
|
79 return false;
|
|
80 }
|
|
81
|
|
82 @Override public T next() throws NoSuchElementException {
|
|
83 throw new NoSuchElementException();
|
|
84 }
|
|
85
|
|
86 @Override public void close() {}
|
|
87
|
|
88 };
|
|
89 }
|
|
90
|
|
91 public static <T extends Node> NodeIterator<T> nodeIterator(final Iterator<T> iter) {
|
|
92 return new NodeIterator<T>() {
|
|
93
|
|
94 @Override public boolean hasNext() {
|
|
95 return iter.hasNext();
|
|
96 }
|
|
97
|
|
98 @Override public T next() throws NoSuchElementException {
|
|
99 return iter.next();
|
|
100 }
|
|
101
|
|
102 @Override public void close() {}
|
|
103
|
|
104 };
|
|
105 }
|
|
106
|
|
107 public static <T extends Node> NodeIterator<T> nodeIterator(final List<T> list) {
|
|
108 return new NodeIterator<T>() {
|
|
109 private Iterator<T> iter = null;
|
|
110
|
|
111 private Iterator<T> iter() {
|
|
112 if( iter == null )
|
|
113 iter = list.iterator();
|
|
114 return iter;
|
|
115 }
|
|
116
|
|
117 @Override public boolean hasNext() {
|
|
118 return iter().hasNext();
|
|
119 }
|
|
120
|
|
121 @Override public T next() throws NoSuchElementException {
|
|
122 return iter().next();
|
|
123 }
|
|
124
|
|
125 @Override public void close() {}
|
|
126
|
|
127 @Override public List<T> asList() {
|
|
128 return iter==null ? list : super.asList();
|
|
129 }
|
|
130
|
|
131 };
|
|
132 }
|
|
133
|
|
134 }
|