1 package net
.kezvh
.collections
;
3 import java
.util
.Iterator
;
4 import java
.util
.NoSuchElementException
;
8 * @param <T> type to iterate over
10 public abstract class AbstractIterator
<T
> implements Iterator
<T
> {
12 private boolean firstTime
= true;
13 private boolean noMoreElements
= false;
16 * @return the next object
17 * @throws NoSuchElementException if there are no more elements
19 protected abstract T
findNext() throws NoSuchElementException
;
21 private T
tryFindNext() {
22 this.firstTime
= false;
24 return this.findNext();
25 } catch (final NoSuchElementException e
) {
26 this.noMoreElements
= true;
32 * @see java.util.Iterator#hasNext()
35 public final boolean hasNext() {
37 this.nextObject
= this.tryFindNext();
38 return !this.noMoreElements
;
42 * @see java.util.Iterator#next()
45 public final T
next() {
47 this.nextObject
= this.tryFindNext();
49 throw new NoSuchElementException();
51 return this.nextObject
;
53 this.nextObject
= this.tryFindNext();
58 * By default, throws UnsupportedOperationException. This can be overridden.
60 * @see java.util.Iterator#remove()
62 public void remove() {
63 throw new UnsupportedOperationException();