Skip to content

Commit

Permalink
Fix #53, filter operator issue
Browse files Browse the repository at this point in the history
  • Loading branch information
aNNiMON committed Jul 4, 2016
1 parent fc0ea56 commit 528d932
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
28 changes: 26 additions & 2 deletions stream/src/main/java/com/annimon/stream/Stream.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Queue;
import java.util.Set;

Expand Down Expand Up @@ -472,10 +473,33 @@ public <R> R custom(Function<Stream<T>, R> function) {
* @return the new stream
*/
public Stream<T> filter(final Predicate<? super T> predicate) {
return new Stream<T>(new LsaExtIterator<T>() {
return new Stream<T>(new Iterator<T>() {

private boolean hasNext, hasNextEvaluated;
private T next;

@Override
protected void nextIteration() {
public boolean hasNext() {
if (!hasNextEvaluated) {
nextIteration();
hasNextEvaluated = true;
}
return hasNext;
}

@Override
public T next() {
if (!hasNextEvaluated) {
hasNext = hasNext();
}
if (!hasNext) {
throw new NoSuchElementException();
}
hasNextEvaluated = false;
return next;
}

private void nextIteration() {
while (iterator.hasNext()) {
next = iterator.next();
if (predicate.test(next)) {
Expand Down
20 changes: 20 additions & 0 deletions stream/src/test/java/com/annimon/stream/StreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,26 @@ public BigInteger apply(BigInteger value1, BigInteger value2) {
assertEquals(new BigInteger("1267650600228229401496703205375"), sum);
}

@Test(timeout=2000)
public void testIterateIssue53() {
Optional<Integer> res = Stream
.iterate(0, new UnaryOperator<Integer>() {
@Override
public Integer apply(Integer value) {
return value + 1;
}
})
.filter(new Predicate<Integer>() {
@Override
public boolean test(Integer value) {
return value == 0;
}
})
.findFirst();
assertThat(res, isPresent());
assertThat(res.get(), is(0));
}

@Test
public void testConcat() {
final PrintConsumer<String> consumer = new PrintConsumer<String>();
Expand Down

0 comments on commit 528d932

Please sign in to comment.