com.aol.cyclops.streams.operators.RecoverOperator 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.Iterator;
import java.util.function.Function;
import java.util.stream.Stream;
import lombok.Value;
import com.aol.cyclops.invokedynamic.ExceptionSoftener;
import com.aol.cyclops.streams.StreamUtils;
@Value
public class RecoverOperator {
Stream stream;
Class type;
private static final Object UNSET = new Object();
public Stream recover(Function fn){
Iterator it = stream.iterator();
return StreamUtils.stream(new Iterator(){
T result = (T)UNSET;
@Override
public boolean hasNext() {
try{
return it.hasNext();
}catch(Throwable t){
if(type.isAssignableFrom(t.getClass())){
result= fn.apply(t);
return true;
}
ExceptionSoftener.throwSoftenedException(t);
return false;
}
}
@Override
public T next() {
if(result!=UNSET){
T toReturn = result;
result = (T)UNSET;
return toReturn;
}
return it.next();
}
});
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy