| 1 | |
package org.webslinger.commons.vfs.util; |
| 2 | |
|
| 3 | |
import java.util.Iterator; |
| 4 | |
|
| 5 | |
public class IteratorWrapper<T> implements Iterator<T> { |
| 6 | |
protected Iterator<T> it; |
| 7 | |
protected IteratorRemover remover; |
| 8 | |
protected T lastObject; |
| 9 | |
protected int serialNumber; |
| 10 | |
protected IteratorOwner owner; |
| 11 | |
|
| 12 | 28 | public IteratorWrapper(Iterator<T> it, IteratorRemover remover, IteratorOwner owner) { |
| 13 | 28 | this.it = it; |
| 14 | 28 | this.remover = remover; |
| 15 | 28 | this.owner = owner; |
| 16 | 28 | serialNumber = owner.getSerialNumber(); |
| 17 | 28 | } |
| 18 | |
|
| 19 | |
protected void checkRemoved() { |
| 20 | 126 | if (owner.getSerialNumber() != serialNumber) throw new IllegalStateException("removed"); |
| 21 | 126 | } |
| 22 | |
|
| 23 | |
public void remove() { |
| 24 | 0 | checkRemoved(); |
| 25 | 0 | it.remove(); |
| 26 | 0 | remover.remove(lastObject); |
| 27 | 0 | } |
| 28 | |
|
| 29 | |
public T next() { |
| 30 | 49 | checkRemoved(); |
| 31 | 49 | return lastObject = it.next(); |
| 32 | |
} |
| 33 | |
|
| 34 | |
public boolean hasNext() { |
| 35 | 77 | checkRemoved(); |
| 36 | 77 | return it.hasNext(); |
| 37 | |
} |
| 38 | |
} |
| 39 | |
|