    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.io.function;
    019
    020import java.io.IOException;
    021import java.io.UncheckedIOException;
    022import java.util.function.Supplier;
    023
    024/**
    025 * Unchecks calls by throwing {@link UncheckedIOException} instead of {@link IOException}.
    026 *
    027 * @since 2.12.0
    028 */
    029public final class Uncheck {
    030
    031    /**
    032     * Accepts an IO consumer with the given arguments.
    033     *
    034     * @param <T> the first input type.
    035     * @param <U> the second input type.
    036     * @param t the first input argument.
    037     * @param u the second input argument.
    038     * @param consumer Consumes the value.
    039     * @throws UncheckedIOException if an I/O error occurs.
    040     */
    041    public static <T, U> void accept(final IOBiConsumer<T, U> consumer, final T t, final U u) {
    042        try {
    043            consumer.accept(t, u);
    044        } catch (final IOException e) {
    045            throw wrap(e);
    046        }
    047    }
    048
    049    /**
    050     * Accepts an IO consumer with the given argument.
    051     *
    052     * @param <T> the input type.
    053     * @param t the input argument.
    054     * @param consumer Consumes the value.
    055     * @throws UncheckedIOException if an I/O error occurs.
    056     */
    057    public static <T> void accept(final IOConsumer<T> consumer, final T t) {
    058        try {
    059            consumer.accept(t);
    060        } catch (final IOException e) {
    061            throw wrap(e);
    062        }
    063    }
    064
    065    /**
    066     * Accepts an IO consumer with the given arguments.
    067     *
    068     * @param <T> the first input type.
    069     * @param <U> the second input type.
    070     * @param <V> the third input type.
    071     * @param t the first input argument.
    072     * @param u the second input argument.
    073     * @param v the third input argument.
    074     * @param consumer Consumes the value.
    075     * @throws UncheckedIOException if an I/O error occurs.
    076     */
    077    public static <T, U, V> void accept(final IOTriConsumer<T, U, V> consumer, final T t, final U u, final V v) {
    078        try {
    079            consumer.accept(t, u, v);
    080        } catch (final IOException e) {
    081            throw wrap(e);
    082        }
    083    }
    084
    085    /**
    086     * Applies an IO function with the given arguments.
    087     *
    088     * @param <T> the first function argument type.
    089     * @param <U> the second function argument type.
    090     * @param <R> the return type.
    091     * @param function the function.
    092     * @param t the first function argument.
    093     * @param u the second function argument.
    094     * @return the function result.
    095     * @throws UncheckedIOException if an I/O error occurs.
    096     */
    097    public static <T, U, R> R apply(final IOBiFunction<T, U, R> function, final T t, final U u) {
    098        try {
    099            return function.apply(t, u);
    100        } catch (final IOException e) {
    101            throw wrap(e);
    102        }
    103    }
    104
    105    /**
    106     * Applies an IO function with the given arguments.
    107     *
    108     * @param function the function.
    109     * @param <T> the first function argument type.
    110     * @param <R> the return type.
    111     * @param t the first function argument.
    112     * @return the function result.
    113     * @throws UncheckedIOException if an I/O error occurs.
    114     */
    115    public static <T, R> R apply(final IOFunction<T, R> function, final T t) {
    116        try {
    117            return function.apply(t);
    118        } catch (final IOException e) {
    119            throw wrap(e);
    120        }
    121    }
    122
    123    /**
    124     * Applies an IO quad-function with the given arguments.
    125     *
    126     * @param function the function.
    127     * @param <T> the first function argument type.
    128     * @param <U> the second function argument type.
    129     * @param <V> the third function argument type.
    130     * @param <W> the fourth function argument type.
    131     * @param <R> the return type.
    132     * @param t the first function argument.
    133     * @param u the second function argument.
    134     * @param v the third function argument.
    135     * @param w the fourth function argument.
    136     * @return the function result.
    137     * @throws UncheckedIOException if an I/O error occurs.
    138     */
    139    public static <T, U, V, W, R> R apply(final IOQuadFunction<T, U, V, W, R> function, final T t, final U u, final V v, final W w) {
    140        try {
    141            return function.apply(t, u, v, w);
    142        } catch (final IOException e) {
    143            throw wrap(e);
    144        }
    145    }
    146
    147    /**
    148     * Applies an IO tri-function with the given arguments.
    149     *
    150     * @param <T> the first function argument type.
    151     * @param <U> the second function argument type.
    152     * @param <V> the third function argument type.
    153     * @param <R> the return type.
    154     * @param function the function.
    155     * @param t the first function argument.
    156     * @param u the second function argument.
    157     * @param v the third function argument.
    158     * @return the function result.
    159     * @throws UncheckedIOException if an I/O error occurs.
    160     */
    161    public static <T, U, V, R> R apply(final IOTriFunction<T, U, V, R> function, final T t, final U u, final V v) {
    162        try {
    163            return function.apply(t, u, v);
    164        } catch (final IOException e) {
    165            throw wrap(e);
    166        }
    167    }
    168
    169    /**
    170     * Compares the arguments with the comparator.
    171     *
    172     * @param <T> the first function argument type.
    173     * @param comparator the function.
    174     * @param t the first function argument.
    175     * @param u the second function argument.
    176     * @return the comparator result.
    177     * @throws UncheckedIOException if an I/O error occurs.
    178     */
    179    public static <T> int compare(final IOComparator<T> comparator, final T t, final T u) {
    180        try {
    181            return comparator.compare(t, u);
    182        } catch (final IOException e) {
    183            throw wrap(e);
    184        }
    185    }
    186
    187    /**
    188     * Gets the result from an IO supplier.
    189     *
    190     * @param <T> the return type of the operations.
    191     * @param supplier Supplies the return value.
    192     * @return result from the supplier.
    193     * @throws UncheckedIOException if an I/O error occurs.
    194     */
    195    public static <T> T get(final IOSupplier<T> supplier) {
    196        try {
    197            return supplier.get();
    198        } catch (final IOException e) {
    199            throw wrap(e);
    200        }
    201    }
    202
    203    /**
    204     * Gets the result from an IO supplier.
    205     *
    206     * @param <T> the return type of the operations.
    207     * @param supplier Supplies the return value.
    208     * @param message The UncheckedIOException message if an I/O error occurs.
    209     * @return result from the supplier.
    210     * @throws UncheckedIOException if an I/O error occurs.
    211     */
    212    public static <T> T get(final IOSupplier<T> supplier, final Supplier<String> message) {
    213        try {
    214            return supplier.get();
    215        } catch (final IOException e) {
    216            throw wrap(e, message);
    217        }
    218    }
    219
    220    /**
    221     * Gets the result from an IO int supplier.
    222     *
    223     * @param supplier Supplies the return value.
    224     * @return result from the supplier.
    225     * @throws UncheckedIOException if an I/O error occurs.
    226     * @since 2.14.0
    227     */
    228    public static int getAsInt(final IOIntSupplier supplier) {
    229        try {
    230            return supplier.getAsInt();
    231        } catch (final IOException e) {
    232            throw wrap(e);
    233        }
    234    }
    235
    236    /**
    237     * Gets the result from an IO int supplier.
    238     *
    239     * @param supplier Supplies the return value.
    240     * @param message The UncheckedIOException message if an I/O error occurs.
    241     * @return result from the supplier.
    242     * @throws UncheckedIOException if an I/O error occurs.
    243     * @since 2.14.0
    244     */
    245    public static int getAsInt(final IOIntSupplier supplier, final Supplier<String> message) {
    246        try {
    247            return supplier.getAsInt();
    248        } catch (final IOException e) {
    249            throw wrap(e, message);
    250        }
    251    }
    252
    253    /**
    254     * Gets the result from an IO long supplier.
    255     *
    256     * @param supplier Supplies the return value.
    257     * @return result from the supplier.
    258     * @throws UncheckedIOException if an I/O error occurs.
    259     * @since 2.14.0
    260     */
    261    public static long getAsLong(final IOLongSupplier supplier) {
    262        try {
    263            return supplier.getAsLong();
    264        } catch (final IOException e) {
    265            throw wrap(e);
    266        }
    267    }
    268
    269    /**
    270     * Gets the result from an IO long supplier.
    271     *
    272     * @param supplier Supplies the return value.
    273     * @param message The UncheckedIOException message if an I/O error occurs.
    274     * @return result from the supplier.
    275     * @throws UncheckedIOException if an I/O error occurs.
    276     * @since 2.14.0
    277     */
    278    public static long getAsLong(final IOLongSupplier supplier, final Supplier<String> message) {
    279        try {
    280            return supplier.getAsLong();
    281        } catch (final IOException e) {
    282            throw wrap(e, message);
    283        }
    284    }
    285
    286    /**
    287     * Runs an IO runnable.
    288     *
    289     * @param runnable The runnable to run.
    290     * @throws UncheckedIOException if an I/O error occurs.
    291     */
    292    public static void run(final IORunnable runnable) {
    293        try {
    294            runnable.run();
    295        } catch (final IOException e) {
    296            throw wrap(e);
    297        }
    298    }
    299
    300    /**
    301     * Runs an IO runnable.
    302     *
    303     * @param runnable The runnable to run.
    304     * @param message The UncheckedIOException message if an I/O error occurs.
    305     * @throws UncheckedIOException if an I/O error occurs.
    306     * @since 2.14.0
    307     */
    308    public static void run(final IORunnable runnable, final Supplier<String> message) {
    309        try {
    310            runnable.run();
    311        } catch (final IOException e) {
    312            throw wrap(e, message);
    313        }
    314    }
    315
    316    /**
    317     * Tests an IO predicate.
    318     *
    319     * @param <T> the type of the input to the predicate.
    320     * @param predicate the predicate.
    321     * @param t the input to the predicate.
    322     * @return {@code true} if the input argument matches the predicate, otherwise {@code false}.
    323     */
    324    public static <T> boolean test(final IOPredicate<T> predicate, final T t) {
    325        try {
    326            return predicate.test(t);
    327        } catch (final IOException e) {
    328            throw wrap(e);
    329        }
    330    }
    331
    332    /**
    333     * Constructs a new {@link UncheckedIOException} for the given exception.
    334     *
    335     * @param e The exception to wrap.
    336     * @return a new {@link UncheckedIOException}.
    337     */
    338    private static UncheckedIOException wrap(final IOException e) {
    339        return new UncheckedIOException(e);
    340    }
    341
    342    /**
    343     * Constructs a new {@link UncheckedIOException} for the given exception and detail message.
    344     *
    345     * @param e The exception to wrap.
    346     * @param message The UncheckedIOException message if an I/O error occurs.
    347     * @return a new {@link UncheckedIOException}.
    348     */
    349    private static UncheckedIOException wrap(final IOException e, final Supplier<String> message) {
    350        return new UncheckedIOException(message.get(), e);
    351    }
    352
    353    /**
    354     * No instances needed.
    355     */
    356    private Uncheck() {
    357        // no instances needed.
    358    }
    359}