blob: a503071784d1e4d9c1043ccd6ee7276134f3e0a0 (
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
|
/*
* Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Eike Stepper - initial API and implementation
*/
package org.eclipse.net4j.util.collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* An abstract base class for custom iterators that only requires to implement a single {@link #computeNextElement()}
* method.
*
* @author Eike Stepper
* @since 3.2
*/
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 computed;
private T next;
public AbstractIterator()
{
}
public final boolean hasNext()
{
if (computed)
{
return true;
}
Object object = computeNextElement();
computed = true;
if (object == END_OF_DATA)
{
return false;
}
@SuppressWarnings("unchecked")
T cast = (T)object;
next = cast;
return true;
}
public final T next()
{
if (!hasNext())
{
throw new NoSuchElementException();
}
computed = false;
return next;
}
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();
}
|