/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package jdk.jpackage.test; import java.lang.reflect.InvocationTargetException; import java.util.function.*; public class Functional { @FunctionalInterface public interface ThrowingConsumer { void accept(T t) throws Throwable; public static Consumer toConsumer(ThrowingConsumer v) { return o -> { try { v.accept(o); } catch (Throwable ex) { rethrowUnchecked(ex); } }; } } @FunctionalInterface public interface ThrowingBiConsumer { void accept(T t, U u) throws Throwable; public static BiConsumer toBiConsumer(ThrowingBiConsumer v) { return (t, u) -> { try { v.accept(t, u); } catch (Throwable ex) { rethrowUnchecked(ex); } }; } } @FunctionalInterface public interface ThrowingSupplier { T get() throws Throwable; public static Supplier toSupplier(ThrowingSupplier v) { return () -> { try { return v.get(); } catch (Throwable ex) { rethrowUnchecked(ex); } // Unreachable return null; }; } } @FunctionalInterface public interface ThrowingFunction { R apply(T t) throws Throwable; public static Function toFunction(ThrowingFunction v) { return (t) -> { try { return v.apply(t); } catch (Throwable ex) { rethrowUnchecked(ex); } // Unreachable return null; }; } } @FunctionalInterface public interface ThrowingRunnable { void run() throws Throwable; public static Runnable toRunnable(ThrowingRunnable v) { return () -> { try { v.run(); } catch (Throwable ex) { rethrowUnchecked(ex); } }; } } public static Supplier identity(Supplier v) { return v; } public static Consumer identity(Consumer v) { return v; } public static BiConsumer identity(BiConsumer v) { return v; } public static Runnable identity(Runnable v) { return v; } public static Function identity(Function v) { return v; } public static Function identityFunction(Function v) { return v; } public static Predicate identity(Predicate v) { return v; } public static Predicate identityPredicate(Predicate v) { return v; } public static class ExceptionBox extends RuntimeException { public ExceptionBox(Throwable throwable) { super(throwable); } } @SuppressWarnings("unchecked") public static void rethrowUnchecked(Throwable throwable) throws ExceptionBox { if (throwable instanceof ExceptionBox) { throw (ExceptionBox)throwable; } if (throwable instanceof InvocationTargetException) { new ExceptionBox(throwable.getCause()); } throw new ExceptionBox(throwable); } }