2019-01-23 14:59:25 +01:00
|
|
|
/*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership.
|
|
|
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
* (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package org.apache.commons.lang3;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.UncheckedIOException;
|
|
|
|
import java.lang.reflect.UndeclaredThrowableException;
|
2020-01-26 22:57:13 +01:00
|
|
|
import java.util.Collection;
|
2019-12-25 23:00:43 -05:00
|
|
|
import java.util.Objects;
|
2019-08-22 22:09:11 +02:00
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
import java.util.function.BiConsumer;
|
|
|
|
import java.util.function.BiFunction;
|
|
|
|
import java.util.function.BiPredicate;
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
import java.util.function.Function;
|
|
|
|
import java.util.function.Predicate;
|
|
|
|
import java.util.function.Supplier;
|
2020-01-26 22:57:13 +01:00
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.Streams.FailableStream;
|
2019-01-23 14:59:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
/** This class provides utility functions, and classes for working with the
|
|
|
|
* {@code java.util.function} package, or more generally, with Java 8
|
|
|
|
* lambdas.
|
|
|
|
* More specifically, it attempts to address the fact that lambdas are supposed
|
|
|
|
* not to throw Exceptions, at least not checked Exceptions, aka instances of
|
|
|
|
* {@link Exception}. This enforces the use of constructs like
|
2020-02-17 15:58:19 +01:00
|
|
|
* <pre>{@code
|
|
|
|
* Consumer<java.lang.reflect.Method> consumer = (m) -> {
|
2019-01-23 14:59:25 +01:00
|
|
|
* try {
|
|
|
|
* m.invoke(o, args);
|
|
|
|
* } catch (Throwable t) {
|
|
|
|
* throw Functions.rethrow(t);
|
|
|
|
* }
|
|
|
|
* };
|
2020-02-17 15:58:19 +01:00
|
|
|
* }</pre>
|
2019-02-08 20:05:43 +02:00
|
|
|
* By replacing a {@link java.util.function.Consumer Consumer<O>} with a
|
|
|
|
* {@link FailableConsumer FailableConsumer<O,? extends Throwable>}, this can be
|
2019-01-23 14:59:25 +01:00
|
|
|
* written like follows:
|
2020-02-17 15:58:19 +01:00
|
|
|
* <pre>{@code
|
|
|
|
* Functions.accept((m) -> m.invoke(o,args));
|
|
|
|
* }</pre>
|
2019-01-23 14:59:25 +01:00
|
|
|
* Obviously, the second version is much more concise and the spirit of
|
|
|
|
* Lambda expressions is met better than the second version.
|
|
|
|
*/
|
|
|
|
public class Functions {
|
2019-09-05 11:49:43 -04:00
|
|
|
|
2019-02-08 19:30:35 +02:00
|
|
|
@FunctionalInterface
|
|
|
|
public interface FailableRunnable<T extends Throwable> {
|
|
|
|
/**
|
|
|
|
* Runs the function.
|
|
|
|
* @throws T if the function fails
|
|
|
|
*/
|
|
|
|
void run() throws T;
|
|
|
|
}
|
2019-09-05 11:49:43 -04:00
|
|
|
|
2019-02-08 19:30:35 +02:00
|
|
|
@FunctionalInterface
|
|
|
|
public interface FailableCallable<O, T extends Throwable> {
|
|
|
|
/**
|
|
|
|
* Calls the callable.
|
|
|
|
* @return The value returned from the callable
|
|
|
|
* @throws T if the callable fails
|
|
|
|
*/
|
|
|
|
O call() throws T;
|
|
|
|
}
|
2019-09-05 11:49:43 -04:00
|
|
|
|
2019-02-08 19:30:35 +02:00
|
|
|
@FunctionalInterface
|
|
|
|
public interface FailableConsumer<O, T extends Throwable> {
|
|
|
|
/**
|
|
|
|
* Accepts the consumer.
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param object the parameter for the consumable to accept
|
2019-02-08 19:30:35 +02:00
|
|
|
* @throws T if the consumer fails
|
|
|
|
*/
|
2020-02-17 10:10:40 -05:00
|
|
|
void accept(O object) throws T;
|
2019-02-08 19:30:35 +02:00
|
|
|
}
|
2019-09-05 11:49:43 -04:00
|
|
|
|
2019-02-08 19:30:35 +02:00
|
|
|
@FunctionalInterface
|
|
|
|
public interface FailableBiConsumer<O1, O2, T extends Throwable> {
|
|
|
|
/**
|
|
|
|
* Accepts the consumer.
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param object1 the first parameter for the consumable to accept
|
|
|
|
* @param object2 the second parameter for the consumable to accept
|
2019-02-08 19:30:35 +02:00
|
|
|
* @throws T if the consumer fails
|
|
|
|
*/
|
2020-02-17 10:10:40 -05:00
|
|
|
void accept(O1 object1, O2 object2) throws T;
|
2019-02-08 19:30:35 +02:00
|
|
|
}
|
2019-09-05 11:49:43 -04:00
|
|
|
|
2019-02-08 19:30:35 +02:00
|
|
|
@FunctionalInterface
|
|
|
|
public interface FailableFunction<I, O, T extends Throwable> {
|
|
|
|
/**
|
|
|
|
* Apply the function.
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param input the input for the function
|
2019-02-08 19:30:35 +02:00
|
|
|
* @return the result of the function
|
|
|
|
* @throws T if the function fails
|
|
|
|
*/
|
2020-02-17 10:10:40 -05:00
|
|
|
O apply(I input) throws T;
|
2019-02-08 19:30:35 +02:00
|
|
|
}
|
2019-09-05 11:49:43 -04:00
|
|
|
|
2019-02-08 19:30:35 +02:00
|
|
|
@FunctionalInterface
|
|
|
|
public interface FailableBiFunction<I1, I2, O, T extends Throwable> {
|
|
|
|
/**
|
|
|
|
* Apply the function.
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param input1 the first input for the function
|
|
|
|
* @param input2 the second input for the function
|
2019-02-08 19:30:35 +02:00
|
|
|
* @return the result of the function
|
|
|
|
* @throws T if the function fails
|
|
|
|
*/
|
2020-02-17 10:10:40 -05:00
|
|
|
O apply(I1 input1, I2 input2) throws T;
|
2019-02-08 19:30:35 +02:00
|
|
|
}
|
2019-09-05 11:49:43 -04:00
|
|
|
|
2019-02-08 19:30:35 +02:00
|
|
|
@FunctionalInterface
|
|
|
|
public interface FailablePredicate<O, T extends Throwable> {
|
|
|
|
/**
|
|
|
|
* Test the predicate.
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param object the object to test the predicate on
|
2019-02-08 19:30:35 +02:00
|
|
|
* @return the predicate's evaluation
|
|
|
|
* @throws T if the predicate fails
|
|
|
|
*/
|
2020-02-17 10:10:40 -05:00
|
|
|
boolean test(O object) throws T;
|
2019-02-08 19:30:35 +02:00
|
|
|
}
|
2019-09-05 11:49:43 -04:00
|
|
|
|
2019-02-08 19:30:35 +02:00
|
|
|
@FunctionalInterface
|
|
|
|
public interface FailableBiPredicate<O1, O2, T extends Throwable> {
|
|
|
|
/**
|
|
|
|
* Test the predicate.
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param object1 the first object to test the predicate on
|
|
|
|
* @param object2 the second object to test the predicate on
|
2019-02-08 19:30:35 +02:00
|
|
|
* @return the predicate's evaluation
|
|
|
|
* @throws T if the predicate fails
|
|
|
|
*/
|
2020-02-17 10:10:40 -05:00
|
|
|
boolean test(O1 object1, O2 object2) throws T;
|
2019-02-08 19:30:35 +02:00
|
|
|
}
|
2019-09-05 11:49:43 -04:00
|
|
|
|
2019-08-22 22:09:11 +02:00
|
|
|
@FunctionalInterface
|
|
|
|
public interface FailableSupplier<O, T extends Throwable> {
|
|
|
|
/**
|
|
|
|
* Supplies an object
|
|
|
|
* @return the suppliers result
|
|
|
|
* @throws T if the supplier fails
|
|
|
|
*/
|
|
|
|
O get() throws T;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the given {@link FailableRunnable} into a standard {@link Runnable}.
|
2019-08-25 13:09:20 +12:00
|
|
|
*
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param runnable a {@code FailableRunnable}
|
2019-08-25 13:09:20 +12:00
|
|
|
* @return a standard {@code Runnable}
|
2019-08-22 22:09:11 +02:00
|
|
|
*/
|
2020-02-17 10:10:40 -05:00
|
|
|
public static Runnable asRunnable(final FailableRunnable<?> runnable) {
|
|
|
|
return () -> run(runnable);
|
2019-08-22 22:09:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the given {@link FailableConsumer} into a standard {@link Consumer}.
|
2019-08-25 13:09:20 +12:00
|
|
|
*
|
|
|
|
* @param <I> the type used by the consumers
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param consumer a {@code FailableConsumer}
|
2019-08-25 13:09:20 +12:00
|
|
|
* @return a standard {@code Consumer}
|
2019-08-22 22:09:11 +02:00
|
|
|
*/
|
2020-02-17 10:10:40 -05:00
|
|
|
public static <I> Consumer<I> asConsumer(final FailableConsumer<I, ?> consumer) {
|
|
|
|
return input -> accept(consumer, input);
|
2019-08-22 22:09:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the given {@link FailableCallable} into a standard {@link Callable}.
|
2019-08-25 13:09:20 +12:00
|
|
|
*
|
|
|
|
* @param <O> the type used by the callables
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param callable a {@code FailableCallable}
|
2019-08-25 13:09:20 +12:00
|
|
|
* @return a standard {@code Callable}
|
2019-08-22 22:09:11 +02:00
|
|
|
*/
|
2020-02-17 10:10:40 -05:00
|
|
|
public static <O> Callable<O> asCallable(final FailableCallable<O, ?> callable) {
|
|
|
|
return () -> call(callable);
|
2019-08-22 22:09:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the given {@link FailableBiConsumer} into a standard {@link BiConsumer}.
|
2019-08-25 13:09:20 +12:00
|
|
|
*
|
|
|
|
* @param <I1> the type of the first argument of the consumers
|
|
|
|
* @param <I2> the type of the second argument of the consumers
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param consumer a failable {@code BiConsumer}
|
2019-08-25 13:09:20 +12:00
|
|
|
* @return a standard {@code BiConsumer}
|
2019-08-22 22:09:11 +02:00
|
|
|
*/
|
2020-02-17 10:10:40 -05:00
|
|
|
public static <I1, I2> BiConsumer<I1, I2> asBiConsumer(final FailableBiConsumer<I1, I2, ?> consumer) {
|
|
|
|
return (input1, input2) -> accept(consumer, input1, input2);
|
2019-08-22 22:09:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the given {@link FailableFunction} into a standard {@link Function}.
|
2019-08-25 13:09:20 +12:00
|
|
|
*
|
|
|
|
* @param <I> the type of the input of the functions
|
|
|
|
* @param <O> the type of the output of the functions
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param function a {code FailableFunction}
|
2019-08-25 13:09:20 +12:00
|
|
|
* @return a standard {@code Function}
|
2019-08-22 22:09:11 +02:00
|
|
|
*/
|
2020-02-17 10:10:40 -05:00
|
|
|
public static <I, O> Function<I, O> asFunction(final FailableFunction<I, O, ?> function) {
|
|
|
|
return input -> apply(function, input);
|
2019-08-22 22:09:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the given {@link FailableBiFunction} into a standard {@link BiFunction}.
|
2019-08-25 13:09:20 +12:00
|
|
|
*
|
|
|
|
* @param <I1> the type of the first argument of the input of the functions
|
|
|
|
* @param <I2> the type of the second argument of the input of the functions
|
|
|
|
* @param <O> the type of the output of the functions
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param function a {@code FailableBiFunction}
|
2019-08-25 13:09:20 +12:00
|
|
|
* @return a standard {@code BiFunction}
|
2019-08-22 22:09:11 +02:00
|
|
|
*/
|
2020-02-17 10:10:40 -05:00
|
|
|
public static <I1, I2, O> BiFunction<I1, I2, O> asBiFunction(final FailableBiFunction<I1, I2, O, ?> function) {
|
|
|
|
return (input1, input2) -> apply(function, input1, input2);
|
2019-08-22 22:09:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the given {@link FailablePredicate} into a standard {@link Predicate}.
|
2019-08-25 13:09:20 +12:00
|
|
|
*
|
|
|
|
* @param <I> the type used by the predicates
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param predicate a {@code FailablePredicate}
|
2019-08-25 13:09:20 +12:00
|
|
|
* @return a standard {@code Predicate}
|
2019-08-22 22:09:11 +02:00
|
|
|
*/
|
2020-02-17 10:10:40 -05:00
|
|
|
public static <I> Predicate<I> asPredicate(final FailablePredicate<I, ?> predicate) {
|
|
|
|
return input -> test(predicate, input);
|
2019-08-22 22:09:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the given {@link FailableBiPredicate} into a standard {@link BiPredicate}.
|
2019-08-25 13:09:20 +12:00
|
|
|
*
|
|
|
|
* @param <I1> the type of the first argument used by the predicates
|
|
|
|
* @param <I2> the type of the second argument used by the predicates
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param predicate a {@code FailableBiPredicate}
|
2019-08-25 13:09:20 +12:00
|
|
|
* @return a standard {@code BiPredicate}
|
2019-08-22 22:09:11 +02:00
|
|
|
*/
|
2020-02-17 10:10:40 -05:00
|
|
|
public static <I1, I2> BiPredicate<I1, I2> asBiPredicate(final FailableBiPredicate<I1, I2, ?> predicate) {
|
|
|
|
return (input1, input2) -> test(predicate, input1, input2);
|
2019-08-22 22:09:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the given {@link FailableSupplier} into a standard {@link Supplier}.
|
2019-08-25 13:09:20 +12:00
|
|
|
*
|
|
|
|
* @param <O> the type supplied by the suppliers
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param supplier a {@code FailableSupplier}
|
2019-08-25 13:09:20 +12:00
|
|
|
* @return a standard {@code Supplier}
|
2019-08-22 22:09:11 +02:00
|
|
|
*/
|
2020-02-17 10:10:40 -05:00
|
|
|
public static <O> Supplier<O> asSupplier(final FailableSupplier<O, ?> supplier) {
|
|
|
|
return () -> get(supplier);
|
2019-08-22 22:09:11 +02:00
|
|
|
}
|
2019-01-23 14:59:25 +01:00
|
|
|
|
2019-02-08 19:30:35 +02:00
|
|
|
/**
|
|
|
|
* Runs a runnable and rethrows any exception as a {@link RuntimeException}.
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param runnable The runnable to run
|
2019-02-08 19:30:35 +02:00
|
|
|
* @param <T> the type of checked exception the runnable may throw
|
|
|
|
*/
|
2020-02-17 10:10:40 -05:00
|
|
|
public static <T extends Throwable> void run(final FailableRunnable<T> runnable) {
|
2019-02-08 19:30:35 +02:00
|
|
|
try {
|
2020-02-17 10:10:40 -05:00
|
|
|
runnable.run();
|
2020-02-14 09:36:04 -05:00
|
|
|
} catch (final Throwable t) {
|
2019-02-08 19:30:35 +02:00
|
|
|
throw rethrow(t);
|
|
|
|
}
|
|
|
|
}
|
2019-01-23 14:59:25 +01:00
|
|
|
|
2019-02-08 19:30:35 +02:00
|
|
|
/**
|
|
|
|
* Calls a callable and rethrows any exception as a {@link RuntimeException}.
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param callable the callable to call
|
2019-02-08 19:30:35 +02:00
|
|
|
* @param <O> the return type of the callable
|
|
|
|
* @param <T> the type of checked exception the callable may throw
|
|
|
|
* @return the value returned from the callable
|
|
|
|
*/
|
2020-02-17 10:10:40 -05:00
|
|
|
public static <O, T extends Throwable> O call(final FailableCallable<O, T> callable) {
|
2019-02-08 19:30:35 +02:00
|
|
|
try {
|
2020-02-17 10:10:40 -05:00
|
|
|
return callable.call();
|
2020-02-14 09:36:04 -05:00
|
|
|
} catch (final Throwable t) {
|
2019-02-08 19:30:35 +02:00
|
|
|
throw rethrow(t);
|
|
|
|
}
|
|
|
|
}
|
2019-01-23 14:59:25 +01:00
|
|
|
|
2019-02-08 19:30:35 +02:00
|
|
|
/**
|
|
|
|
* Consumes a consumer and rethrows any exception as a {@link RuntimeException}.
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param consumer the consumer to consume
|
|
|
|
* @param object the object to consume by {@code consumer}
|
2019-02-08 19:30:35 +02:00
|
|
|
* @param <O> the type the consumer accepts
|
|
|
|
* @param <T> the type of checked exception the consumer may throw
|
|
|
|
*/
|
2020-02-17 10:10:40 -05:00
|
|
|
public static <O, T extends Throwable> void accept(final FailableConsumer<O, T> consumer, final O object) {
|
2019-02-08 19:30:35 +02:00
|
|
|
try {
|
2020-02-17 10:10:40 -05:00
|
|
|
consumer.accept(object);
|
2020-02-14 09:36:04 -05:00
|
|
|
} catch (final Throwable t) {
|
2019-02-08 19:30:35 +02:00
|
|
|
throw rethrow(t);
|
|
|
|
}
|
|
|
|
}
|
2019-01-23 14:59:25 +01:00
|
|
|
|
2019-02-08 19:30:35 +02:00
|
|
|
/**
|
|
|
|
* Consumes a consumer and rethrows any exception as a {@link RuntimeException}.
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param consumer the consumer to consume
|
|
|
|
* @param object1 the first object to consume by {@code consumer}
|
|
|
|
* @param object2 the second object to consume by {@code consumer}
|
2019-02-08 19:30:35 +02:00
|
|
|
* @param <O1> the type of the first argument the consumer accepts
|
|
|
|
* @param <O2> the type of the second argument the consumer accepts
|
|
|
|
* @param <T> the type of checked exception the consumer may throw
|
|
|
|
*/
|
2020-02-17 10:10:40 -05:00
|
|
|
public static <O1, O2, T extends Throwable> void accept(final FailableBiConsumer<O1, O2, T> consumer, final O1 object1, final O2 object2) {
|
2019-02-08 19:30:35 +02:00
|
|
|
try {
|
2020-02-17 10:10:40 -05:00
|
|
|
consumer.accept(object1, object2);
|
2020-02-14 09:36:04 -05:00
|
|
|
} catch (final Throwable t) {
|
2019-02-08 19:30:35 +02:00
|
|
|
throw rethrow(t);
|
|
|
|
}
|
|
|
|
}
|
2019-01-23 14:59:25 +01:00
|
|
|
|
2019-02-08 19:30:35 +02:00
|
|
|
/**
|
|
|
|
* Applies a function and rethrows any exception as a {@link RuntimeException}.
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param function the function to apply
|
|
|
|
* @param input the input to apply {@code function} on
|
2019-02-08 19:30:35 +02:00
|
|
|
* @param <I> the type of the argument the function accepts
|
|
|
|
* @param <O> the return type of the function
|
|
|
|
* @param <T> the type of checked exception the function may throw
|
|
|
|
* @return the value returned from the function
|
|
|
|
*/
|
2020-02-17 10:10:40 -05:00
|
|
|
public static <I, O, T extends Throwable> O apply(final FailableFunction<I, O, T> function, final I input) {
|
2019-02-08 19:30:35 +02:00
|
|
|
try {
|
2020-02-17 10:10:40 -05:00
|
|
|
return function.apply(input);
|
2020-02-14 09:36:04 -05:00
|
|
|
} catch (final Throwable t) {
|
2019-02-08 19:30:35 +02:00
|
|
|
throw rethrow(t);
|
|
|
|
}
|
|
|
|
}
|
2019-01-23 14:59:25 +01:00
|
|
|
|
2019-02-08 19:30:35 +02:00
|
|
|
/**
|
|
|
|
* Applies a function and rethrows any exception as a {@link RuntimeException}.
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param function the function to apply
|
|
|
|
* @param input1 the first input to apply {@code function} on
|
|
|
|
* @param input2 the second input to apply {@code function} on
|
2019-02-08 19:30:35 +02:00
|
|
|
* @param <I1> the type of the first argument the function accepts
|
|
|
|
* @param <I2> the type of the second argument the function accepts
|
|
|
|
* @param <O> the return type of the function
|
|
|
|
* @param <T> the type of checked exception the function may throw
|
|
|
|
* @return the value returned from the function
|
|
|
|
*/
|
2020-02-17 10:10:40 -05:00
|
|
|
public static <I1, I2, O, T extends Throwable> O apply(final FailableBiFunction<I1, I2, O, T> function, final I1 input1, final I2 input2) {
|
2019-02-08 19:30:35 +02:00
|
|
|
try {
|
2020-02-17 10:10:40 -05:00
|
|
|
return function.apply(input1, input2);
|
2020-02-14 09:36:04 -05:00
|
|
|
} catch (final Throwable t) {
|
2019-02-08 19:30:35 +02:00
|
|
|
throw rethrow(t);
|
|
|
|
}
|
|
|
|
}
|
2019-01-23 14:59:25 +01:00
|
|
|
|
2019-02-08 19:30:35 +02:00
|
|
|
/**
|
|
|
|
* Tests a predicate and rethrows any exception as a {@link RuntimeException}.
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param predicate the predicate to test
|
|
|
|
* @param object the input to test by {@code predicate}
|
2019-02-08 19:30:35 +02:00
|
|
|
* @param <O> the type of argument the predicate tests
|
|
|
|
* @param <T> the type of checked exception the predicate may throw
|
|
|
|
* @return the boolean value returned by the predicate
|
|
|
|
*/
|
2020-02-17 10:10:40 -05:00
|
|
|
public static <O, T extends Throwable> boolean test(final FailablePredicate<O, T> predicate, final O object) {
|
2019-02-08 19:30:35 +02:00
|
|
|
try {
|
2020-02-17 10:10:40 -05:00
|
|
|
return predicate.test(object);
|
2020-02-14 09:36:04 -05:00
|
|
|
} catch (final Throwable t) {
|
2019-02-08 19:30:35 +02:00
|
|
|
throw rethrow(t);
|
|
|
|
}
|
|
|
|
}
|
2019-01-29 11:42:07 +01:00
|
|
|
|
2019-02-08 19:30:35 +02:00
|
|
|
/**
|
|
|
|
* Tests a predicate and rethrows any exception as a {@link RuntimeException}.
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param predicate the predicate to test
|
|
|
|
* @param object1 the first input to test by {@code predicate}
|
|
|
|
* @param object2 the second input to test by {@code predicate}
|
2019-02-08 19:30:35 +02:00
|
|
|
* @param <O1> the type of the first argument the predicate tests
|
|
|
|
* @param <O2> the type of the second argument the predicate tests
|
|
|
|
* @param <T> the type of checked exception the predicate may throw
|
|
|
|
* @return the boolean value returned by the predicate
|
|
|
|
*/
|
2020-02-17 10:10:40 -05:00
|
|
|
public static <O1, O2, T extends Throwable> boolean test(final FailableBiPredicate<O1, O2, T> predicate, final O1 object1, final O2 object2) {
|
2019-02-08 19:30:35 +02:00
|
|
|
try {
|
2020-02-17 10:10:40 -05:00
|
|
|
return predicate.test(object1, object2);
|
2020-02-14 09:36:04 -05:00
|
|
|
} catch (final Throwable t) {
|
2019-02-08 19:30:35 +02:00
|
|
|
throw rethrow(t);
|
|
|
|
}
|
|
|
|
}
|
2019-01-29 11:42:07 +01:00
|
|
|
|
2019-08-22 22:09:11 +02:00
|
|
|
/**
|
|
|
|
* Invokes the supplier, and returns the result.
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param supplier The supplier to invoke.
|
2019-08-22 22:09:11 +02:00
|
|
|
* @param <O> The suppliers output type.
|
|
|
|
* @param <T> The type of checked exception, which the supplier can throw.
|
|
|
|
* @return The object, which has been created by the supplier
|
|
|
|
*/
|
2020-02-17 10:10:40 -05:00
|
|
|
public static <O, T extends Throwable> O get(final FailableSupplier<O, T> supplier) {
|
2019-08-25 13:09:20 +12:00
|
|
|
try {
|
2020-02-17 10:10:40 -05:00
|
|
|
return supplier.get();
|
2020-02-14 09:36:04 -05:00
|
|
|
} catch (final Throwable t) {
|
2019-08-25 13:09:20 +12:00
|
|
|
throw rethrow(t);
|
|
|
|
}
|
2019-08-22 22:09:11 +02:00
|
|
|
}
|
|
|
|
|
2020-01-26 22:57:13 +01:00
|
|
|
/**
|
|
|
|
* Converts the given stream into a {@link FailableStream}. The
|
|
|
|
* {@link FailableStream} consists of the same elements, than the
|
|
|
|
* input stream. However, failable lambdas, like
|
|
|
|
* {@link FailablePredicate}, {@link FailableFunction}, and
|
|
|
|
* {@link FailableConsumer} may be applied, rather than
|
|
|
|
* {@link Predicate}, {@link Function}, {@link Consumer}, etc.
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param stream The stream, which is being converted into a
|
2020-01-26 22:57:13 +01:00
|
|
|
* {@link FailableStream}.
|
|
|
|
* @param <O> The streams element type.
|
|
|
|
* @return The created {@link FailableStream}.
|
|
|
|
*/
|
2020-02-17 10:10:40 -05:00
|
|
|
public static <O> FailableStream<O> stream(final Stream<O> stream) {
|
|
|
|
return new FailableStream<>(stream);
|
2020-01-26 22:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the given collection into a {@link FailableStream}.
|
|
|
|
* The {@link FailableStream} consists of the collections
|
|
|
|
* elements. Shortcut for
|
|
|
|
* <pre>
|
2020-02-17 10:10:40 -05:00
|
|
|
* Functions.stream(collection.stream());
|
2020-01-26 22:57:13 +01:00
|
|
|
* </pre>
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param collection The collection, which is being converted into a
|
2020-01-26 22:57:13 +01:00
|
|
|
* {@link FailableStream}.
|
|
|
|
* @param <O> The collections element type. (In turn, the result
|
|
|
|
* streams element type.)
|
|
|
|
* @return The created {@link FailableStream}.
|
|
|
|
*/
|
2020-02-17 10:10:40 -05:00
|
|
|
public static <O> FailableStream<O> stream(final Collection<O> collection) {
|
|
|
|
return new FailableStream<>(collection.stream());
|
2020-01-26 22:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-08-22 22:09:11 +02:00
|
|
|
|
2019-02-08 19:30:35 +02:00
|
|
|
/**
|
|
|
|
* A simple try-with-resources implementation, that can be used, if your
|
|
|
|
* objects do not implement the {@link AutoCloseable} interface. The method
|
2020-02-17 10:10:40 -05:00
|
|
|
* executes the {@code action}. The method guarantees, that <em>all</em>
|
|
|
|
* the {@code resources} are being executed, in the given order, afterwards,
|
2019-02-08 19:30:35 +02:00
|
|
|
* and regardless of success, or failure. If either the original action, or
|
|
|
|
* any of the resource action fails, then the <em>first</em> failure (aka
|
|
|
|
* {@link Throwable} is rethrown. Example use:
|
2020-02-17 15:58:19 +01:00
|
|
|
* <pre>{@code
|
2019-02-08 19:30:35 +02:00
|
|
|
* final FileInputStream fis = new FileInputStream("my.file");
|
2020-02-17 15:58:19 +01:00
|
|
|
* Functions.tryWithResources(useInputStream(fis), null, () -> fis.close());
|
|
|
|
* }</pre>
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param action The action to execute. This object <em>will</em> always
|
2019-02-08 19:30:35 +02:00
|
|
|
* be invoked.
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param errorHandler An optional error handler, which will be invoked finally,
|
2019-02-08 19:30:35 +02:00
|
|
|
* if any error occurred. The error handler will receive the first
|
|
|
|
* error, aka {@link Throwable}.
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param resources The resource actions to execute. <em>All</em> resource
|
2019-02-08 19:30:35 +02:00
|
|
|
* actions will be invoked, in the given order. A resource action is an
|
|
|
|
* instance of {@link FailableRunnable}, which will be executed.
|
|
|
|
* @see #tryWithResources(FailableRunnable, FailableRunnable...)
|
|
|
|
*/
|
|
|
|
@SafeVarargs
|
2020-02-17 10:10:40 -05:00
|
|
|
public static void tryWithResources(final FailableRunnable<? extends Throwable> action,
|
|
|
|
final FailableConsumer<Throwable, ? extends Throwable> errorHandler,
|
|
|
|
final FailableRunnable<? extends Throwable>... resources) {
|
|
|
|
final FailableConsumer<Throwable, ? extends Throwable> actualErrorHandler;
|
|
|
|
if (errorHandler == null) {
|
|
|
|
actualErrorHandler = (t) -> rethrow(t);
|
2019-02-08 19:30:35 +02:00
|
|
|
} else {
|
2020-02-17 10:10:40 -05:00
|
|
|
actualErrorHandler = errorHandler;
|
2019-02-08 19:30:35 +02:00
|
|
|
}
|
2020-02-17 10:10:40 -05:00
|
|
|
if (resources != null) {
|
|
|
|
for (final FailableRunnable<? extends Throwable> failableRunnable : resources) {
|
2019-12-25 23:00:43 -05:00
|
|
|
Objects.requireNonNull(failableRunnable, "runnable");
|
2019-02-08 19:30:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Throwable th = null;
|
|
|
|
try {
|
2020-02-17 10:10:40 -05:00
|
|
|
action.run();
|
2020-02-14 09:36:04 -05:00
|
|
|
} catch (final Throwable t) {
|
2019-02-08 19:30:35 +02:00
|
|
|
th = t;
|
|
|
|
}
|
2020-02-17 10:10:40 -05:00
|
|
|
if (resources != null) {
|
|
|
|
for (final FailableRunnable<? extends Object> runnable : resources) {
|
2019-02-08 19:30:35 +02:00
|
|
|
try {
|
|
|
|
runnable.run();
|
2020-02-14 09:36:04 -05:00
|
|
|
} catch (final Throwable t) {
|
2019-02-08 19:30:35 +02:00
|
|
|
if (th == null) {
|
|
|
|
th = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (th != null) {
|
|
|
|
try {
|
2020-02-17 10:10:40 -05:00
|
|
|
actualErrorHandler.accept(th);
|
2020-02-14 09:36:04 -05:00
|
|
|
} catch (final Throwable t) {
|
2019-02-08 19:30:35 +02:00
|
|
|
throw rethrow(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-29 11:42:07 +01:00
|
|
|
|
2019-02-08 19:30:35 +02:00
|
|
|
/**
|
|
|
|
* A simple try-with-resources implementation, that can be used, if your
|
|
|
|
* objects do not implement the {@link AutoCloseable} interface. The method
|
2020-02-17 10:10:40 -05:00
|
|
|
* executes the {@code action}. The method guarantees, that <em>all</em>
|
|
|
|
* the {@code resources} are being executed, in the given order, afterwards,
|
2019-02-08 19:30:35 +02:00
|
|
|
* and regardless of success, or failure. If either the original action, or
|
|
|
|
* any of the resource action fails, then the <em>first</em> failure (aka
|
|
|
|
* {@link Throwable} is rethrown. Example use:
|
2020-02-17 15:58:19 +01:00
|
|
|
* <pre>{@code
|
2019-02-08 19:30:35 +02:00
|
|
|
* final FileInputStream fis = new FileInputStream("my.file");
|
2020-02-17 15:58:19 +01:00
|
|
|
* Functions.tryWithResources(useInputStream(fis), () -> fis.close());
|
|
|
|
* }</pre>
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param action The action to execute. This object <em>will</em> always
|
2019-02-08 19:30:35 +02:00
|
|
|
* be invoked.
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param resources The resource actions to execute. <em>All</em> resource
|
2019-02-08 19:30:35 +02:00
|
|
|
* actions will be invoked, in the given order. A resource action is an
|
|
|
|
* instance of {@link FailableRunnable}, which will be executed.
|
|
|
|
* @see #tryWithResources(FailableRunnable, FailableConsumer, FailableRunnable...)
|
|
|
|
*/
|
|
|
|
@SafeVarargs
|
2020-02-17 10:10:40 -05:00
|
|
|
public static void tryWithResources(final FailableRunnable<? extends Throwable> action,
|
|
|
|
final FailableRunnable<? extends Throwable>... resources) {
|
|
|
|
tryWithResources(action, null, resources);
|
2019-02-08 19:30:35 +02:00
|
|
|
}
|
2019-01-29 11:42:07 +01:00
|
|
|
|
2019-02-08 19:30:35 +02:00
|
|
|
/**
|
2019-12-21 14:42:53 +01:00
|
|
|
* <p>Rethrows a {@link Throwable} as an unchecked exception. If the argument is
|
|
|
|
* already unchecked, namely a {@code RuntimeException} or {@code Error} then
|
|
|
|
* the argument will be rethrown without modification. If the exception is
|
|
|
|
* {@code IOException} then it will be wrapped into a {@code UncheckedIOException}.
|
|
|
|
* In every other cases the exception will be wrapped into a {@code
|
|
|
|
* UndeclaredThrowableException}</p>
|
|
|
|
*
|
|
|
|
* <p>Note that there is a declared return type for this method, even though it
|
|
|
|
* never returns. The reason for that is to support the usual pattern:</p>
|
|
|
|
*
|
|
|
|
* <pre>
|
|
|
|
* throw rethrow(myUncheckedException);
|
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* <p>instead of just calling the method. This pattern may help the Java compiler to
|
2020-02-17 10:10:40 -05:00
|
|
|
* recognize that at that oint an exception will be thrown and the code flow
|
2019-12-21 14:42:53 +01:00
|
|
|
* analysis will not demand otherwise mandatory commands that could follow the
|
|
|
|
* method call, like a {@code return} statement from a value returning method.</p>
|
|
|
|
*
|
2020-02-17 10:10:40 -05:00
|
|
|
* @param throwable The throwable to rethrow ossibly wrapped into an unchecked exception
|
2019-12-21 14:42:53 +01:00
|
|
|
* @return Never returns anything, this method never terminates normally.
|
2019-02-08 19:30:35 +02:00
|
|
|
*/
|
2020-02-17 10:10:40 -05:00
|
|
|
public static RuntimeException rethrow(final Throwable throwable) {
|
|
|
|
Objects.requireNonNull(throwable, "throwable");
|
|
|
|
if (throwable instanceof RuntimeException) {
|
|
|
|
throw (RuntimeException) throwable;
|
|
|
|
} else if (throwable instanceof Error) {
|
|
|
|
throw (Error) throwable;
|
|
|
|
} else if (throwable instanceof IOException) {
|
|
|
|
throw new UncheckedIOException((IOException) throwable);
|
2019-02-08 19:30:35 +02:00
|
|
|
} else {
|
2020-02-17 10:10:40 -05:00
|
|
|
throw new UndeclaredThrowableException(throwable);
|
2019-02-08 19:30:35 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-23 14:59:25 +01:00
|
|
|
}
|