/** uncheck(Class::forName, "xxx"); */ public static <T, R> R uncheck(Function_WithExceptions<T, R> function, T t) { try { return function.apply(t); } catch (Exception exception) { throwAsUnchecked(exception); return null; } }
/** * Create a new URL list from a @a stringOfUrls that contains a * list of URLs separated by commas and add them to the URL list * that's returned. */ private List<URL> convertStringToUrls(String stringOfUrls) { // Create a Function that returns a new URL object when // applied. Function<String, URL> urlFactory = ExceptionUtils.rethrowFunction(URL::new); return // Create a regular expression for the "," separator. Pattern.compile(",") // Use the regular expression to split the strongOfUrls into a Stream<String>. .splitAsStream(stringOfUrls) // Convert each String in the Stream to a URL. .map(urlFactory::apply) // Create a list of URLs. .collect(toList()); }
/** .forEach(rethrowConsumer(name -> System.out.println(Class.forName(name)))); or .forEach(rethrowConsumer(ClassNameUtil::println)); */ public static <T> Consumer<T> rethrowConsumer(Consumer_WithExceptions<T> consumer) { return t -> { try { consumer.accept(t); } catch (Exception exception) { throwAsUnchecked(exception); } }; }
/** rethrowSupplier(() -> new StringJoiner(new String(new byte[]{77, 97, 114, 107}, "UTF-8"))), */ public static <T> Supplier<T> rethrowSupplier(Supplier_WithExceptions<T> function) { return () -> { try { return function.get(); } catch (Exception exception) { throwAsUnchecked(exception); return null; } }; }
/** .map(rethrowFunction(name -> Class.forName(name))) or .map(rethrowFunction(Class::forName)) */ public static <T, R> Function<T, R> rethrowFunction(Function_WithExceptions<T, R> function) { return t -> { try { return function.apply(t); } catch (Exception exception) { throwAsUnchecked(exception); return null; } }; }
/** uncheck(() -> Class.forName("xxx")); */ public static void uncheck(Runnable_WithExceptions t) { try { t.accept(); } catch (Exception exception) { throwAsUnchecked(exception); } }
/** uncheck(() -> Class.forName("xxx")); */ public static <R> R uncheck(Supplier_WithExceptions<R> supplier) { try { return supplier.get(); } catch (Exception exception) { throwAsUnchecked(exception); return null; } }