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

org.apache.commons.io.function.Uncheck Maven / Gradle / Ivy

There is a newer version: 2024.11.18751.20241128T090041Z-241100
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.commons.io.function;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.function.Supplier;

/**
 * Unchecks calls by throwing {@link UncheckedIOException} instead of {@link IOException}.
 *
 * @since 2.12.0
 */
public final class Uncheck {

    /**
     * Accepts an IO consumer with the given arguments.
     *
     * @param  the first input type.
     * @param  the second input type.
     * @param t the first input argument.
     * @param u the second input argument.
     * @param consumer Consumes the value.
     * @throws UncheckedIOException if an I/O error occurs.
     */
    public static  void accept(final IOBiConsumer consumer, final T t, final U u) {
        try {
            consumer.accept(t, u);
        } catch (final IOException e) {
            throw wrap(e);
        }
    }

    /**
     * Accepts an IO consumer with the given argument.
     *
     * @param  the input type.
     * @param t the input argument.
     * @param consumer Consumes the value.
     * @throws UncheckedIOException if an I/O error occurs.
     */
    public static  void accept(final IOConsumer consumer, final T t) {
        try {
            consumer.accept(t);
        } catch (final IOException e) {
            throw wrap(e);
        }
    }

    /**
     * Accepts an IO consumer with the given arguments.
     *
     * @param  the first input type.
     * @param  the second input type.
     * @param  the third input type.
     * @param t the first input argument.
     * @param u the second input argument.
     * @param v the third input argument.
     * @param consumer Consumes the value.
     * @throws UncheckedIOException if an I/O error occurs.
     */
    public static  void accept(final IOTriConsumer consumer, final T t, final U u, final V v) {
        try {
            consumer.accept(t, u, v);
        } catch (final IOException e) {
            throw wrap(e);
        }
    }

    /**
     * Applies an IO function with the given arguments.
     *
     * @param  the first function argument type.
     * @param  the second function argument type.
     * @param  the return type.
     * @param function the function.
     * @param t the first function argument.
     * @param u the second function argument.
     * @return the function result.
     * @throws UncheckedIOException if an I/O error occurs.
     */
    public static  R apply(final IOBiFunction function, final T t, final U u) {
        try {
            return function.apply(t, u);
        } catch (final IOException e) {
            throw wrap(e);
        }
    }

    /**
     * Applies an IO function with the given arguments.
     *
     * @param function the function.
     * @param  the first function argument type.
     * @param  the return type.
     * @param t the first function argument.
     * @return the function result.
     * @throws UncheckedIOException if an I/O error occurs.
     */
    public static  R apply(final IOFunction function, final T t) {
        try {
            return function.apply(t);
        } catch (final IOException e) {
            throw wrap(e);
        }
    }

    /**
     * Applies an IO quad-function with the given arguments.
     *
     * @param function the function.
     * @param  the first function argument type.
     * @param  the second function argument type.
     * @param  the third function argument type.
     * @param  the fourth function argument type.
     * @param  the return type.
     * @param t the first function argument.
     * @param u the second function argument.
     * @param v the third function argument.
     * @param w the fourth function argument.
     * @return the function result.
     * @throws UncheckedIOException if an I/O error occurs.
     */
    public static  R apply(final IOQuadFunction function, final T t, final U u, final V v, final W w) {
        try {
            return function.apply(t, u, v, w);
        } catch (final IOException e) {
            throw wrap(e);
        }
    }

    /**
     * Applies an IO tri-function with the given arguments.
     *
     * @param  the first function argument type.
     * @param  the second function argument type.
     * @param  the third function argument type.
     * @param  the return type.
     * @param function the function.
     * @param t the first function argument.
     * @param u the second function argument.
     * @param v the third function argument.
     * @return the function result.
     * @throws UncheckedIOException if an I/O error occurs.
     */
    public static  R apply(final IOTriFunction function, final T t, final U u, final V v) {
        try {
            return function.apply(t, u, v);
        } catch (final IOException e) {
            throw wrap(e);
        }
    }

    /**
     * Compares the arguments with the comparator.
     *
     * @param  the first function argument type.
     * @param comparator the function.
     * @param t the first function argument.
     * @param u the second function argument.
     * @return the comparator result.
     * @throws UncheckedIOException if an I/O error occurs.
     */
    public static  int compare(final IOComparator comparator, final T t, final T u) {
        try {
            return comparator.compare(t, u);
        } catch (final IOException e) {
            throw wrap(e);
        }
    }

