View Javadoc
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  
19  import java.io.IOException;
20  
21  /**
22   * Erases {@link IOException} for the compiler but still throws that exception at runtime.
23   *
24   * @since 2.16.0
25   */
26  public final class Erase {
27  
28      /**
29       * Delegates to the given {@link IOBiConsumer} but erases its {@link IOException} for the compiler, while still throwing
30       * the exception at runtime.
31       *
32       * @param <T> See delegate.
33       * @param <U> See delegate.
34       * @param consumer See delegate.
35       * @param t See delegate.
36       * @param u See delegate.
37       * @see IOBiConsumer
38       */
39      static <T, U> void accept(final IOBiConsumer<T, U> consumer, final T t, final U u) {
40          try {
41              consumer.accept(t, u);
42          } catch (final IOException ex) {
43              rethrow(ex); // throws IOException
44          }
45      }
46  
47      /**
48       * Delegates to the given {@link IOConsumer} but erases its {@link IOException} for the compiler, while still throwing
49       * the exception at runtime.
50       *
51       * @param <T> See delegate.
52       * @param consumer See delegate.
53       * @param t See delegate.
54       * @see IOConsumer
55       */
56      static <T> void accept(final IOConsumer<T> consumer, final T t) {
57          try {
58              consumer.accept(t);
59          } catch (final IOException ex) {
60              rethrow(ex); // throws IOException
61          }
62      }
63  
64      /**
65       * Delegates to the given {@link IOBiFunction} but erases its {@link IOException} for the compiler, while still throwing
66       * the exception at runtime.
67       *
68       * @param <T> See delegate.
69       * @param <U> See delegate.
70       * @param <R> See delegate.
71       * @param mapper See delegate.
72       * @param t See delegate.
73       * @param u See delegate.
74       * @return See delegate.
75       * @see IOBiFunction
76       */
77      static <T, U, R> R apply(final IOBiFunction<? super T, ? super U, ? extends R> mapper, final T t, final U u) {
78          try {
79              return mapper.apply(t, u);
80          } catch (final IOException e) {
81              throw rethrow(e); // throws IOException
82          }
83      }
84  
85      /**
86       * Delegates to the given {@link IOFunction} but erases its {@link IOException} for the compiler, while still throwing
87       * the exception at runtime.
88       *
89       * @param <T> See delegate.
90       * @param <R> See delegate.
91       * @param mapper See delegate.
92       * @param t See delegate.
93       * @return See delegate.
94       * @see IOFunction
95       */
96      static <T, R> R apply(final IOFunction<? super T, ? extends R> mapper, final T t) {
97          try {
98              return mapper.apply(t);
99          } catch (final IOException e) {
100             throw rethrow(e); // throws IOException
101         }
102     }
103 
104     /**
105      * Delegates to the given {@link IOComparator} but erases its {@link IOException} for the compiler, while still throwing
106      * the exception at runtime.
107      *
108      * @param <T> See delegate.
109      * @param comparator See delegate.
110      * @param t See delegate.
111      * @param u See delegate.
112      * @return See delegate.
113      * @see IOComparator
114      */
115     static <T> int compare(final IOComparator<? super T> comparator, final T t, final T u) {
116         try {
117             return comparator.compare(t, u);
118         } catch (final IOException e) {
119             throw rethrow(e); // throws IOException
120         }
121     }
122 
123     /**
124      * Delegates to the given {@link IOSupplier} but erases its {@link IOException} for the compiler, while still throwing
125      * the exception at runtime.
126      *
127      * @param <T> See delegate.
128      * @param supplier See delegate.
129      * @return See delegate.
130      * @see IOSupplier
131      */
132     static <T> T get(final IOSupplier<T> supplier) {
133         try {
134             return supplier.get();
135         } catch (final IOException e) {
136             throw rethrow(e); // throws IOException
137         }
138     }
139 
140     /**
141      * Throws the given throwable.
142      *
143      * @param <T> The throwable cast type.
144      * @param throwable The throwable to rethrow.
145      * @return nothing because we throw.
146      * @throws T Always thrown.
147      */
148     @SuppressWarnings("unchecked")
149     public static <T extends Throwable> RuntimeException rethrow(final Throwable throwable) throws T {
150         throw (T) throwable;
151     }
152 
153     /**
154      * Delegates to the given {@link IORunnable} but erases its {@link IOException} for the compiler, while still throwing
155      * the exception at runtime.
156      *
157      * @param runnable See delegate.
158      * @see IORunnable
159      */
160     static void run(final IORunnable runnable) {
161         try {
162             runnable.run();
163         } catch (final IOException e) {
164             throw rethrow(e); // throws IOException
165         }
166     }
167 
168     /**
169      * Delegates to the given {@link IOPredicate} but erases its {@link IOException} for the compiler, while still throwing
170      * the exception at runtime.
171      *
172      * @param <T> See delegate.
173      * @param predicate See delegate.
174      * @param t See delegate.
175      * @return See delegate.
176      * @see IOPredicate
177      */
178     static <T> boolean test(final IOPredicate<? super T> predicate, final T t) {
179         try {
180             return predicate.test(t);
181         } catch (final IOException e) {
182             throw rethrow(e); // throws IOException
183         }
184     }
185 
186     /** No instances. */
187     private Erase() {
188         // No instances.
189     }
190 
191 }