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  
18  package org.apache.commons.io.function;
19  
20  import java.io.IOException;
21  import java.io.UncheckedIOException;
22  import java.util.function.Supplier;
23  
24  /**
25   * Unchecks calls by throwing {@link UncheckedIOException} instead of {@link IOException}.
26   *
27   * @since 2.12.0
28   */
29  public final class Uncheck {
30  
31      /**
32       * Accepts an IO consumer with the given arguments.
33       *
34       * @param <T> the first input type.
35       * @param <U> the second input type.
36       * @param t the first input argument.
37       * @param u the second input argument.
38       * @param consumer Consumes the value.
39       * @throws UncheckedIOException if an I/O error occurs.
40       */
41      public static <T, U> void accept(final IOBiConsumer<T, U> consumer, final T t, final U u) {
42          try {
43              consumer.accept(t, u);
44          } catch (final IOException e) {
45              throw wrap(e);
46          }
47      }
48  
49      /**
50       * Accepts an IO consumer with the given argument.
51       *
52       * @param <T> the input type.
53       * @param t the input argument.
54       * @param consumer Consumes the value.
55       * @throws UncheckedIOException if an I/O error occurs.
56       */
57      public static <T> void accept(final IOConsumer<T> consumer, final T t) {
58          try {
59              consumer.accept(t);
60          } catch (final IOException e) {
61              throw wrap(e);
62          }
63      }
64  
65      /**
66       * Accepts an IO consumer with the given arguments.
67       *
68       * @param <T> the first input type.
69       * @param <U> the second input type.
70       * @param <V> the third input type.
71       * @param t the first input argument.
72       * @param u the second input argument.
73       * @param v the third input argument.
74       * @param consumer Consumes the value.
75       * @throws UncheckedIOException if an I/O error occurs.
76       */
77      public static <T, U, V> void accept(final IOTriConsumer<T, U, V> consumer, final T t, final U u, final V v) {
78          try {
79              consumer.accept(t, u, v);
80          } catch (final IOException e) {
81              throw wrap(e);
82          }
83      }
84  
85      /**
86       * Applies an IO function with the given arguments.
87       *
88       * @param <T> the first function argument type.
89       * @param <U> the second function argument type.
90       * @param <R> the return type.
91       * @param function the function.
92       * @param t the first function argument.
93       * @param u the second function argument.
94       * @return the function result.
95       * @throws UncheckedIOException if an I/O error occurs.
96       */
97      public static <T, U, R> R apply(final IOBiFunction<T, U, R> function, final T t, final U u) {
98          try {
99              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 }