    /**
     * Gets the result from an IO supplier.
     *
     * @param  the return type of the operations.
     * @param supplier Supplies the return value.
     * @return result from the supplier.
     * @throws UncheckedIOException if an I/O error occurs.
     */
    public static  T get(final IOSupplier supplier) {
        try {
            return supplier.get();
        } catch (final IOException e) {
            throw wrap(e);
        }
    }

    /**
     * Gets the result from an IO supplier.
     *
     * @param  the return type of the operations.
     * @param supplier Supplies the return value.
     * @param message The UncheckedIOException message if an I/O error occurs.
     * @return result from the supplier.
     * @throws UncheckedIOException if an I/O error occurs.
     */
    public static  T get(final IOSupplier supplier, final Supplier message) {
        try {
            return supplier.get();
        } catch (final IOException e) {
            throw wrap(e, message);
        }
    }

    /**
     * Gets the result from an IO int supplier.
     *
     * @param supplier Supplies the return value.
     * @return result from the supplier.
     * @throws UncheckedIOException if an I/O error occurs.
     * @since 2.14.0
     */
    public static int getAsInt(final IOIntSupplier supplier) {
        try {
            return supplier.getAsInt();
        } catch (final IOException e) {
            throw wrap(e);
        }
    }

    /**
     * Gets the result from an IO int supplier.
     *
     * @param supplier Supplies the return value.
     * @param message The UncheckedIOException message if an I/O error occurs.
     * @return result from the supplier.
     * @throws UncheckedIOException if an I/O error occurs.
     * @since 2.14.0
     */
    public static int getAsInt(final IOIntSupplier supplier, final Supplier message) {
        try {
            return supplier.getAsInt();
        } catch (final IOException e) {
            throw wrap(e, message);
        }
    }

    /**
     * Gets the result from an IO long supplier.
     *
     * @param supplier Supplies the return value.
     * @return result from the supplier.
     * @throws UncheckedIOException if an I/O error occurs.
     * @since 2.14.0
     */
    public static long getAsLong(final IOLongSupplier supplier) {
        try {
            return supplier.getAsLong();
        } catch (final IOException e) {
            throw wrap(e);
        }
    }

    /**
     * Gets the result from an IO long supplier.
     *
     * @param supplier Supplies the return value.
     * @param message The UncheckedIOException message if an I/O error occurs.
     * @return result from the supplier.
     * @throws UncheckedIOException if an I/O error occurs.
     * @since 2.14.0
     */
    public static long getAsLong(final IOLongSupplier supplier, final Supplier message) {
        try {
            return supplier.getAsLong();
        } catch (final IOException e) {
            throw wrap(e, message);
        }
    }

    /**
     * Runs an IO runnable.
     *
     * @param runnable The runnable to run.
     * @throws UncheckedIOException if an I/O error occurs.
     */
    public static void run(final IORunnable runnable) {
        try {
            runnable.run();
        } catch (final IOException e) {
            throw wrap(e);
        }
    }

    /**
     * Runs an IO runnable.
     *
     * @param runnable The runnable to run.
     * @param message The UncheckedIOException message if an I/O error occurs.
     * @throws UncheckedIOException if an I/O error occurs.
     * @since 2.14.0
     */
    public static void run(final IORunnable runnable, final Supplier message) {
        try {
            runnable.run();
        } catch (final IOException e) {
            throw wrap(e, message);
        }
    }

    /**
     * Tests an IO predicate.
     *
     * @param  the type of the input to the predicate.
     * @param predicate the predicate.
     * @param t the input to the predicate.
     * @return {@code true} if the input argument matches the predicate, otherwise {@code false}.
     */
    public static  boolean test(final IOPredicate predicate, final T t) {
        try {
            return predicate.test(t);
        } catch (final IOException e) {
            throw wrap(e);
        }
    }

    /**
     * Constructs a new {@link UncheckedIOException} for the given exception.
     *
     * @param e The exception to wrap.
     * @return a new {@link UncheckedIOException}.
     */
    private static UncheckedIOException wrap(final IOException e) {
        return new UncheckedIOException(e);
    }

    /**
     * Constructs a new {@link UncheckedIOException} for the given exception and detail message.
     *
     * @param e The exception to wrap.
     * @param message The UncheckedIOException message if an I/O error occurs.
     * @return a new {@link UncheckedIOException}.
     */
    private static UncheckedIOException wrap(final IOException e, final Supplier message) {
        return new UncheckedIOException(message.get(), e);
    }

    /**
     * No instances needed.
     */
    private Uncheck() {
        // no instances needed.
    }
}