    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 argument.
    067     *
    068     * @param i the input argument.
    069     * @param consumer Consumes the value.
    070     * @throws UncheckedIOException if an I/O error occurs.
    071     * @since 2.18.0
    072     */
    073    public static void accept(final IOIntConsumer consumer, final int i) {
    074        try {
    075            consumer.accept(i);
    076        } catch (final IOException e) {
    077            throw wrap(e);
    078        }
    079    }
    080
    081    /**
    082     * Accepts an IO consumer with the given arguments.
    083     *
    084     * @param <T> the first input type.
    085     * @param <U> the second input type.
    086     * @param <V> the third input type.
    087     * @param t the first input argument.
    088     * @param u the second input argument.
    089     * @param v the third input argument.
    090     * @param consumer Consumes the value.
    091     * @throws UncheckedIOException if an I/O error occurs.
    092     */
    093    public static <T, U, V> void accept(final IOTriConsumer<T, U, V> consumer, final T t, final U u, final V v) {
    094        try {
    095            consumer.accept(t, u, v);
    096        } catch (final IOException e) {
    097            throw wrap(e);
    098        }
    099    }
    100
    101    /**
    102     * Applies an IO function with the given arguments.
    103     *
    104     * @param <T> the first function argument type.
    105     * @param <U> the second function argument type.
    106     * @param <R> the return type.
    107     * @param function the function.
    108     * @param t the first function argument.
    109     * @param u the second function argument.
    110     * @return the function result.
    111     * @throws UncheckedIOException if an I/O error occurs.
    112     */
    113    public static <T, U, R> R apply(final IOBiFunction<T, U, R> function, final T t, final U u) {
    114        try {
    115            return function.apply(t, u);
    116        } catch (final IOException e) {
    117            throw wrap(e);
    118        }
    119    }
    120
    121    /**
    122     * Applies an IO function with the given arguments.
    123     *
    124     * @param function the function.
    125     * @param <T> the first function argument type.
    126     * @param <R> the return type.
    127     * @param t the first function argument.
    128     * @return the function result.
    129     * @throws UncheckedIOException if an I/O error occurs.
    130     */
    131    public static <T, R> R apply(final IOFunction<T, R> function, final T t) {
    132        try {
    133            return function.apply(t);
    134        } catch (final IOException e) {
    135            throw wrap(e);
    136        }
    137    }
    138
    139    /**
    140     * Applies an IO quad-function with the given arguments.
    141     *
    142     * @param function the function.
    143     * @param <T> the first function argument type.
    144     * @param <U> the second function argument type.
    145     * @param <V> the third function argument type.
    146     * @param <W> the fourth function argument type.
    147     * @param <R> the return type.
    148     * @param t the first function argument.
    149     * @param u the second function argument.
    150     * @param v the third function argument.
    151     * @param w the fourth function argument.
    152     * @return the function result.
    153     * @throws UncheckedIOException if an I/O error occurs.
    154     */
    155    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) {
    156        try {
    157            return function.apply(t, u, v, w);
    158        } catch (final IOException e) {
    159            throw wrap(e);
    160        }
    161    }
    162
    163    /**
    164     * Applies an IO tri-function with the given arguments.
    165     *
    166     * @param <T> the first function argument type.
    167     * @param <U> the second function argument type.
    168     * @param <V> the third function argument type.
    169     * @param <R> the return type.
    170     * @param function the function.
    171     * @param t the first function argument.
    172     * @param u the second function argument.
    173     * @param v the third function argument.
    174     * @return the function result.
    175     * @throws UncheckedIOException if an I/O error occurs.
    176     */
    177    public static <T, U, V, R> R apply(final IOTriFunction<T, U, V, R> function, final T t, final U u, final V v) {
    178        try {
    179            return function.apply(t, u, v);
    180        } catch (final IOException e) {
    181            throw wrap(e);
    182        }
    183    }
    184
    185    /**
    186     * Compares the arguments with the comparator.
    187     *
    188     * @param <T> the first function argument type.
    189     * @param comparator the function.
    190     * @param t the first function argument.
    191     * @param u the second function argument.
    192     * @return the comparator result.
    193     * @throws UncheckedIOException if an I/O error occurs.
    194     */
    195    public static <T> int compare(final IOComparator<T> comparator, final T t, final T u) {
    196        try {
    197            return comparator.compare(t, u);
    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     * @return result from the supplier.
    209     * @throws UncheckedIOException if an I/O error occurs.
    210     */
    211    public static <T> T get(final IOSupplier<T> supplier) {
    212        try {
    213            return supplier.get();
    214        } catch (final IOException e) {
    215            throw wrap(e);
    216        }
    217    }
    218
    219    /**
    220     * Gets the result from an IO supplier.
    221     *
    222     * @param <T> the return type of the operations.
    223     * @param supplier Supplies the return value.
    224     * @param message The UncheckedIOException message if an I/O error occurs.
    225     * @return result from the supplier.
    226     * @throws UncheckedIOException if an I/O error occurs.
    227     */
    228    public static <T> T get(final IOSupplier<T> supplier, final Supplier<String> message) {
    229        try {
    230            return supplier.get();
    231        } catch (final IOException e) {
    232            throw wrap(e, message);
    233        }
    234    }
    235
    236    /**
    237     * Gets the result from an IO int supplier.
    238     *
    239     * @param supplier Supplies the return value.
    240     * @return result from the supplier.
    241     * @throws UncheckedIOException if an I/O error occurs.
    242     * @since 2.14.0
    243     */
    244    public static int getAsInt(final IOIntSupplier supplier) {
    245        try {
    246            return supplier.getAsInt();
    247        } catch (final IOException e) {
    248            throw wrap(e);
    249        }
    250    }
    251
    252    /**
    253     * Gets the result from an IO int supplier.
    254     *
    255     * @param supplier Supplies the return value.
    256     * @param message The UncheckedIOException message if an I/O error occurs.
    257     * @return result from the supplier.
    258     * @throws UncheckedIOException if an I/O error occurs.
    259     * @since 2.14.0
    260     */
    261    public static int getAsInt(final IOIntSupplier supplier, final Supplier<String> message) {
    262        try {
    263            return supplier.getAsInt();
    264        } catch (final IOException e) {
    265            throw wrap(e, message);
    266        }
    267    }
    268
    269    /**
    270     * Gets the result from an IO long supplier.
    271     *
    272     * @param supplier Supplies the return value.
    273     * @return result from the supplier.
    274     * @throws UncheckedIOException if an I/O error occurs.
    275     * @since 2.14.0
    276     */
    277    public static long getAsLong(final IOLongSupplier supplier) {
    278        try {
    279            return supplier.getAsLong();
    280        } catch (final IOException e) {
    281            throw wrap(e);
    282        }
    283    }
    284
    285    /**
    286     * Gets the result from an IO long supplier.
    287     *
    288     * @param supplier Supplies the return value.
    289     * @param message The UncheckedIOException message if an I/O error occurs.
    290     * @return result from the supplier.
    291     * @throws UncheckedIOException if an I/O error occurs.
    292     * @since 2.14.0
    293     */
    294    public static long getAsLong(final IOLongSupplier supplier, final Supplier<String> message) {
    295        try {
    296            return supplier.getAsLong();
    297        } catch (final IOException e) {
    298            throw wrap(e, message);
    299        }
    300    }
    301
    302    /**
    303     * Runs an IO runnable.
    304     *
    305     * @param runnable The runnable to run.
    306     * @throws UncheckedIOException if an I/O error occurs.
    307     */
    308    public static void run(final IORunnable runnable) {
    309        try {
    310            runnable.run();
    311        } catch (final IOException e) {
    312            throw wrap(e);
    313        }
    314    }
    315
    316    /**
    317     * Runs an IO runnable.
    318     *
    319     * @param runnable The runnable to run.
    320     * @param message The UncheckedIOException message if an I/O error occurs.
    321     * @throws UncheckedIOException if an I/O error occurs.
    322     * @since 2.14.0
    323     */
    324    public static void run(final IORunnable runnable, final Supplier<String> message) {
    325        try {
    326            runnable.run();
    327        } catch (final IOException e) {
    328            throw wrap(e, message);
    329        }
    330    }
    331
    332    /**
    333     * Tests an IO predicate.
    334     *
    335     * @param <T> the type of the input to the predicate.
    336     * @param predicate the predicate.
    337     * @param t the input to the predicate.
    338     * @return {@code true} if the input argument matches the predicate, otherwise {@code false}.
    339     */
    340    public static <T> boolean test(final IOPredicate<T> predicate, final T t) {
    341        try {
    342            return predicate.test(t);
    343        } catch (final IOException e) {
    344            throw wrap(e);
    345        }
    346    }
    347
    348    /**
    349     * Constructs a new {@link UncheckedIOException} for the given exception.
    350     *
    351     * @param e The exception to wrap.
    352     * @return a new {@link UncheckedIOException}.
    353     */
    354    private static UncheckedIOException wrap(final IOException e) {
    355        return new UncheckedIOException(e);
    356    }
    357
    358    /**
    359     * Constructs a new {@link UncheckedIOException} for the given exception and detail message.
    360     *
    361     * @param e The exception to wrap.
    362     * @param message The UncheckedIOException message if an I/O error occurs.
    363     * @return a new {@link UncheckedIOException}.
    364     */
    365    private static UncheckedIOException wrap(final IOException e, final Supplier<String> message) {
    366        return new UncheckedIOException(message.get(), e);
    367    }
    368
    369    /**
    370     * No instances needed.
    371     */
    372    private Uncheck() {
    373        // no instances needed.
    374    }
    375}