All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aol.cyclops.streams.operators.LimitLastOperator Maven / Gradle / Ivy

There is a newer version: 7.3.1
Show newest version
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