com.aol.cyclops.streams.operators.WindowStatefullyWhileOperator 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.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