package com.github.markozajc.ef; import java.util.concurrent.Callable; import java.util.function.*; import java.util.function.IntPredicate; import java.util.function.LongPredicate; import javax.annotation.Nonnull; import com.github.markozajc.ef.biconsumer.*; import com.github.markozajc.ef.biconsumer.except.*; import com.github.markozajc.ef.bifunction.*; import com.github.markozajc.ef.bifunction.except.*; import com.github.markozajc.ef.bipredicate.*; import com.github.markozajc.ef.bipredicate.except.*; import com.github.markozajc.ef.consumer.*; import com.github.markozajc.ef.consumer.execpt.*; import com.github.markozajc.ef.function.*; import com.github.markozajc.ef.function.except.*; import com.github.markozajc.ef.predicate.*; import com.github.markozajc.ef.predicate.except.*; import com.github.markozajc.ef.runnable.except.ERunnable; import com.github.markozajc.ef.supplier.except.*; import com.github.markozajc.ef.triconsumer.*; import com.github.markozajc.ef.trifunction.*; import com.github.markozajc.ef.tripredicate.*; /** * A class containing various helper utilities for exceptionable (E*) functions and * {@link Callable} ({@link ESupplier}). * * @author Marko Zajc */ public class EHandle { /* * ================== Consumers ================== */ /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static Consumer handle(@Nonnull EConsumer handled, @Nonnull BiConsumer handler) { return t -> { try { handled.acceptChecked(t); } catch (Throwable e) { // NOSONAR can't catch generic exceptions handler.accept(e, t); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static BooleanConsumer handle(@Nonnull EBooleanConsumer handled, @Nonnull ObjBooleanConsumer handler) { return p -> { try { handled.acceptChecked(p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions handler.accept(e, p); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static ByteConsumer handle(@Nonnull EByteConsumer handled, @Nonnull ObjByteConsumer handler) { return p -> { try { handled.acceptChecked(p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions handler.accept(e, p); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static IntConsumer handle(@Nonnull EIntConsumer handled, @Nonnull ObjIntConsumer handler) { return p -> { try { handled.acceptChecked(p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions handler.accept(e, p); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static LongConsumer handle(@Nonnull ELongConsumer handled, @Nonnull ObjLongConsumer handler) { return p -> { try { handled.acceptChecked(p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions handler.accept(e, p); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static ShortConsumer handle(@Nonnull EShortConsumer handled, @Nonnull ObjShortConsumer handler) { return p -> { try { handled.acceptChecked(p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions handler.accept(e, p); } }; } /* * ================== BiConsumers ================== */ /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static BiConsumer handle(@Nonnull EBiConsumer handled, @Nonnull TriConsumer handler) { return (t, u) -> { try { handled.acceptChecked(t, u); } catch (Throwable e) { // NOSONAR can't catch generic exceptions handler.accept(e, t, u); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static ObjBooleanConsumer handle(@Nonnull EObjBooleanConsumer handled, @Nonnull ObjObjBooleanConsumer handler) { return (t, p) -> { try { handled.acceptChecked(t, p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions handler.accept(e, t, p); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static ObjByteConsumer handle(@Nonnull EObjByteConsumer handled, @Nonnull ObjObjByteConsumer handler) { return (t, p) -> { try { handled.acceptChecked(t, p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions handler.accept(e, t, p); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static ObjIntConsumer handle(@Nonnull EObjIntConsumer handled, @Nonnull ObjObjIntConsumer handler) { return (t, p) -> { try { handled.acceptChecked(t, p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions handler.accept(e, t, p); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static ObjLongConsumer handle(@Nonnull EObjLongConsumer handled, @Nonnull ObjObjLongConsumer handler) { return (t, p) -> { try { handled.acceptChecked(t, p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions handler.accept(e, t, p); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static ObjShortConsumer handle(@Nonnull EObjShortConsumer handled, @Nonnull ObjObjShortConsumer handler) { return (t, p) -> { try { handled.acceptChecked(t, p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions handler.accept(e, t, p); } }; } /* * ================== Functions ================== */ /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static Function handle(@Nonnull EFunction handled, @Nonnull BiFunction handler) { return t -> { try { return handled.applyChecked(t); } catch (Throwable e) { // NOSONAR can't catch generic exceptions return handler.apply(e, t); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static BooleanFunction handle(@Nonnull EBooleanFunction handled, @Nonnull ObjBooleanFunction handler) { return p -> { try { return handled.applyChecked(p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions return handler.apply(e, p); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static ByteFunction handle(@Nonnull EByteFunction handled, @Nonnull ObjByteFunction handler) { return p -> { try { return handled.applyChecked(p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions return handler.apply(e, p); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static IntFunction handle(@Nonnull EIntFunction handled, @Nonnull ObjIntFunction handler) { return p -> { try { return handled.applyChecked(p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions return handler.apply(e, p); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static LongFunction handle(@Nonnull ELongFunction handled, @Nonnull ObjLongFunction handler) { return p -> { try { return handled.applyChecked(p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions return handler.apply(e, p); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static ShortFunction handle(@Nonnull EShortFunction handled, @Nonnull ObjShortFunction handler) { return p -> { try { return handled.applyChecked(p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions return handler.apply(e, p); } }; } /* * ================== BiFunctions ================== */ /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static BiFunction handle(@Nonnull EBiFunction handled, @Nonnull TriFunction handler) { return (t, u) -> { try { return handled.applyChecked(t, u); } catch (Throwable e) { // NOSONAR can't catch generic exceptions return handler.apply(e, t, u); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static ObjBooleanFunction handle(@Nonnull EObjBooleanFunction handled, @Nonnull ObjObjBooleanFunction handler) { return (t, p) -> { try { return handled.applyChecked(t, p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions return handler.apply(e, t, p); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static ObjByteFunction handle(@Nonnull EObjByteFunction handled, @Nonnull ObjObjByteFunction handler) { return (t, p) -> { try { return handled.applyChecked(t, p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions return handler.apply(e, t, p); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static ObjIntFunction handle(@Nonnull EObjIntFunction handled, @Nonnull ObjObjIntFunction handler) { return (t, p) -> { try { return handled.applyChecked(t, p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions return handler.apply(e, t, p); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static ObjLongFunction handle(@Nonnull EObjLongFunction handled, @Nonnull ObjObjLongFunction handler) { return (t, p) -> { try { return handled.applyChecked(t, p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions return handler.apply(e, t, p); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static ObjShortFunction handle(@Nonnull EObjShortFunction handled, @Nonnull ObjObjShortFunction handler) { return (t, p) -> { try { return handled.applyChecked(t, p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions return handler.apply(e, t, p); } }; } /* * ================== Predicates ================== */ /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static Predicate handle(@Nonnull EPredicate handled, @Nonnull BiPredicate handler) { return t -> { try { return handled.testChecked(t); } catch (Throwable e) { // NOSONAR can't catch generic exceptions return handler.test(e, t); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static BooleanPredicate handle(@Nonnull EBooleanPredicate handled, @Nonnull ObjBooleanPredicate handler) { return p -> { try { return handled.testChecked(p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions return handler.test(e, p); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static BytePredicate handle(@Nonnull EBytePredicate handled, @Nonnull ObjBytePredicate handler) { return p -> { try { return handled.testChecked(p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions return handler.test(e, p); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static IntPredicate handle(@Nonnull EIntPredicate handled, @Nonnull ObjIntPredicate handler) { return p -> { try { return handled.testChecked(p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions return handler.test(e, p); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static LongPredicate handle(@Nonnull ELongPredicate handled, @Nonnull ObjLongPredicate handler) { return p -> { try { return handled.testChecked(p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions return handler.test(e, p); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static ShortPredicate handle(@Nonnull EShortPredicate handled, @Nonnull ObjShortPredicate handler) { return p -> { try { return handled.testChecked(p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions return handler.test(e, p); } }; } /* * ================== BiPredicates ================== */ /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static BiPredicate handle(@Nonnull EBiPredicate handled, @Nonnull TriPredicate handler) { return (t, u) -> { try { return handled.testChecked(t, u); } catch (Throwable e) { // NOSONAR can't catch generic exceptions return handler.test(e, t, u); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static ObjBooleanPredicate handle(@Nonnull EObjBooleanPredicate handled, @Nonnull ObjObjBooleanPredicate handler) { return (t, p) -> { try { return handled.testChecked(t, p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions return handler.test(e, t, p); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static ObjBytePredicate handle(@Nonnull EObjBytePredicate handled, @Nonnull ObjObjBytePredicate handler) { return (t, p) -> { try { return handled.testChecked(t, p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions return handler.test(e, t, p); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static ObjIntPredicate handle(@Nonnull EObjIntPredicate handled, @Nonnull ObjObjIntPredicate handler) { return (t, p) -> { try { return handled.testChecked(t, p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions return handler.test(e, t, p); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static ObjLongPredicate handle(@Nonnull EObjLongPredicate handled, @Nonnull ObjObjLongPredicate handler) { return (t, p) -> { try { return handled.testChecked(t, p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions return handler.test(e, t, p); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static ObjShortPredicate handle(@Nonnull EObjShortPredicate handled, @Nonnull ObjObjShortPredicate handler) { return (t, p) -> { try { return handled.testChecked(t, p); } catch (Throwable e) { // NOSONAR can't catch generic exceptions return handler.test(e, t, p); } }; } /* * ================== Runnables ================== */ /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static Runnable handle(@Nonnull ERunnable handled, @Nonnull Consumer handler) { return () -> { try { handled.runChecked(); } catch (Throwable e) { // NOSONAR can't catch generic exceptions handler.accept(e); } }; } /* * ================== Suppliers & Callable ================== */ /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static Supplier handle(@Nonnull Callable handled, @Nonnull Function handler) { return () -> { try { return handled.call(); } catch (Exception e) { return handler.apply(e); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static Supplier handle(@Nonnull ESupplier handled, @Nonnull Function handler) { return () -> { try { return handled.getChecked(); } catch (Throwable e) { // NOSONAR can't catch generic exceptions return handler.apply(e); } }; } /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static BooleanSupplier handle(@Nonnull EBooleanSupplier handled, @Nonnull Predicate handler) { return () -> { try { return handled.getAsBoolean(); } catch (Throwable e) { // NOSONAR can't catch generic exceptions return handler.test(e); } }; } // missing: EByteSupplier, blocked by: ToByteFunction /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static IntSupplier handle(@Nonnull EIntSupplier handled, @Nonnull ToIntFunction handler) { return () -> { try { return handled.getAsInt(); } catch (Throwable e) { // NOSONAR can't catch generic exceptions return handler.applyAsInt(e); } }; } // missing: EShortSupplier, blocked by: ToShortFunction /** * Handles any exception types that may occur in the handled function with a provided * handler. * * @param * The handled function's exception type * @param handled * The function that may throw an exception * @param handler * The function handling exceptions * * @return An exception-proofed function */ @Nonnull public static LongSupplier handle(@Nonnull ELongSupplier handled, @Nonnull ToLongFunction handler) { return () -> { try { return handled.getAsLong(); } catch (Throwable e) { // NOSONAR can't catch generic exceptions return handler.applyAsLong(e); } }; } private EHandle() {} }