com.aol.cyclops.streams.operators.LimitLastOperator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cyclops-streams Show documentation
Show all versions of cyclops-streams Show documentation
Sequential Streams and Stream Utilities for Java 8
package com.aol.cyclops.streams.operators;
import java.util.ArrayDeque;
import java.util.Iterator;
import java.util.stream.Stream;
import com.aol.cyclops.streams.StreamUtils;
public class LimitLastOperator {
Stream stream;
ArrayDeque buffer;
int limit;
public LimitLastOperator(Stream stream,int limit){
buffer = new ArrayDeque<>(limit);
this.stream = stream;
this.limit = limit;
}
public Stream limitLast(){
Iterator it = stream.iterator();
return StreamUtils.stream(new Iterator(){
@Override
public boolean hasNext() {
while(it.hasNext()){
buffer.add(it.next());
if(buffer.size()>limit)
buffer.pop();
}
return buffer.size()>0;
}
@Override
public T next() {
return buffer.pop();
}
});
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy