Erase.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. /**
  20.  * Erases {@link IOException} for the compiler but still throws that exception at runtime.
  21.  *
  22.  * @since 2.16.0
  23.  */
  24. public final class Erase {

  25.     /**
  26.      * Delegates to the given {@link IOBiConsumer} but erases its {@link IOException} for the compiler, while still throwing
  27.      * the exception at runtime.
  28.      *
  29.      * @param <T> See delegate.
  30.      * @param <U> See delegate.
  31.      * @param consumer See delegate.
  32.      * @param t See delegate.
  33.      * @param u See delegate.
  34.      * @see IOBiConsumer
  35.      */
  36.     static <T, U> void accept(final IOBiConsumer<T, U> consumer, final T t, final U u) {
  37.         try {
  38.             consumer.accept(t, u);
  39.         } catch (final IOException ex) {
  40.             rethrow(ex); // throws IOException
  41.         }
  42.     }

  43.     /**
  44.      * Delegates to the given {@link IOConsumer} but erases its {@link IOException} for the compiler, while still throwing
  45.      * the exception at runtime.
  46.      *
  47.      * @param <T> See delegate.
  48.      * @param consumer See delegate.
  49.      * @param t See delegate.
  50.      * @see IOConsumer
  51.      */
  52.     static <T> void accept(final IOConsumer<T> consumer, final T t) {
  53.         try {
  54.             consumer.accept(t);
  55.         } catch (final IOException ex) {
  56.             rethrow(ex); // throws IOException
  57.         }
  58.     }

  59.     /**
  60.      * Delegates to the given {@link IOBiFunction} but erases its {@link IOException} for the compiler, while still throwing
  61.      * the exception at runtime.
  62.      *
  63.      * @param <T> See delegate.
  64.      * @param <U> See delegate.
  65.      * @param <R> See delegate.
  66.      * @param mapper See delegate.
  67.      * @param t See delegate.
  68.      * @param u See delegate.
  69.      * @return See delegate.
  70.      * @see IOBiFunction
  71.      */
  72.     static <T, U, R> R apply(final IOBiFunction<? super T, ? super U, ? extends R> mapper, final T t, final U u) {
  73.         try {
  74.             return mapper.apply(t, u);
  75.         } catch (final IOException e) {
  76.             throw rethrow(e); // throws IOException
  77.         }
  78.     }

  79.     /**
  80.      * Delegates to the given {@link IOFunction} but erases its {@link IOException} for the compiler, while still throwing
  81.      * the exception at runtime.
  82.      *
  83.      * @param <T> See delegate.
  84.      * @param <R> See delegate.
  85.      * @param mapper See delegate.
  86.      * @param t See delegate.
  87.      * @return See delegate.
  88.      * @see IOFunction
  89.      */
  90.     static <T, R> R apply(final IOFunction<? super T, ? extends R> mapper, final T t) {
  91.         try {
  92.             return mapper.apply(t);
  93.         } catch (final IOException e) {
  94.             throw rethrow(e); // throws IOException
  95.         }
  96.     }

  97.     /**
  98.      * Delegates to the given {@link IOComparator} but erases its {@link IOException} for the compiler, while still throwing
  99.      * the exception at runtime.
  100.      *
  101.      * @param <T> See delegate.
  102.      * @param comparator See delegate.
  103.      * @param t See delegate.
  104.      * @param u See delegate.
  105.      * @return See delegate.
  106.      * @see IOComparator
  107.      */
  108.     static <T> int compare(final IOComparator<? super T> comparator, final T t, final T u) {
  109.         try {
  110.             return comparator.compare(t, u);
  111.         } catch (final IOException e) {
  112.             throw rethrow(e); // throws IOException
  113.         }
  114.     }

  115.     /**
  116.      * Delegates to the given {@link IOSupplier} but erases its {@link IOException} for the compiler, while still throwing
  117.      * the exception at runtime.
  118.      *
  119.      * @param <T> See delegate.
  120.      * @param supplier See delegate.
  121.      * @return See delegate.
  122.      * @see IOSupplier
  123.      */
  124.     static <T> T get(final IOSupplier<T> supplier) {
  125.         try {
  126.             return supplier.get();
  127.         } catch (final IOException e) {
  128.             throw rethrow(e); // throws IOException
  129.         }
  130.     }

  131.     /**
  132.      * Throws the given throwable.
  133.      *
  134.      * @param <T> The throwable cast type.
  135.      * @param throwable The throwable to rethrow.
  136.      * @return nothing because we throw.
  137.      * @throws T Always thrown.
  138.      */
  139.     @SuppressWarnings("unchecked")
  140.     public static <T extends Throwable> RuntimeException rethrow(final Throwable throwable) throws T {
  141.         throw (T) throwable;
  142.     }

  143.     /**
  144.      * Delegates to the given {@link IORunnable} but erases its {@link IOException} for the compiler, while still throwing
  145.      * the exception at runtime.
  146.      *
  147.      * @param runnable See delegate.
  148.      * @see IORunnable
  149.      */
  150.     static void run(final IORunnable runnable) {
  151.         try {
  152.             runnable.run();
  153.         } catch (final IOException e) {
  154.             throw rethrow(e); // throws IOException
  155.         }
  156.     }

  157.     /**
  158.      * Delegates to the given {@link IOPredicate} but erases its {@link IOException} for the compiler, while still throwing
  159.      * the exception at runtime.
  160.      *
  161.      * @param <T> See delegate.
  162.      * @param predicate See delegate.
  163.      * @param t See delegate.
  164.      * @return See delegate.
  165.      * @see IOPredicate
  166.      */
  167.     static <T> boolean test(final IOPredicate<? super T> predicate, final T t) {
  168.         try {
  169.             return predicate.test(t);
  170.         } catch (final IOException e) {
  171.             throw rethrow(e); // throws IOException
  172.         }
  173.     }

  174.     /** No instances. */
  175.     private Erase() {
  176.         // No instances.
  177.     }

  178. }