-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
90acee8
commit 09943f7
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
kool/src/main/java/org/davidmoten/kool/internal/operators/stream/SkipLast.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.davidmoten.kool.internal.operators.stream; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
import org.davidmoten.kool.Stream; | ||
import org.davidmoten.kool.StreamIterator; | ||
import org.davidmoten.kool.internal.util.RingBuffer; | ||
|
||
public final class SkipLast<T> implements Stream<T> { | ||
|
||
private final Stream<T> stream; | ||
private final int size; | ||
|
||
public SkipLast(int size, Stream<T> stream) { | ||
this.stream = stream; | ||
this.size = size; | ||
} | ||
|
||
@Override | ||
public StreamIterator<T> iterator() { | ||
RingBuffer<T> buffer = new RingBuffer<T>(size + 1); | ||
StreamIterator<T> it = stream.iterator(); | ||
return new StreamIterator<T>() { | ||
|
||
@Override | ||
public boolean hasNext() { | ||
loadNext(); | ||
return buffer.size() == size + 1; | ||
} | ||
|
||
@Override | ||
public T next() { | ||
loadNext(); | ||
if (buffer.size() == size + 1) { | ||
return buffer.poll(); | ||
} else { | ||
throw new NoSuchElementException(); | ||
} | ||
} | ||
|
||
@Override | ||
public void dispose() { | ||
it.dispose(); | ||
} | ||
|
||
private void loadNext() { | ||
while (buffer.size() < size + 1 && it.hasNext()) { | ||
buffer.add(it.next()); | ||
} | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters