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

info.freelibrary.util.ThrowingConsumer Maven / Gradle / Ivy

There is a newer version: 5.2.0
Show newest version

package info.freelibrary.util;

import java.util.function.Consumer;

import info.freelibrary.util.warnings.PMD;

/**
 * A consumer that throws a runtime exception. This is not an ideal solution (as the PMD suppressions indicate), but it
 * provides a generic way to handle exceptions thrown in lambdas.
 *
 * @param  The type that the function accepts
 */
@FunctionalInterface
public interface ThrowingConsumer extends Consumer {

    @Override
    @SuppressWarnings(PMD.AVOID_CATCHING_GENERIC_EXCEPTION)
    default void accept(final T aType) {
        try {
            acceptThrows(aType);
        } catch (final Exception details) {
            throw new I18nRuntimeException(details);
        }
    }

    /**
     * A method that wraps any exceptions through by the consumer with a runtime exception.
     *
     * @param aType A type being accepted by the consumer
     * @throws Exception An exception thrown by the consumer
     */
    @SuppressWarnings(PMD.SIGNATURE_DECLARE_THROWS_EXCEPTION)
    void acceptThrows(T aType) throws Exception;

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy