Skip to content

Instantly share code, notes, and snippets.

@RaffaeleSgarro
Created November 23, 2014 09:02
Show Gist options
  • Select an option

  • Save RaffaeleSgarro/c5bb0855215bbe48db5c to your computer and use it in GitHub Desktop.

Select an option

Save RaffaeleSgarro/c5bb0855215bbe48db5c to your computer and use it in GitHub Desktop.
Java LazyIterator
package stackoverflow;
public interface Acceptable {
boolean accept();
}
package stackoverflow;
import java.util.Iterator;
public class LazyIterable<E extends Acceptable> implements Iterable<E> {
private final Iterable<E> wrapped;
public LazyIterable(Iterable<E> wrapped) {
this.wrapped = wrapped;
}
@Override
public Iterator<E> iterator() {
return new LazyIterator<E>(wrapped.iterator());
}
}
package stackoverflow;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class LazyIterator<E extends Acceptable> implements Iterator<E> {
private final Iterator<E> iterator;
private boolean hasNext;
private E next;
public LazyIterator(Iterator<E> iterator) {
this.iterator = iterator;
iterate();
}
private void iterate() {
hasNext = false;
while (iterator.hasNext()) {
next = iterator.next();
if (next.accept()) {
hasNext = true;
break;
}
}
}
@Override
public boolean hasNext() {
return hasNext;
}
@Override
public E next() {
if (!hasNext) throw new NoSuchElementException();
E out = next;
iterate();
return out;
}
@Override
public void remove() {
throw new RuntimeException("Not supported");
}
}
package stackoverflow;
import org.testng.annotations.Test;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.NoSuchElementException;
import static org.testng.Assert.*;
public class LazyIteratorTest {
private LazyIterator<?> target;
@Test
public void emptyList() {
setup(Collections.<Acceptable>emptyList());
assertFalse(target.hasNext());
assertThrowsNoSuchElementException();
}
@Test
public void firstElementAcceptable() {
SimpleAcceptable e1 = el(true);
SimpleAcceptable e2 = el(true);
setup(Arrays.asList(e1, e2, el(false)));
assertTrue(target.hasNext());
assertEquals(target.next(), e1);
assertTrue(target.hasNext());
assertEquals(target.next(), e2);
assertFalse(target.hasNext());
assertThrowsNoSuchElementException();
}
@Test
public void firstElementNotAcceptable() {
SimpleAcceptable e1 = el(false);
SimpleAcceptable e2 = el(true);
setup(Arrays.asList(e1, e2));
assertTrue(target.hasNext());
assertEquals(target.next(), e2);
assertFalse(target.hasNext());
assertThrowsNoSuchElementException();
}
@Test
public void noAcceptableElements() {
setup(Arrays.asList(el(false), el(false), el(false)));
assertFalse(target.hasNext());
assertThrowsNoSuchElementException();
}
private void assertThrowsNoSuchElementException() {
boolean exceptionThrown = false;
try {
target.next();
} catch (NoSuchElementException e) {
exceptionThrown = true;
}
assertTrue(exceptionThrown);
}
private <E extends Acceptable> void setup(Collection<E> collection) {
target = new LazyIterator<E>(collection.iterator());
}
private SimpleAcceptable el(boolean acceptable) {
return new SimpleAcceptable(acceptable);
}
private static class SimpleAcceptable implements Acceptable {
private final boolean accept;
private SimpleAcceptable(boolean accept) {
this.accept = accept;
}
@Override
public boolean accept() {
return accept;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment