/** * Closes the underlying reader. * * @throws UncheckedIOException if an IO exception occurs */ @Override public void close() { Unchecked.wrap(() -> reader.close()); }
/** * Converts checked exceptions to unchecked based on the {@code Runnable} interface. * <p> * This wraps the specified runnable returning an instance that handles checked exceptions. * If a checked exception is thrown it is converted to an {@link UncheckedIOException} * or {@link RuntimeException} as appropriate. * * @param runnable the runnable to be decorated * @return the runnable instance that handles checked exceptions */ public static Runnable runnable(CheckedRunnable runnable) { return () -> { try { runnable.run(); } catch (Throwable ex) { throw propagate(ex); } }; }
public void test_biConsumer_success() { BiConsumer<String, String> a = Unchecked.biConsumer((t, u) -> {}); a.accept("A", "B"); }
public void test_predicate_success() { Predicate<String> a = Unchecked.predicate((t) -> true); assertEquals(a.test("A"), true); }
public void test_function_success() { Function<String, String> a = Unchecked.function((t) -> t); assertEquals(a.apply("A"), "A"); }
public void test_binaryOperator_success() { BinaryOperator<String> a = Unchecked.binaryOperator((t, u) -> t + u); assertEquals(a.apply("A", "B"), "AB"); }
public void test_biPredicate_success() { BiPredicate<String, String> a = Unchecked.biPredicate((t, u) -> true); assertEquals(a.test("A", "B"), true); }
public void test_biFunction_success() { BiFunction<String, String, String> a = Unchecked.biFunction((t, u) -> t + u); assertEquals(a.apply("A", "B"), "AB"); }
public void test_consumer_success() { Consumer<String> a = Unchecked.consumer((t) -> {}); a.accept("A"); }
public void test_predicate_fail2() { Predicate<String> a = Unchecked.predicate((t) -> { throw new Exception(); }); assertThrows(() -> a.test("A"), RuntimeException.class); }
public void test_function_fail1() { Function<String, String> a = Unchecked.function((t) -> { throw new IOException(); }); assertThrows(() -> a.apply("A"), UncheckedIOException.class); }
public void test_binaryOperator_fail2() { BinaryOperator<String> a = Unchecked.binaryOperator((t, u) -> { throw new Exception(); }); assertThrows(() -> a.apply("A", "B"), RuntimeException.class); }
public void test_biPredicate_fail2() { BiPredicate<String, String> a = Unchecked.biPredicate((t, u) -> { throw new Exception(); }); assertThrows(() -> a.test("A", "B"), RuntimeException.class); }
public void test_biFunction_fail1() { BiFunction<String, String, String> a = Unchecked.biFunction((t, u) -> { throw new IOException(); }); assertThrows(() -> a.apply("A", "B"), UncheckedIOException.class); }
public void test_consumer_fail2() { Consumer<String> a = Unchecked.consumer((t) -> { throw new Exception(); }); assertThrows(() -> a.accept("A"), RuntimeException.class); }
/** * Obtains an instance of the specified named type by name. * <p> * This method operates by reflection. * It requires a static method {@code of(String)} method to be present on the type specified. * If the method does not exist an exception is thrown. * * @param <T> the named type * @param type the named type with the {@code of(String)} method * @param name the name to find * @return the instance of the named type * @throws IllegalArgumentException if the specified name could not be found */ public static <T extends Named> T of(Class<T> type, String name) { return Unchecked.wrap(() -> { Method method = type.getMethod("of", String.class); return type.cast(method.invoke(null, name)); }); }
/** * Converts checked exceptions to unchecked based on the {@code UnaryOperator} interface. * <p> * This wraps the specified operator returning an instance that handles checked exceptions. * If a checked exception is thrown it is converted to an {@link UncheckedIOException} * or {@link RuntimeException} as appropriate. * * @param <T> the type of the operator * @param function the function to be decorated * @return the function instance that handles checked exceptions */ public static <T> UnaryOperator<T> unaryOperator(CheckedUnaryOperator<T> function) { return (t) -> { try { return function.apply(t); } catch (Throwable ex) { throw propagate(ex); } }; }
public void test_predicate_fail1() { Predicate<String> a = Unchecked.predicate((t) -> { throw new IOException(); }); assertThrows(() -> a.test("A"), UncheckedIOException.class); }
public void test_biConsumer_fail1() { BiConsumer<String, String> a = Unchecked.biConsumer((t, u) -> { throw new IOException(); }); assertThrows(() -> a.accept("A", "B"), UncheckedIOException.class); }
public void test_function_fail2() { Function<String, String> a = Unchecked.function((t) -> { throw new Exception(); }); assertThrows(() -> a.apply("A"), RuntimeException.class); }