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

com.github.andyshao.util.function.ExceptionableFunction Maven / Gradle / Ivy

The newest version!
package com.github.andyshao.util.function;

import com.github.andyshao.lang.Convert;
import com.github.andyshao.util.stream.RuntimeExceptionFactory;

import java.util.Objects;
import java.util.function.Function;

/**
 * 
 * 
 * Title:
* Description:
* Copyright: Copryright(c) May 27, 2019
* Encoding: UNIX UTF-8 * * @author Andy.Shao * * @param argument type * @param return type * @see Function */ @FunctionalInterface public interface ExceptionableFunction { /** * apply * @param t argument * @return ret * @throws Throwable any error */ R apply(T t) throws Throwable; /** * to function * @param factory exception convert factory * @return {@link Convert} * @param left type * @param right type */ static Convert, Function> toFunction(RuntimeExceptionFactory factory) { return input -> { return t -> { try { return input.apply(t); } catch (Throwable e) { throw factory.build(e); } }; }; } /** * to function * @return {@link Convert} * @param left type * @param right type */ static Convert, Function> toFunction() { return toFunction(RuntimeExceptionFactory.DEFAULT); } /** * compose * @param before before {@link ExceptionableFunction} * @return new {@link ExceptionableFunction} * @param data type */ default ExceptionableFunction compose(ExceptionableFunction before) { Objects.requireNonNull(before); return v -> this.apply(before.apply(v)); } /** * and then * @param after after {@link ExceptionableFunction} * @return a new {@link ExceptionableFunction} * @param data type */ default ExceptionableFunction andThen(ExceptionableFunction after) { Objects.requireNonNull(after); return t -> after.apply(this.apply(t)); } /** * identity * @return {@link ExceptionableFunction} * @param data type */ static ExceptionableFunction identity() { return t -> t; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy