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