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

net.mintern.functions.nullary.NilToObj Maven / Gradle / Ivy

The newest version!
package net.mintern.functions.nullary;

/**
 * An operation of type {@code () -> R}.
 *
 * @param  the type of the return value
 */
@FunctionalInterface
public interface NilToObj extends
        net.mintern.functions.nullary.checked.NilToObjE,
        java.util.function.Supplier {

    /**
     * Returns a wrapped version of {@code f} that uses {@code toRuntime} to convert any checked
     * {@code Exception} to a {@code RuntimeException}.
     *
     * @param  the type of the return value
     * @param  the {@code Exception} type that the operation may throw
     * @param toRuntime if a checked exception is thrown from
     *      {@link net.mintern.functions.nullary.checked.NilToObjE#call}, then this function
     *      is called in in order to convert it to a {@code RuntimeException}
     * @param f the operation to wrap
     * @return a wrapped version of {@code f} that does not throw checked exceptions
     */
    @SuppressWarnings("unchecked")
    static  NilToObj unchecked(
            java.util.function.Function toRuntime,
            net.mintern.functions.nullary.checked.NilToObjE f) {
        return () -> {
            try {
                return f.call();
            } catch (RuntimeException e) {
                throw e;
            } catch (Exception e) {
                throw toRuntime.apply((E) e);
            }
        };
    }

    /**
     * Returns a wrapped version of {@code f} that wraps any checked {@code Exception} with a
     * {@code RuntimeException}.
     *
     * @param  the type of the return value
     * @param  the {@code Exception} type that the operation may throw
     * @param f the operation to wrap
     * @return a wrapped version of {@code f} that does not throw checked exceptions
     */
    static  NilToObj unchecked(
            net.mintern.functions.nullary.checked.NilToObjE f) {
        return unchecked(RuntimeException::new, f);
    }

    /**
     * Returns a wrapped version of {@code f} that wraps any {@code IOException} with an
     * {@link java.io.UncheckedIOException}.
     *
     * @param  the type of the return value
     * @param  the {@code Exception} type that the operation may throw
     * @param f the operation to wrap
     * @return a wrapped version of {@code f} that throws {@code UncheckedIOException} instead of
     *      {@code IOException}
     */
    static  NilToObj uncheckedIO(
            net.mintern.functions.nullary.checked.NilToObjE f) {
        return unchecked(java.io.UncheckedIOException::new, f);
    }

    /**
     * Allows {@code this} to act as a {@code Supplier}.
     *
     * @return the result of the operation
     */
    @Override
    default R get() {
        return call();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy