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

de.team33.libs.exceptional.v3.Exposer Maven / Gradle / Ivy

There is a newer version: 8.3.2
Show newest version
package de.team33.libs.exceptional.v3;

import java.util.function.Supplier;


/**
 * A Tool that can execute methods that may throw a {@link WrappedException} caused by a checked Exception of
 * a certain type. If this is the case, the latter will be unwrapped and re-thrown.
 *
 * @see BiExposer
 * @see TriExposer
 */
public class Exposer {

    private final Class xClass;

    private Exposer(final Class xClass) {
        this.xClass = xClass;
    }

    /**
     * Returns a new instance that handles a given exception type (when wrapped in a {@link WrappedException}).
     */
    public static  Exposer expose(final Class xClass) {
        return new Exposer<>(xClass);
    }

    private static Supplier wrap(final Runnable runnable) {
        return () -> {
            runnable.run();
            return null;
        };
    }

    /**
     * Immediately executes a runnable.
     *
     * @throws X                if the runnable causes a {@link WrappedException}, which in turn is caused by
     *                          an exception of type X
     * @throws Error            if the runnable causes an {@link Error} or a {@link WrappedException},
     *                          which in turn is caused by an {@link Error}
     * @throws RuntimeException if the runnable causes a {@link RuntimeException} or a
     *                          {@link WrappedException} which in turn is caused by another {@link RuntimeException}
     * @throws WrappedException if the runnable causes a {@link WrappedException} that cannot be unwrapped in
     *                          any of the above ways
     */
    public final void run(final Runnable runnable) throws X {
        get(wrap(runnable));
    }

    /**
     * Immediately executes a supplier and returns its result.
     *
     * @throws X                if the supplier causes a {@link WrappedException}, which in turn is caused by
     *                          an exception of type X
     * @throws Error            if the supplier causes an {@link Error} or a {@link WrappedException},
     *                          which in turn is caused by an {@link Error}
     * @throws RuntimeException if the supplier causes a {@link RuntimeException} or a
     *                          {@link WrappedException} which in turn is caused by another {@link RuntimeException}
     * @throws WrappedException if the supplier causes a {@link WrappedException} that cannot be unwrapped in
     *                          any of the above ways
     */
    public final  T get(final Supplier supplier) throws X {
        try {
            return supplier.get();
        } catch (final WrappedException caught) {
            throw caught
                    .reThrowCauseIf(Error.class)
                    .reThrowCauseIf(RuntimeException.class)
                    .reThrowCauseIf(xClass);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy