blob: 1f33f7e5d17db4237dd25a5e7ca8a8c511e20b41 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
/*
* Copyright (c) 2014 Eike Stepper (Loehne, Germany) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Eike Stepper - initial API and implementation
*/
package org.eclipse.oomph.util;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.NoSuchElementException;
/**
* An abstract base class for custom iterators that only requires to implement a single {@link #computeNextElement()} method.
*
* @author Eike Stepper
*/
public abstract class AbstractIterator<T> implements Iterator<T>
{
/**
* The token to be used in {@link #computeNextElement()} to indicate the end of the iteration.
*/
protected static final Object END_OF_DATA = new Object();
private boolean nextComputed;
private T next;
public AbstractIterator()
{
}
@Override
public final boolean hasNext()
{
if (nextComputed)
{
return true;
}
Object object = computeNextElement();
nextComputed = true;
if (object == END_OF_DATA)
{
return false;
}
@SuppressWarnings("unchecked")
T cast = (T)object;
next = cast;
return true;
}
@Override
public final T next()
{
if (!hasNext())
{
throw new NoSuchElementException();
}
nextComputed = false;
return next;
}
@Override
public void remove()
{
throw new UnsupportedOperationException();
}
/**
* Returns the next iteration element, or {@link #END_OF_DATA} if the end of the iteration has been reached.
*/
protected abstract Object computeNextElement();
@SuppressWarnings("unchecked")
public static <T> ListIterator<T> empty()
{
return (ListIterator<T>)EmptyIterator.INSTANCE;
}
/**
* @author Eike Stepper
*/
private static final class EmptyIterator implements ListIterator<Object>
{
private static final ListIterator<Object> INSTANCE = new EmptyIterator();
@Override
public boolean hasNext()
{
return false;
}
@Override
public Object next()
{
throw new NoSuchElementException();
}
@Override
public boolean hasPrevious()
{
return false;
}
@Override
public Object previous()
{
throw new NoSuchElementException();
}
@Override
public int nextIndex()
{
throw new NoSuchElementException();
}
@Override
public int previousIndex()
{
throw new NoSuchElementException();
}
@Override
public void remove()
{
throw new UnsupportedOperationException();
}
@Override
public void set(Object e)
{
throw new UnsupportedOperationException();
}
@Override
public void add(Object e)
{
throw new UnsupportedOperationException();
}
}
}
|