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 * https://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 argument.
67 *
68 * @param i the input argument.
69 * @param consumer Consumes the value.
70 * @throws UncheckedIOException if an I/O error occurs.
71 * @since 2.18.0
72 */
73 public static void accept(final IOIntConsumer consumer, final int i) {
74 try {
75 consumer.accept(i);
76 } catch (final IOException e) {
77 throw wrap(e);
78 }
79 }
80
81 /**
82 * Accepts an IO consumer with the given arguments.
83 *
84 * @param <T> the first input type.
85 * @param <U> the second input type.
86 * @param <V> the third input type.
87 * @param t the first input argument.
88 * @param u the second input argument.
89 * @param v the third input argument.
90 * @param consumer Consumes the value.
91 * @throws UncheckedIOException if an I/O error occurs.
92 */
93 public static <T, U, V> void accept(final IOTriConsumer<T, U, V> consumer, final T t, final U u, final V v) {
94 try {
95 consumer.accept(t, u, v);
96 } catch (final IOException e) {
97 throw wrap(e);
98 }
99 }
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 boolean 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.19.0
243 */
244 public static boolean getAsBoolean(final IOBooleanSupplier supplier) {
245 try {
246 return supplier.getAsBoolean();
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 * @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) {
261 try {
262 return supplier.getAsInt();
263 } catch (final IOException e) {
264 throw wrap(e);
265 }
266 }
267
268 /**
269 * Gets the result from an IO int supplier.
270 *
271 * @param supplier Supplies the return value.
272 * @param message The UncheckedIOException message if an I/O error occurs.
273 * @return result from the supplier.
274 * @throws UncheckedIOException if an I/O error occurs.
275 * @since 2.14.0
276 */
277 public static int getAsInt(final IOIntSupplier supplier, final Supplier<String> message) {
278 try {
279 return supplier.getAsInt();
280 } catch (final IOException e) {
281 throw wrap(e, message);
282 }
283 }
284
285 /**
286 * Gets the result from an IO long supplier.
287 *
288 * @param supplier Supplies the return value.
289 * @return result from the supplier.
290 * @throws UncheckedIOException if an I/O error occurs.
291 * @since 2.14.0
292 */
293 public static long getAsLong(final IOLongSupplier supplier) {
294 try {
295 return supplier.getAsLong();
296 } catch (final IOException e) {
297 throw wrap(e);
298 }
299 }
300
301 /**
302 * Gets the result from an IO long supplier.
303 *
304 * @param supplier Supplies the return value.
305 * @param message The UncheckedIOException message if an I/O error occurs.
306 * @return result from the supplier.
307 * @throws UncheckedIOException if an I/O error occurs.
308 * @since 2.14.0
309 */
310 public static long getAsLong(final IOLongSupplier supplier, final Supplier<String> message) {
311 try {
312 return supplier.getAsLong();
313 } catch (final IOException e) {
314 throw wrap(e, message);
315 }
316 }
317
318 /**
319 * Runs an IO runnable.
320 *
321 * @param runnable The runnable to run.
322 * @throws UncheckedIOException if an I/O error occurs.
323 */
324 public static void run(final IORunnable runnable) {
325 try {
326 runnable.run();
327 } catch (final IOException e) {
328 throw wrap(e);
329 }
330 }
331
332 /**
333 * Runs an IO runnable.
334 *
335 * @param runnable The runnable to run.
336 * @param message The UncheckedIOException message if an I/O error occurs.
337 * @throws UncheckedIOException if an I/O error occurs.
338 * @since 2.14.0
339 */
340 public static void run(final IORunnable runnable, final Supplier<String> message) {
341 try {
342 runnable.run();
343 } catch (final IOException e) {
344 throw wrap(e, message);
345 }
346 }
347
348 /**
349 * Tests an IO predicate.
350 *
351 * @param <T> the type of the input to the predicate.
352 * @param predicate the predicate.
353 * @param t the input to the predicate.
354 * @return {@code true} if the input argument matches the predicate, otherwise {@code false}.
355 */
356 public static <T> boolean test(final IOPredicate<T> predicate, final T t) {
357 try {
358 return predicate.test(t);
359 } catch (final IOException e) {
360 throw wrap(e);
361 }
362 }
363
364 /**
365 * Constructs a new {@link UncheckedIOException} for the given exception.
366 *
367 * @param e The exception to wrap.
368 * @return a new {@link UncheckedIOException}.
369 */
370 private static UncheckedIOException wrap(final IOException e) {
371 return new UncheckedIOException(e);
372 }
373
374 /**
375 * Constructs a new {@link UncheckedIOException} for the given exception and detail message.
376 *
377 * @param e The exception to wrap.
378 * @param message The UncheckedIOException message if an I/O error occurs.
379 * @return a new {@link UncheckedIOException}.
380 */
381 private static UncheckedIOException wrap(final IOException e, final Supplier<String> message) {
382 return new UncheckedIOException(message.get(), e);
383 }
384
385 /**
386 * No instances needed.
387 */
388 private Uncheck() {
389 // no instances needed.
390 }
391 }