Skip to content

Commit

Permalink
Fix streams concatenation
Browse files Browse the repository at this point in the history
  • Loading branch information
aNNiMON committed Feb 3, 2016
1 parent 78b096f commit 80885bb
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/main/java/com/annimon/stream/Stream.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,18 +273,38 @@ public static <T> Stream<T> concat(Stream<? extends T> stream1, Stream<? extends
final Iterator<? extends T> it1 = stream1.iterator;
final Iterator<? extends T> it2 = stream2.iterator;
return new Stream<T>(new LsaIterator<T>() {


private T next;
private boolean hasNext, isInit;

@Override
public boolean hasNext() {
return it1.hasNext() || it2.hasNext();
if (!isInit) {
nextIteration();
isInit = true;
}
return hasNext;
}

@Override
public T next() {
final T result = next;
nextIteration();
return result;
}

private void nextIteration() {
if (it1.hasNext()) {
return it1.next();
next = it1.next();
hasNext = true;
return;
}
if (it2.hasNext()) {
next = it2.next();
hasNext = true;
return;
}
return it2.next();
hasNext = false;
}
});
}
Expand Down

0 comments on commit 80885bb

Please sign in to comment.