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

no.digipost.DiggExceptions Maven / Gradle / Ivy

There is a newer version: 0.36
Show newest version
/*
 * Copyright (C) Posten Norge AS
 *
 * 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 no.digipost;

import no.digipost.function.*;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;

public final class DiggExceptions {

    /**
     * @param t the Throwable to get the causal chain from.
     * @return the entire chain of causes, including the given Throwable.
     */
    public static Stream causalChainOf(Throwable t) {
        Stream.Builder causes = Stream.builder();
        for (Throwable cause = t; cause != null; cause = cause.getCause()) {
            causes.add(cause);
        }
        return causes.build();
    }

    public static String exceptionNameAndMessage(Throwable t) {
        return t.getClass().getSimpleName() + ": '" + t.getMessage() + "'";
    }

    public static RuntimeException asUnchecked(Throwable t) {
        return asUnchecked(t, DiggExceptions::exceptionNameAndMessage);
    }

    public static  RuntimeException asUnchecked(X t, Function message) {
        return t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(message.apply(t), t);
    }

    /**
     * Immediately {@link ThrowingFunction#apply apply} the given {@code function} with the given {@code argument},
     * and if needed, convert any thrown exceptions to unckecked.
     *
     * @param function The {@link ThrowingFunction}.
     * @param argument The argument to pass to {@link ThrowingFunction#apply(Object)}
     * @return the result.
     */
    public static  R applyUnchecked(ThrowingFunction function, T argument) {
        return function.asUnchecked().apply(argument);
    }

    /**
     * Immediately {@link ThrowingSupplier#get get} a result from the given {@code supplier},
     * and if needed, convert any thrown exceptions to unckecked.
     *
     * @param supplier The {@link ThrowingSupplier}.
     * @return the result.
     */
    public static  T getUnchecked(ThrowingSupplier supplier) {
        return supplier.asUnchecked().get();
    }

    /**
     * Immediately {@link ThrowingRunnable#run() run} the given {@code runnable},
     * and if needed, convert any thrown exceptions to unckecked.
     *
     * @param runnable The {@link ThrowingRunnable}.
     */
    public static void runUnchecked(ThrowingRunnable runnable) {
        runnable.asUnchecked().run();
    }


    /**
     * Convenience to acquire a {@link ThrowingFunction}-reference from a lambda expression.
     */
    public static final  ThrowingFunction mayThrow(ThrowingFunction function) {
        return function;
    }

    /**
     * Convenience to acquire a {@link ThrowingBiFunction}-reference from a lambda expression.
     */
    public static final  ThrowingBiFunction mayThrow(ThrowingBiFunction bifunction) {
        return bifunction;
    }

    /**
     * Convenience to acquire a {@link ThrowingSupplier}-reference from a lambda expression.
     */
    public static final  ThrowingSupplier mayThrow(ThrowingSupplier supplier) {
        return supplier;
    }

    /**
     * Convenience to acquire a {@link ThrowingConsumer}-reference from a lambda expression.
     */
    public static final  ThrowingConsumer mayThrow(ThrowingConsumer consumer) {
        return consumer;
    }

    /**
     * Convenience to acquire a {@link ThrowingBiConsumer}-reference from a lambda expression.
     */
    public static final  ThrowingBiConsumer mayThrow(ThrowingBiConsumer consumer) {
        return consumer;
    }

    /**
     * Convenience to acquire a {@link ThrowingRunnable}-reference from a lambda expression.
     */
    public static final  ThrowingRunnable mayThrow(ThrowingRunnable runnable) {
        return runnable;
    }


    /**
     * This consumer rethrows any given {@link Exception} as an unchecked {@link RuntimeException}.
     */
    public static final Consumer rethrowAnyException = rethrow(DiggExceptions::asUnchecked);


    /**
     * Create a exception handler ({@link Consumer}) which simply rethrows given exceptions.
     *
     * @param createUnchecked a function to convert a (typically) checked exception to {@link RuntimeException}.
     * @return the {@link Consumer} which rethrows any given exception.
     */
    public static final  Consumer rethrow(Function createUnchecked) {
        return e -> { throw createUnchecked.apply(e); };
    }



    private DiggExceptions() {}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy