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

alexh.Unchecker Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2015 Alex Butler
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package alexh;

import java.util.function.*;

/**
 * Utility methods to handle unwanted checked exceptions, avoiding try-catch blocks that simply wrap checked
 * exceptions in unchecked exceptions
 */
public class Unchecker {

    private static final Function DEFAULT_EXCEPTION_TRANSFORMER = RuntimeException::new;

    /**
     * Converts Checked throwing supplier -> standard supplier, any checked exceptions are wrapped using the input
     * exception transformer
     * @param supplier supplier that can throw a checked exception
     * @param exTransformer checked -> unchecked exception transformer
     * @return supplier that will not throw checked exceptions
     */
    public static  Supplier uncheck(ThrowingSupplier supplier, Function exTransformer) {
        return () -> {
            try { return supplier.get(); }
            catch (RuntimeException | Error e) { throw e; }
            catch (Throwable t) { throw exTransformer.apply(t); }
        };
    }

    /**
     * Gets the supplier result wrapping any checked exceptions using the input exception transformer
     * @param supplier supplier that can throw a checked exception
     * @param exTransformer checked -> unchecked exception transformer
     * @param  Supplier / return type
     * @return Supplier result
     */
    public static  T uncheckedGet(ThrowingSupplier supplier, Function exTransformer) {
        return uncheck(supplier, exTransformer).get();
    }

    /**
     * Runs wrapping any checked exceptions using the input exception transformer
     * @param runnable supplier that can throw a checked exception
     * @param exTransformer checked -> unchecked exception transformer
     */
    public static void unchecked(ThrowingRunnable runnable, Function exTransformer) {
        uncheck(runnable, exTransformer).run();
    }

    /**
     * Converts Checked throwing function -> standard function, any checked exceptions are wrapped using the input
     * exception transformer
     * @param function function that can throw a checked exception
     * @param exTransformer checked -> unchecked exception transformer
     * @return function that will not throw checked exceptions
     */
    public static  Function uncheck(ThrowingFunction function, Function exTransformer) {
        return (In in) -> uncheckedGet(() -> function.apply(in), exTransformer);
    }

    /** As {@link Unchecker#uncheck(alexh.Unchecker.ThrowingFunction, java.util.function.Function)} for BiFunctions */
    public static  BiFunction uncheck(ThrowingBiFunction function, Function exTransformer) {
        return (In1 in1, In2 in2) -> uncheckedGet(() -> function.apply(in1, in2), exTransformer);
    }

    /**
     * Converts Checked throwing runnable -> standard runnable, any checked exceptions are wrapped using the input
     * exception transformer
     * @param runnable function that can throw a checked exception
     * @param exTransformer checked -> unchecked exception transformer
     * @return runnable that will not throw checked exceptions
     */
    public static Runnable uncheck(ThrowingRunnable runnable, Function exTransformer) {
        return () -> uncheckedGet(() -> {
            runnable.run();
            return null;
        }, exTransformer);
    }

    /**
     * Converts Checked throwing consumer -> standard consumer, any checked exceptions are wrapped using the input
     * exception transformer
     * @param consumer function that can throw a checked exception
     * @param exTransformer checked -> unchecked exception transformer
     * @return consumer that will not throw checked exceptions
     */
    public static  Consumer uncheck(ThrowingConsumer consumer, Function exTransformer) {
        return (T t) -> unchecked(() -> consumer.accept(t), exTransformer);
    }

    /** As {@link Unchecker#uncheck(alexh.Unchecker.ThrowingConsumer, java.util.function.Function)} for BiConsumers */
    public static  BiConsumer uncheck(ThrowingBiConsumer consumer, Function exTransformer) {
        return (T t, U u) -> unchecked(() -> consumer.accept(t, u), exTransformer);
    }

    /**
     * As {@link Unchecker#uncheck(alexh.Unchecker.ThrowingSupplier, java.util.function.Function)}
     * wrapping checked exceptions in {@link RuntimeException}s
     */
    public static  Supplier uncheck(ThrowingSupplier supplier) {
        return uncheck(supplier, DEFAULT_EXCEPTION_TRANSFORMER);
    }

