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

io.github.kits.LambdaExKit Maven / Gradle / Ivy

The newest version!
package io.github.kits;

import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * lambda受检异常再次抛出, 不用捕获
 *
 * @project: kits
 * @created: with IDEA
 * @author: kits
 * @date: 2018 08 21 上午9:5420 | 八月. 星期二
 */
public class LambdaExKit {

    @FunctionalInterface
    public interface ConsumerWithExceptions {
        void accept(T t) throws E, ReflectiveOperationException;
    }

    @FunctionalInterface
    public interface BiConsumerWithExceptions {
        void accept(T t, U u) throws E;
    }

    @FunctionalInterface
    public interface FunctionWithExceptions {
        R apply(T t) throws E;
    }

    @FunctionalInterface
    public interface SupplierWithExceptions {
        T get() throws E;
    }

    @FunctionalInterface
    public interface RunnableWithExceptions {
        void run() throws E;
    }

    /** .forEach(rethrowConsumer(name -> System.out.println(Class.forName(name)))); or .forEach(rethrowConsumer(ClassNameUtil::println)); */
    public static  Consumer rethrowConsumer(ConsumerWithExceptions consumer) throws E {
        return t -> {
            try {
                consumer.accept(t);
            } catch (Exception exception) {
                throwAsUnchecked(exception);
            }
        };
    }

    public static  BiConsumer rethrowBiConsumer(BiConsumerWithExceptions biConsumer) throws E {
        return (t, u) -> {
            try {
                biConsumer.accept(t, u);
            } catch (Exception exception) {
                throwAsUnchecked(exception);
            }
        };
    }

    /** .map(rethrowFunction(name -> Class.forName(name))) or .map(rethrowFunction(Class::forName)) */
    public static  Function rethrowFunction(FunctionWithExceptions function) throws E {
        return t -> {
            try {
                return function.apply(t);
            } catch (Exception exception) {
                throwAsUnchecked(exception);
                return null;
            }
        };
    }

    /** rethrowSupplier(() -> new StringJoiner(new String(new byte[]{77, 97, 114, 107}, "UTF-8"))), */
    public static  Supplier rethrowSupplier(SupplierWithExceptions function) throws E {
        return () -> {
            try {
                return function.get();
            } catch (Exception exception) {
                throwAsUnchecked(exception);
                return null;
            }
        };
    }

    /** uncheck(() -> Class.forName("xxx")); */
    public static void uncheck(RunnableWithExceptions t) {
        try {
            t.run();
        } catch (Exception exception) {
            throwAsUnchecked(exception);
        }
    }

    /** uncheck(() -> Class.forName("xxx")); */
    public static  R uncheck(SupplierWithExceptions supplier) {
        try {
            return supplier.get();
        } catch (Exception exception) {
            throwAsUnchecked(exception);
            return null;
        }
    }

    /** uncheck(Class::forName, "xxx"); */
    public static  R uncheck(FunctionWithExceptions function, T t) {
        try {
            return function.apply(t);
        } catch (Exception exception) {
            throwAsUnchecked(exception);
            return null;
        }
    }

    private static  void throwAsUnchecked(Exception exception) throws E {
        throw (E)exception;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy