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.Consumer;
  27 import java.util.function.Function;
  28 import java.util.function.Predicate;
  29 import java.util.function.Supplier;
  30 
  31 
  32 public class Functional {
  33     @FunctionalInterface
  34     public interface ThrowingConsumer<T> {
  35         void accept(T t) throws Throwable;
  36 
  37         public static <T> Consumer<T> toConsumer(ThrowingConsumer<T> v) {
  38             return o -> {
  39                 try {
  40                     v.accept(o);
  41                 } catch (Throwable ex) {
  42                     rethrowUnchecked(ex);
  43                 }
  44             };
  45         }
  46     }
  47 
  48     @FunctionalInterface
  49     public interface ThrowingSupplier<T> {
  50         T get() throws Throwable;
  51 
  52         public static <T> Supplier<T> toSupplier(ThrowingSupplier<T> v) {
  53             return () -> {
  54                 try {
  55                     return v.get();
  56                 } catch (Throwable ex) {
  57                     rethrowUnchecked(ex);
  58                 }
  59                 // Unreachable
  60                 return null;
  61             };
  62         }
  63     }
  64 
  65     @FunctionalInterface
  66     public interface ThrowingFunction<T, R> {
  67         R apply(T t) throws Throwable;
  68 
  69         public static <T, R> Function<T, R> toFunction(ThrowingFunction<T, R> v) {
  70             return (t) -> {
  71                 try {
  72                     return v.apply(t);
  73                 } catch (Throwable ex) {
  74                     rethrowUnchecked(ex);
  75                 }
  76                 // Unreachable
  77                 return null;
  78             };
  79         }
  80     }
  81 
  82     @FunctionalInterface
  83     public interface ThrowingRunnable {
  84         void run() throws Throwable;
  85 
  86         public static Runnable toRunnable(ThrowingRunnable v) {
  87             return () -> {
  88                 try {
  89                     v.run();
  90                 } catch (Throwable ex) {
  91                     rethrowUnchecked(ex);
  92                 }
  93             };
  94         }
  95     }
  96 
  97     public static <T> Supplier<T> identity(Supplier<T> v) {
  98         return v;
  99     }
 100 
 101     public static <T> Consumer<T> identity(Consumer<T> v) {
 102         return v;
 103     }
 104 
 105     public static Runnable identity(Runnable v) {
 106         return v;
 107     }
 108 
 109     public static <T, R> Function<T, R> identity(Function<T, R> v) {
 110         return v;
 111     }
 112 
 113     public static <T, R> Function<T, R> identityFunction(Function<T, R> v) {
 114         return v;
 115     }
 116 
 117     public static <T> Predicate<T> identity(Predicate<T> v) {
 118         return v;
 119     }
 120 
 121     public static <T> Predicate<T> identityPredicate(Predicate<T> v) {
 122         return v;
 123     }
 124 
 125     public static class ExceptionBox extends RuntimeException {
 126         public ExceptionBox(Throwable throwable) {
 127             super(throwable);
 128         }
 129     }
 130 
 131     @SuppressWarnings("unchecked")
 132     public static void rethrowUnchecked(Throwable throwable) throws ExceptionBox {
 133         if (throwable instanceof ExceptionBox) {
 134             throw (ExceptionBox)throwable;
 135         }
 136 
 137         if (throwable instanceof InvocationTargetException) {
 138             new ExceptionBox(throwable.getCause());
 139         }
 140 
 141         throw new ExceptionBox(throwable);
 142     }
 143 }