• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.testutils;
18 
19 import java.util.function.Supplier;
20 
21 /**
22  * A class grouping some utilities to deal with exceptions.
23  */
24 public class FunctionalUtils {
25     /**
26      * Like a Consumer, but declared to throw an exception.
27      * @param <T>
28      */
29     @FunctionalInterface
30     public interface ThrowingConsumer<T> {
31         /** @see java.util.function.Consumer */
accept(T t)32         void accept(T t) throws Exception;
33     }
34 
35     /**
36      * Like a Supplier, but declared to throw an exception.
37      * @param <T>
38      */
39     @FunctionalInterface
40     public interface ThrowingSupplier<T> {
41         /** @see java.util.function.Supplier */
get()42         T get() throws Exception;
43     }
44 
45     /**
46      * Like a Runnable, but declared to throw an exception.
47      */
48     @FunctionalInterface
49     public interface ThrowingRunnable {
50         /** @see java.lang.Runnable */
run()51         void run() throws Exception;
52     }
53 
54     /**
55      * Convert a supplier that throws into one that doesn't.
56      *
57      * The returned supplier returns null in cases where the source throws.
58      */
ignoreExceptions(ThrowingSupplier<T> func)59     public static <T> Supplier<T> ignoreExceptions(ThrowingSupplier<T> func) {
60         return () -> {
61             try {
62                 return func.get();
63             } catch (Exception e) {
64                 return null;
65             }
66         };
67     }
68 
69     /**
70      * Convert a runnable that throws into one that doesn't.
71      *
72      * All exceptions are ignored by the returned Runnable.
73      */
74     public static Runnable ignoreExceptions(ThrowingRunnable r) {
75         return () -> {
76             try {
77                 r.run();
78             } catch (Exception e) {
79             }
80         };
81     }
82 
83     // Java has Function<T, R> and BiFunction<T, U, V> but nothing for higher-arity functions.
84     // Function3 is what Kotlin and Scala use (they also have higher-arity variants, with
85     // FunctionN taking N arguments, as the JVM does not have variadic formal parameters)
86     /**
87      * A function with three arguments.
88      * @param <TArg1> Type of the first argument
89      * @param <TArg2> Type of the second argument
90      * @param <TArg3> Type of the third argument
91      * @param <TResult> Type of the return value
92      */
93     public interface Function3<TArg1, TArg2, TArg3, TResult> {
94         /**
95          * Apply the function to the arguments
96          */
97         TResult apply(TArg1 a1, TArg2 a2, TArg3 a3);
98     }
99 }
100