001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.io.function;
019
020import java.io.IOException;
021import java.io.UncheckedIOException;
022import java.util.function.Supplier;
023
024/**
025 * Unchecks calls by throwing {@link UncheckedIOException} instead of {@link IOException}.
026 *
027 * @since 2.12.0
028 */
029public final class Uncheck {
030
031    /**
032     * Accepts an IO consumer with the given arguments.
033     *
034     * @param <T> the first input type.
035     * @param <U> the second input type.
036     * @param t the first input argument.
037     * @param u the second input argument.
038     * @param consumer Consumes the value.
039     * @throws UncheckedIOException if an I/O error occurs.
040     */
041    public static <T, U> void accept(final IOBiConsumer<T, U> consumer, final T t, final U u) {
042        try {
043            consumer.accept(t, u);
044        } catch (final IOException e) {
045            throw wrap(e);
046        }
047    }
048
049    /**
050     * Accepts an IO consumer with the given argument.
051     *
052     * @param <T> the input type.
053     * @param t the input argument.
054     * @param consumer Consumes the value.
055     * @throws UncheckedIOException if an I/O error occurs.
056     */
057    public static <T> void accept(final IOConsumer<T> consumer, final T t) {
058        try {
059            consumer.accept(t);
060        } catch (final IOException e) {
061            throw wrap(e);
062        }
063    }
064
065    /**
066     * Accepts an IO consumer with the given argument.
067     *
068     * @param i the input argument.
069     * @param consumer Consumes the value.
070     * @throws UncheckedIOException if an I/O error occurs.
071     * @since 2.18.0
072     */
073    public static void accept(final IOIntConsumer consumer, final int i) {
074        try {
075            consumer.accept(i);
076        } catch (final IOException e) {
077            throw wrap(e);
078        }
079    }
080
081    /**
082     * Accepts an IO consumer with the given arguments.
083     *
084     * @param <T> the first input type.
085     * @param <U> the second input type.
086     * @param <V> the third input type.
087     * @param t the first input argument.
088     * @param u the second input argument.
089     * @param v the third input argument.
090     * @param consumer Consumes the value.
091     * @throws UncheckedIOException if an I/O error occurs.
092     */
093    public static <T, U, V> void accept(final IOTriConsumer<T, U, V> consumer, final T t, final U u, final V v) {
094        try {
095            consumer.accept(t, u, v);
096        } catch (final IOException e) {
097            throw wrap(e);
098        }
099    }
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}