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; 022 023/** 024 * Unchecks calls by throwing {@link UncheckedIOException} instead of {@link IOException}. 025 * 026 * @since 2.12.0 027 */ 028public final class Uncheck { 029 030 /** 031 * Accepts an IO consumer with the given arguments. 032 * 033 * @param <T> the first input type. 034 * @param <U> the second input type. 035 * @param t the first input argument. 036 * @param u the second input argument. 037 * @param consumer Consumes the value. 038 * @throws UncheckedIOException if an I/O error occurs. 039 */ 040 public static <T, U> void accept(final IOBiConsumer<T, U> consumer, final T t, final U u) { 041 try { 042 consumer.accept(t, u); 043 } catch (final IOException e) { 044 throw wrap(e); 045 } 046 } 047 048 /** 049 * Accepts an IO consumer with the given argument. 050 * 051 * @param <T> the input type. 052 * @param t the input argument. 053 * @param consumer Consumes the value. 054 * @throws UncheckedIOException if an I/O error occurs. 055 */ 056 public static <T> void accept(final IOConsumer<T> consumer, final T t) { 057 try { 058 consumer.accept(t); 059 } catch (final IOException e) { 060 throw wrap(e); 061 } 062 } 063 064 /** 065 * Accepts an IO consumer with the given arguments. 066 * 067 * @param <T> the first input type. 068 * @param <U> the second input type. 069 * @param <V> the third input type. 070 * @param t the first input argument. 071 * @param u the second input argument. 072 * @param v the third input argument. 073 * @param consumer Consumes the value. 074 * @throws UncheckedIOException if an I/O error occurs. 075 */ 076 public static <T, U, V> void accept(final IOTriConsumer<T, U, V> consumer, final T t, final U u, final V v) { 077 try { 078 consumer.accept(t, u, v); 079 } catch (final IOException e) { 080 throw wrap(e); 081 } 082 } 083 084 /** 085 * Applies an IO function with the given arguments. 086 * 087 * @param <T> the first function argument type. 088 * @param <U> the second function argument type. 089 * @param <R> the return type. 090 * @param function the function. 091 * @param t the first function argument. 092 * @param u the second function argument. 093 * @return the function result. 094 * @throws UncheckedIOException if an I/O error occurs. 095 */ 096 public static <T, U, R> R apply(final IOBiFunction<T, U, R> function, final T t, final U u) { 097 try { 098 return function.apply(t, u); 099 } catch (final IOException e) { 100 throw wrap(e); 101 } 102 } 103 104 /** 105 * Applies an IO function with the given arguments. 106 * 107 * @param function the function. 108 * @param <T> the first function argument type. 109 * @param <R> the return type. 110 * @param t the first function argument. 111 * @return the function result. 112 * @throws UncheckedIOException if an I/O error occurs. 113 */ 114 public static <T, R> R apply(final IOFunction<T, R> function, final T t) { 115 try { 116 return function.apply(t); 117 } catch (final IOException e) { 118 throw wrap(e); 119 } 120 } 121 122 /** 123 * Applies an IO quad-function with the given arguments. 124 * 125 * @param function the function. 126 * @param <T> the first function argument type. 127 * @param <U> the second function argument type. 128 * @param <V> the third function argument type. 129 * @param <W> the fourth function argument type. 130 * @param <R> the return type. 131 * @param t the first function argument. 132 * @param u the second function argument. 133 * @param v the third function argument. 134 * @param w the fourth function argument. 135 * @return the function result. 136 * @throws UncheckedIOException if an I/O error occurs. 137 */ 138 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) { 139 try { 140 return function.apply(t, u, v, w); 141 } catch (final IOException e) { 142 throw wrap(e); 143 } 144 } 145 146 /** 147 * Applies an IO tri-function with the given arguments. 148 * 149 * @param <T> the first function argument type. 150 * @param <U> the second function argument type. 151 * @param <V> the third function argument type. 152 * @param <R> the return type. 153 * @param function the function. 154 * @param t the first function argument. 155 * @param u the second function argument. 156 * @param v the third function argument. 157 * @return the function result. 158 * @throws UncheckedIOException if an I/O error occurs. 159 */ 160 public static <T, U, V, R> R apply(final IOTriFunction<T, U, V, R> function, final T t, final U u, final V v) { 161 try { 162 return function.apply(t, u, v); 163 } catch (final IOException e) { 164 throw wrap(e); 165 } 166 } 167 168 /** 169 * Compares the arguments with the comparator. 170 * 171 * @param <T> the first function argument type. 172 * @param comparator the function. 173 * @param t the first function argument. 174 * @param u the second function argument. 175 * @return the comparator result. 176 * @throws UncheckedIOException if an I/O error occurs. 177 */ 178 public static <T> int compare(final IOComparator<T> comparator, final T t, final T u) { 179 try { 180 return comparator.compare(t, u); 181 } catch (final IOException e) { 182 throw wrap(e); 183 } 184 } 185 186 /** 187 * Gets the result from an IO supplier. 188 * 189 * @param <T> the return type of the operations. 190 * @param supplier Supplies the return value. 191 * @return result from the supplier. 192 * @throws UncheckedIOException if an I/O error occurs. 193 */ 194 public static <T> T get(final IOSupplier<T> supplier) { 195 try { 196 return supplier.get(); 197 } catch (final IOException e) { 198 throw wrap(e); 199 } 200 } 201 202 /** 203 * Runs an IO runnable. 204 * 205 * @param runnable The runnable to run. 206 * @throws UncheckedIOException if an I/O error occurs. 207 */ 208 public static void run(final IORunnable runnable) { 209 try { 210 runnable.run(); 211 } catch (final IOException e) { 212 throw wrap(e); 213 } 214 } 215 216 /** 217 * Tests an IO predicate. 218 * 219 * @param <T> the type of the input to the predicate. 220 * @param predicate the predicate. 221 * @param t the input to the predicate. 222 * @return {@code true} if the input argument matches the predicate, otherwise {@code false}. 223 */ 224 public static <T> boolean test(final IOPredicate<T> predicate, final T t) { 225 try { 226 return predicate.test(t); 227 } catch (final IOException e) { 228 throw wrap(e); 229 } 230 } 231 232 /** 233 * Creates a new UncheckedIOException for the given detail message. 234 * <p> 235 * This method exists because there is no String constructor in {@link UncheckedIOException}. 236 * </p> 237 * 238 * @param e The exception to wrap. 239 * @return a new {@link UncheckedIOException}. 240 */ 241 private static UncheckedIOException wrap(final IOException e) { 242 return new UncheckedIOException(e); 243 } 244 245 /** 246 * No instances needed. 247 */ 248 private Uncheck() { 249 // no instances needed. 250 } 251}