1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package jdk.jpackage.test;
  24 
  25 import java.lang.reflect.InvocationTargetException;
  26 import java.util.function.*;
  27 
  28 
  29 public class Functional {
  30     @FunctionalInterface
  31     public interface ThrowingConsumer<T> {
  32         void accept(T t) throws Throwable;
  33 
  34         public static <T> Consumer<T> toConsumer(ThrowingConsumer<T> v) {
  35             return o -> {
  36                 try {
  37                     v.accept(o);
  38                 } catch (Throwable ex) {
  39                     rethrowUnchecked(ex);
  40                 }
  41             };
  42         }
  43     }
  44 
  45     @FunctionalInterface
  46     public interface ThrowingBiConsumer<T, U> {
  47         void accept(T t, U u) throws Throwable;
  48 
  49         public static <T, U> BiConsumer<T, U> toBiConsumer(ThrowingBiConsumer<T, U> v) {
  50             return (t, u) -> {
  51                 try {
  52                     v.accept(t, u);
  53                 } catch (Throwable ex) {
  54                     rethrowUnchecked(ex);
  55                 }
  56             };
  57         }
  58     }
  59 
  60     @FunctionalInterface
  61     public interface ThrowingSupplier<T> {
  62         T get() throws Throwable;
  63 
  64         public static <T> Supplier<T> toSupplier(ThrowingSupplier<T> v) {
  65             return () -> {
  66                 try {
  67                     return v.get();
  68                 } catch (Throwable ex) {
  69                     rethrowUnchecked(ex);
  70                 }
  71                 // Unreachable
  72                 return null;
  73             };
  74         }
  75     }
  76 
  77     @FunctionalInterface
  78     public interface ThrowingFunction<T, R> {
  79         R apply(T t) throws Throwable;
  80 
  81         public static <T, R> Function<T, R> toFunction(ThrowingFunction<T, R> v) {
  82             return (t) -> {
  83                 try {
  84                     return v.apply(t);
  85                 } catch (Throwable ex) {
  86                     rethrowUnchecked(ex);
  87                 }
  88                 // Unreachable
  89                 return null;
  90             };
  91         }
  92     }
  93 
  94     @FunctionalInterface
  95     public interface ThrowingRunnable {
  96         void run() throws Throwable;
  97 
  98         public static Runnable toRunnable(ThrowingRunnable v) {
  99             return () -> {
 100                 try {
 101                     v.run();
 102                 } catch (Throwable ex) {
 103                     rethrowUnchecked(ex);
 104                 }
 105             };
 106         }
 107     }
 108 
 109     public static <T> Supplier<T> identity(Supplier<T> v) {
 110         return v;
 111     }
 112 
 113     public static <T> Consumer<T> identity(Consumer<T> v) {
 114         return v;
 115     }
 116 
 117     public static <T, U> BiConsumer<T, U> identity(BiConsumer<T, U> v) {
 118         return v;
 119     }
 120 
 121     public static Runnable identity(Runnable v) {
 122         return v;
 123     }
 124 
 125     public static <T, R> Function<T, R> identity(Function<T, R> v) {
 126         return v;
 127     }
 128 
 129     public static <T, R> Function<T, R> identityFunction(Function<T, R> v) {
 130         return v;
 131     }
 132 
 133     public static <T> Predicate<T> identity(Predicate<T> v) {
 134         return v;
 135     }
 136 
 137     public static <T> Predicate<T> identityPredicate(Predicate<T> v) {
 138         return v;
 139     }
 140 
 141     public static class ExceptionBox extends RuntimeException {
 142         public ExceptionBox(Throwable throwable) {
 143             super(throwable);
 144         }
 145     }
 146 
 147     @SuppressWarnings("unchecked")
 148     public static void rethrowUnchecked(Throwable throwable) throws ExceptionBox {
 149         if (throwable instanceof ExceptionBox) {
 150             throw (ExceptionBox)throwable;
 151         }
 152 
 153         if (throwable instanceof InvocationTargetException) {
 154             new ExceptionBox(throwable.getCause());
 155         }
 156 
 157         throw new ExceptionBox(throwable);
 158     }
 159 }