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

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

There is a newer version: 7.3.1
Show newest version
package com.aol.cyclops.streams.operators;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.stream.Stream;

import lombok.Value;

import com.aol.cyclops.sequence.streamable.Streamable;
import com.aol.cyclops.streams.StreamUtils;

@Value
public class WindowStatefullyWhileOperator {
	 private static final Object UNSET = new Object();
	Stream stream;
	public Stream> windowStatefullyWhile(BiPredicate,T> predicate){
		Iterator it = stream.iterator();
		return StreamUtils.stream(new Iterator>(){
			Streamable last= Streamable.empty();
			T value = (T)UNSET;
			@Override
			public boolean hasNext() {
				return value!=UNSET || it.hasNext();
			}
			@Override
			public Streamable next() {
				
				List list = new ArrayList<>();
				if(value!=UNSET)
					list.add(value);
				T value;
				while(list.size()==0 && it.hasNext()){
label:					while(it.hasNext()) {
							value=it.next();
							list.add(value);
							
							if(!predicate.test(last,value)){
								value=(T)UNSET;
								break label;
							}
							value=(T)UNSET;
						
					}
					
						
				}
				
				return last = Streamable.fromIterable(list);
			}
			
		});
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy