Created
March 4, 2015 12:05
-
-
Save fge/0321d848b037595a0731 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.github.fge.lambdas.function; | |
| import com.github.fge.lambdas.ThrowablesFactory; | |
| import com.github.fge.lambdas.Chain; | |
| import java.util.function.Function; | |
| public final class FunctionChain<T, R> | |
| extends Chain<Function<T, R>, ThrowingFunction<T, R>, FunctionChain<T, R>> | |
| implements ThrowingFunction<T, R> | |
| { | |
| public FunctionChain(final ThrowingFunction<T, R> function) | |
| { | |
| super(function); | |
| } | |
| @Override | |
| public R doApply(final T t) | |
| throws Throwable | |
| { | |
| return throwing.doApply(t); | |
| } | |
| @Override | |
| public FunctionChain<T, R> orTryWith( | |
| final ThrowingFunction<T, R> other) | |
| { | |
| final ThrowingFunction<T, R> function = t -> { | |
| try { | |
| return throwing.doApply(t); | |
| } catch (Error | RuntimeException e) { | |
| throw e; | |
| } catch (Throwable ignored) { | |
| return other.doApply(t); | |
| } | |
| }; | |
| return new FunctionChain<>(function); | |
| } | |
| @Override | |
| public <E extends RuntimeException> ThrowingFunction<T, R> orThrow( | |
| final Class<E> exclass) | |
| { | |
| return t -> { | |
| try { | |
| return throwing.doApply(t); | |
| } catch (Error | RuntimeException e) { | |
| throw e; | |
| } catch (Throwable throwable) { | |
| throw ThrowablesFactory.INSTANCE.get(exclass, throwable); | |
| } | |
| }; | |
| } | |
| @Override | |
| public Function<T, R> fallbackTo(final Function<T, R> fallback) | |
| { | |
| return t -> { | |
| try { | |
| return doApply(t); | |
| } catch (Error | RuntimeException e) { | |
| throw e; | |
| } catch (Throwable ignored) { | |
| return fallback.apply(t); | |
| } | |
| }; | |
| } | |
| public Function<T, R> orReturn(final R value) | |
| { | |
| return t -> { | |
| try { | |
| return doApply(t); | |
| } catch (Error | RuntimeException e) { | |
| throw e; | |
| } catch (Throwable ignored) { | |
| return value; | |
| } | |
| }; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment