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

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

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