    /**
     * As {@link Unchecker#uncheckedGet(alexh.Unchecker.ThrowingSupplier, java.util.function.Function)}
     * wrapping checked exceptions in {@link RuntimeException}s
     */
    public static  T uncheckedGet(ThrowingSupplier supplier) {
        return uncheckedGet(supplier, DEFAULT_EXCEPTION_TRANSFORMER);
    }

    /**
     * As {@link Unchecker#unchecked(alexh.Unchecker.ThrowingRunnable, java.util.function.Function)}
     * wrapping checked exceptions in {@link RuntimeException}s
     */
    public static void unchecked(ThrowingRunnable runnable) {
        unchecked(runnable, DEFAULT_EXCEPTION_TRANSFORMER);
    }

    /**
     * As {@link Unchecker#uncheck(alexh.Unchecker.ThrowingFunction, java.util.function.Function)}
     * wrapping checked exceptions in {@link RuntimeException}s
     */
    public static  Function uncheck(ThrowingFunction function) {
        return uncheck(function, DEFAULT_EXCEPTION_TRANSFORMER);
    }

    /**
     * As {@link Unchecker#uncheck(alexh.Unchecker.ThrowingBiFunction, java.util.function.Function)}
     * wrapping checked exceptions in {@link RuntimeException}s
     */
    public static  BiFunction uncheck(ThrowingBiFunction function) {
        return uncheck(function, DEFAULT_EXCEPTION_TRANSFORMER);
    }

    /**
     * As {@link Unchecker#uncheck(alexh.Unchecker.ThrowingRunnable, java.util.function.Function)}
     * wrapping checked exceptions in {@link RuntimeException}s
     */
    public static Runnable uncheck(ThrowingRunnable runnable) {
        return uncheck(runnable, DEFAULT_EXCEPTION_TRANSFORMER);
    }

    /**
     * As {@link Unchecker#uncheck(alexh.Unchecker.ThrowingConsumer, java.util.function.Function)}
     * wrapping checked exceptions in {@link RuntimeException}s
     */
    public static  Consumer uncheck(ThrowingConsumer consumer) {
        return uncheck(consumer, DEFAULT_EXCEPTION_TRANSFORMER);
    }

    /**
     * As {@link Unchecker#uncheck(alexh.Unchecker.ThrowingBiConsumer, java.util.function.Function)}
     * wrapping checked exceptions in {@link RuntimeException}s
     */
    public static  BiConsumer uncheck(ThrowingBiConsumer consumer) {
        return uncheck(consumer, DEFAULT_EXCEPTION_TRANSFORMER);
    }

    /**
     * Represents a supplier of results, that could throw a checked exception
     * @see java.util.function.Supplier
     */
    @FunctionalInterface
    public interface ThrowingSupplier {
        T get() throws Throwable;
    }

    /**
     * Operation that returns no result, but could throw a checked exception
     * @see java.lang.Runnable
     */
    @FunctionalInterface
    public interface ThrowingRunnable {
        void run() throws Throwable;
    }

    /**
     * Function that accepts a single argument and produces a result, but could throw a checked exception
     * @see java.util.function.Function
     */
    @FunctionalInterface
    public interface ThrowingFunction {
        Out apply(In in) throws Throwable;
    }

    /**
     * Function that accepts two arguments and produces a result, but could throw a checked exception
     * @see java.util.function.BiFunction
     */
    @FunctionalInterface
    public interface ThrowingBiFunction {
        Out apply(In1 in1, In2 in2) throws Throwable;
    }

    /**
     * Operation that accepts a single input argument and returns no result, but could throw a checked exception
     * @see java.util.function.Consumer
     */
    @FunctionalInterface
    public interface ThrowingConsumer {
        void accept(T t) throws Throwable;
    }

    /**
     * Operation that accepts two arguments and returns no result, but could throw a checked exception
     * @see java.util.function.BiConsumer
     */
    @FunctionalInterface
    public interface ThrowingBiConsumer {
        void accept(T t, U u) throws Throwable;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy