Uncheck.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.io.function;

  18. import java.io.IOException;
  19. import java.io.UncheckedIOException;
  20. import java.util.function.Supplier;

  21. /**
  22.  * Unchecks calls by throwing {@link UncheckedIOException} instead of {@link IOException}.
  23.  *
  24.  * @since 2.12.0
  25.  */
  26. public final class Uncheck {

  27.     /**
  28.      * Accepts an IO consumer with the given arguments.
  29.      *
  30.      * @param <T> the first input type.
  31.      * @param <U> the second input type.
  32.      * @param t the first input argument.
  33.      * @param u the second input argument.
  34.      * @param consumer Consumes the value.
  35.      * @throws UncheckedIOException if an I/O error occurs.
  36.      */
  37.     public static <T, U> void accept(final IOBiConsumer<T, U> consumer, final T t, final U u) {
  38.         try {
  39.             consumer.accept(t, u);
  40.         } catch (final IOException e) {
  41.             throw wrap(e);
  42.         }
  43.     }

  44.     /**
  45.      * Accepts an IO consumer with the given argument.
  46.      *
  47.      * @param <T> the input type.
  48.      * @param t the input argument.
  49.      * @param consumer Consumes the value.
  50.      * @throws UncheckedIOException if an I/O error occurs.
  51.      */
  52.     public static <T> void accept(final IOConsumer<T> consumer, final T t) {
  53.         try {
  54.             consumer.accept(t);
  55.         } catch (final IOException e) {
  56.             throw wrap(e);
  57.         }
  58.     }

  59.     /**
  60.      * Accepts an IO consumer with the given argument.
  61.      *
  62.      * @param i the input argument.
  63.      * @param consumer Consumes the value.
  64.      * @throws UncheckedIOException if an I/O error occurs.
  65.      * @since 2.18.0
  66.      */
  67.     public static void accept(final IOIntConsumer consumer, final int i) {
  68.         try {
  69.             consumer.accept(i);
  70.         } catch (final IOException e) {
  71.             throw wrap(e);
  72.         }
  73.     }

  74.     /**
  75.      * Accepts an IO consumer with the given arguments.
  76.      *
  77.      * @param <T> the first input type.
  78.      * @param <U> the second input type.
  79.      * @param <V> the third input type.
  80.      * @param t the first input argument.
  81.      * @param u the second input argument.
  82.      * @param v the third input argument.
  83.      * @param consumer Consumes the value.
  84.      * @throws UncheckedIOException if an I/O error occurs.
  85.      */
  86.     public static <T, U, V> void accept(final IOTriConsumer<T, U, V> consumer, final T t, final U u, final V v) {
  87.         try {
  88.             consumer.accept(t, u, v);
  89.         } catch (final IOException e) {
  90.             throw wrap(e);
  91.         }
  92.     }

  93.     /**
  94.      * Applies an IO function with the given arguments.
  95.      *
  96.      * @param <T> the first function argument type.
  97.      * @param <U> the second function argument type.
  98.      * @param <R> the return type.
  99.      * @param function the function.
  100.      * @param t the first function argument.
  101.      * @param u the second function argument.
  102.      * @return the function result.
  103.      * @throws UncheckedIOException if an I/O error occurs.
  104.      */
  105.     public static <T, U, R> R apply(final IOBiFunction<T, U, R> function, final T t, final U u) {
  106.         try {
  107.             return function.apply(t, u);
  108.         } catch (final IOException e) {
  109.             throw wrap(e);
  110.         }
  111.     }

  112.     /**
  113.      * Applies an IO function with the given arguments.
  114.      *
  115.      * @param function the function.
  116.      * @param <T> the first function argument type.
  117.      * @param <R> the return type.
  118.      * @param t the first function argument.
  119.      * @return the function result.
  120.      * @throws UncheckedIOException if an I/O error occurs.
  121.      */
  122.     public static <T, R> R apply(final IOFunction<T, R> function, final T t) {
  123.         try {
  124.             return function.apply(t);
  125.         } catch (final IOException e) {
  126.             throw wrap(e);
  127.         }
  128.     }

  129.     /**
  130.      * Applies an IO quad-function with the given arguments.
  131.      *
  132.      * @param function the function.
  133.      * @param <T> the first function argument type.
  134.      * @param <U> the second function argument type.
  135.      * @param <V> the third function argument type.
  136.      * @param <W> the fourth function argument type.
  137.      * @param <R> the return type.
  138.      * @param t the first function argument.
  139.      * @param u the second function argument.
  140.      * @param v the third function argument.
  141.      * @param w the fourth function argument.
  142.      * @return the function result.
  143.      * @throws UncheckedIOException if an I/O error occurs.
  144.      */
  145.     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) {
  146.         try {
  147.             return function.apply(t, u, v, w);
  148.         } catch (final IOException e) {
  149.             throw wrap(e);
  150.         }
  151.     }

  152.     /**
  153.      * Applies an IO tri-function with the given arguments.
  154.      *
  155.      * @param <T> the first function argument type.
  156.      * @param <U> the second function argument type.
  157.      * @param <V> the third function argument type.
  158.      * @param <R> the return type.
  159.      * @param function the function.
  160.      * @param t the first function argument.
  161.      * @param u the second function argument.
  162.      * @param v the third function argument.
  163.      * @return the function result.
  164.      * @throws UncheckedIOException if an I/O error occurs.
  165.      */
  166.     public static <T, U, V, R> R apply(final IOTriFunction<T, U, V, R> function, final T t, final U u, final V v) {
  167.         try {
  168.             return function.apply(t, u, v);
  169.         } catch (final IOException e) {
  170.             throw wrap(e);
  171.         }
  172.     }

  173.     /**
  174.      * Compares the arguments with the comparator.
  175.      *
  176.      * @param <T> the first function argument type.
  177.      * @param comparator the function.
  178.      * @param t the first function argument.
  179.      * @param u the second function argument.
  180.      * @return the comparator result.
  181.      * @throws UncheckedIOException if an I/O error occurs.
  182.      */
  183.     public static <T> int compare(final IOComparator<T> comparator, final T t, final T u) {
  184.         try {
  185.             return comparator.compare(t, u);
  186.         } catch (final IOException e) {
  187.             throw wrap(e);
  188.         }
  189.     }

  190.     /**
  191.      * Gets the result from an IO supplier.
  192.      *
  193.      * @param <T> the return type of the operations.
  194.      * @param supplier Supplies the return value.
  195.      * @return result from the supplier.
  196.      * @throws UncheckedIOException if an I/O error occurs.
  197.      */
  198.     public static <T> T get(final IOSupplier<T> supplier) {
  199.         try {
  200.             return supplier.get();
  201.         } catch (final IOException e) {
  202.             throw wrap(e);
  203.         }
  204.     }

  205.     /**
  206.      * Gets the result from an IO supplier.
  207.      *
  208.      * @param <T> the return type of the operations.
  209.      * @param supplier Supplies the return value.
  210.      * @param message The UncheckedIOException message if an I/O error occurs.
  211.      * @return result from the supplier.
  212.      * @throws UncheckedIOException if an I/O error occurs.
  213.      */
  214.     public static <T> T get(final IOSupplier<T> supplier, final Supplier<String> message) {
  215.         try {
  216.             return supplier.get();
  217.         } catch (final IOException e) {
  218.             throw wrap(e, message);
  219.         }
  220.     }

  221.     /**
  222.      * Gets the result from an IO boolean supplier.
  223.      *
  224.      * @param supplier Supplies the return value.
  225.      * @return result from the supplier.
  226.      * @throws UncheckedIOException if an I/O error occurs.
  227.      * @since 2.19.0
  228.      */
  229.     public static boolean getAsBoolean(final IOBooleanSupplier supplier) {
  230.         try {
  231.             return supplier.getAsBoolean();
  232.         } catch (final IOException e) {
  233.             throw wrap(e);
  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.      * Gets the result from an IO int supplier.
  253.      *
  254.      * @param supplier Supplies the return value.
  255.      * @param message The UncheckedIOException message if an I/O error occurs.
  256.      * @return result from the supplier.
  257.      * @throws UncheckedIOException if an I/O error occurs.
  258.      * @since 2.14.0
  259.      */
  260.     public static int getAsInt(final IOIntSupplier supplier, final Supplier<String> message) {
  261.         try {
  262.             return supplier.getAsInt();
  263.         } catch (final IOException e) {
  264.             throw wrap(e, message);
  265.         }
  266.     }

  267.     /**
  268.      * Gets the result from an IO long supplier.
  269.      *
  270.      * @param supplier Supplies the return value.
  271.      * @return result from the supplier.
  272.      * @throws UncheckedIOException if an I/O error occurs.
  273.      * @since 2.14.0
  274.      */
  275.     public static long getAsLong(final IOLongSupplier supplier) {
  276.         try {
  277.             return supplier.getAsLong();
  278.         } catch (final IOException e) {
  279.             throw wrap(e);
  280.         }
  281.     }

  282.     /**
  283.      * Gets the result from an IO long supplier.
  284.      *
  285.      * @param supplier Supplies the return value.
  286.      * @param message The UncheckedIOException message if an I/O error occurs.
  287.      * @return result from the supplier.
  288.      * @throws UncheckedIOException if an I/O error occurs.
  289.      * @since 2.14.0
  290.      */
  291.     public static long getAsLong(final IOLongSupplier supplier, final Supplier<String> message) {
  292.         try {
  293.             return supplier.getAsLong();
  294.         } catch (final IOException e) {
  295.             throw wrap(e, message);
  296.         }
  297.     }

  298.     /**
  299.      * Runs an IO runnable.
  300.      *
  301.      * @param runnable The runnable to run.
  302.      * @throws UncheckedIOException if an I/O error occurs.
  303.      */
  304.     public static void run(final IORunnable runnable) {
  305.         try {
  306.             runnable.run();
  307.         } catch (final IOException e) {
  308.             throw wrap(e);
  309.         }
  310.     }

  311.     /**
  312.      * Runs an IO runnable.
  313.      *
  314.      * @param runnable The runnable to run.
  315.      * @param message The UncheckedIOException message if an I/O error occurs.
  316.      * @throws UncheckedIOException if an I/O error occurs.
  317.      * @since 2.14.0
  318.      */
  319.     public static void run(final IORunnable runnable, final Supplier<String> message) {
  320.         try {
  321.             runnable.run();
  322.         } catch (final IOException e) {
  323.             throw wrap(e, message);
  324.         }
  325.     }

  326.     /**
  327.      * Tests an IO predicate.
  328.      *
  329.      * @param <T> the type of the input to the predicate.
  330.      * @param predicate the predicate.
  331.      * @param t the input to the predicate.
  332.      * @return {@code true} if the input argument matches the predicate, otherwise {@code false}.
  333.      */
  334.     public static <T> boolean test(final IOPredicate<T> predicate, final T t) {
  335.         try {
  336.             return predicate.test(t);
  337.         } catch (final IOException e) {
  338.             throw wrap(e);
  339.         }
  340.     }

  341.     /**
  342.      * Constructs a new {@link UncheckedIOException} for the given exception.
  343.      *
  344.      * @param e The exception to wrap.
  345.      * @return a new {@link UncheckedIOException}.
  346.      */
  347.     private static UncheckedIOException wrap(final IOException e) {
  348.         return new UncheckedIOException(e);
  349.     }

  350.     /**
  351.      * Constructs a new {@link UncheckedIOException} for the given exception and detail message.
  352.      *
  353.      * @param e The exception to wrap.
  354.      * @param message The UncheckedIOException message if an I/O error occurs.
  355.      * @return a new {@link UncheckedIOException}.
  356.      */
  357.     private static UncheckedIOException wrap(final IOException e, final Supplier<String> message) {
  358.         return new UncheckedIOException(message.get(), e);
  359.     }

  360.     /**
  361.      * No instances needed.
  362.      */
  363.     private Uncheck() {
  364.         // no instances needed.
  365.     }
  366. }