Don't prefix parameter names with 'p'.

This commit is contained in:
Gary Gregory 2020-02-17 10:10:40 -05:00
parent 7b62c8f6c9
commit f46f4e35c3
1 changed files with 121 additions and 121 deletions

View File

@ -83,67 +83,67 @@ public class Functions {
public interface FailableConsumer<O, T extends Throwable> { public interface FailableConsumer<O, T extends Throwable> {
/** /**
* Accepts the consumer. * Accepts the consumer.
* @param pObject the parameter for the consumable to accept * @param object the parameter for the consumable to accept
* @throws T if the consumer fails * @throws T if the consumer fails
*/ */
void accept(O pObject) throws T; void accept(O object) throws T;
} }
@FunctionalInterface @FunctionalInterface
public interface FailableBiConsumer<O1, O2, T extends Throwable> { public interface FailableBiConsumer<O1, O2, T extends Throwable> {
/** /**
* Accepts the consumer. * Accepts the consumer.
* @param pObject1 the first parameter for the consumable to accept * @param object1 the first parameter for the consumable to accept
* @param pObject2 the second parameter for the consumable to accept * @param object2 the second parameter for the consumable to accept
* @throws T if the consumer fails * @throws T if the consumer fails
*/ */
void accept(O1 pObject1, O2 pObject2) throws T; void accept(O1 object1, O2 object2) throws T;
} }
@FunctionalInterface @FunctionalInterface
public interface FailableFunction<I, O, T extends Throwable> { public interface FailableFunction<I, O, T extends Throwable> {
/** /**
* Apply the function. * Apply the function.
* @param pInput the input for the function * @param input the input for the function
* @return the result of the function * @return the result of the function
* @throws T if the function fails * @throws T if the function fails
*/ */
O apply(I pInput) throws T; O apply(I input) throws T;
} }
@FunctionalInterface @FunctionalInterface
public interface FailableBiFunction<I1, I2, O, T extends Throwable> { public interface FailableBiFunction<I1, I2, O, T extends Throwable> {
/** /**
* Apply the function. * Apply the function.
* @param pInput1 the first input for the function * @param input1 the first input for the function
* @param pInput2 the second input for the function * @param input2 the second input for the function
* @return the result of the function * @return the result of the function
* @throws T if the function fails * @throws T if the function fails
*/ */
O apply(I1 pInput1, I2 pInput2) throws T; O apply(I1 input1, I2 input2) throws T;
} }
@FunctionalInterface @FunctionalInterface
public interface FailablePredicate<O, T extends Throwable> { public interface FailablePredicate<O, T extends Throwable> {
/** /**
* Test the predicate. * Test the predicate.
* @param pObject the object to test the predicate on * @param object the object to test the predicate on
* @return the predicate's evaluation * @return the predicate's evaluation
* @throws T if the predicate fails * @throws T if the predicate fails
*/ */
boolean test(O pObject) throws T; boolean test(O object) throws T;
} }
@FunctionalInterface @FunctionalInterface
public interface FailableBiPredicate<O1, O2, T extends Throwable> { public interface FailableBiPredicate<O1, O2, T extends Throwable> {
/** /**
* Test the predicate. * Test the predicate.
* @param pObject1 the first object to test the predicate on * @param object1 the first object to test the predicate on
* @param pObject2 the second object to test the predicate on * @param object2 the second object to test the predicate on
* @return the predicate's evaluation * @return the predicate's evaluation
* @throws T if the predicate fails * @throws T if the predicate fails
*/ */
boolean test(O1 pObject1, O2 pObject2) throws T; boolean test(O1 object1, O2 object2) throws T;
} }
@FunctionalInterface @FunctionalInterface
@ -159,33 +159,33 @@ public class Functions {
/** /**
* Converts the given {@link FailableRunnable} into a standard {@link Runnable}. * Converts the given {@link FailableRunnable} into a standard {@link Runnable}.
* *
* @param pRunnable a {@code FailableRunnable} * @param runnable a {@code FailableRunnable}
* @return a standard {@code Runnable} * @return a standard {@code Runnable}
*/ */
public static Runnable asRunnable(final FailableRunnable<?> pRunnable) { public static Runnable asRunnable(final FailableRunnable<?> runnable) {
return () -> run(pRunnable); return () -> run(runnable);
} }
/** /**
* Converts the given {@link FailableConsumer} into a standard {@link Consumer}. * Converts the given {@link FailableConsumer} into a standard {@link Consumer}.
* *
* @param <I> the type used by the consumers * @param <I> the type used by the consumers
* @param pConsumer a {@code FailableConsumer} * @param consumer a {@code FailableConsumer}
* @return a standard {@code Consumer} * @return a standard {@code Consumer}
*/ */
public static <I> Consumer<I> asConsumer(final FailableConsumer<I, ?> pConsumer) { public static <I> Consumer<I> asConsumer(final FailableConsumer<I, ?> consumer) {
return (pInput) -> accept(pConsumer, pInput); return input -> accept(consumer, input);
} }
/** /**
* Converts the given {@link FailableCallable} into a standard {@link Callable}. * Converts the given {@link FailableCallable} into a standard {@link Callable}.
* *
* @param <O> the type used by the callables * @param <O> the type used by the callables
* @param pCallable a {@code FailableCallable} * @param callable a {@code FailableCallable}
* @return a standard {@code Callable} * @return a standard {@code Callable}
*/ */
public static <O> Callable<O> asCallable(final FailableCallable<O, ?> pCallable) { public static <O> Callable<O> asCallable(final FailableCallable<O, ?> callable) {
return () -> call(pCallable); return () -> call(callable);
} }
/** /**
@ -193,11 +193,11 @@ public class Functions {
* *
* @param <I1> the type of the first argument of the consumers * @param <I1> the type of the first argument of the consumers
* @param <I2> the type of the second argument of the consumers * @param <I2> the type of the second argument of the consumers
* @param pConsumer a failable {@code BiConsumer} * @param consumer a failable {@code BiConsumer}
* @return a standard {@code BiConsumer} * @return a standard {@code BiConsumer}
*/ */
public static <I1, I2> BiConsumer<I1, I2> asBiConsumer(final FailableBiConsumer<I1, I2, ?> pConsumer) { public static <I1, I2> BiConsumer<I1, I2> asBiConsumer(final FailableBiConsumer<I1, I2, ?> consumer) {
return (pInput1, pInput2) -> accept(pConsumer, pInput1, pInput2); return (input1, input2) -> accept(consumer, input1, input2);
} }
/** /**
@ -205,11 +205,11 @@ public class Functions {
* *
* @param <I> the type of the input of the functions * @param <I> the type of the input of the functions
* @param <O> the type of the output of the functions * @param <O> the type of the output of the functions
* @param pFunction a {code FailableFunction} * @param function a {code FailableFunction}
* @return a standard {@code Function} * @return a standard {@code Function}
*/ */
public static <I, O> Function<I, O> asFunction(final FailableFunction<I, O, ?> pFunction) { public static <I, O> Function<I, O> asFunction(final FailableFunction<I, O, ?> function) {
return (pInput) -> apply(pFunction, pInput); return input -> apply(function, input);
} }
/** /**
@ -218,22 +218,22 @@ public class Functions {
* @param <I1> the type of the first argument of the input of the functions * @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 <I2> the type of the second argument of the input of the functions
* @param <O> the type of the output of the functions * @param <O> the type of the output of the functions
* @param pFunction a {@code FailableBiFunction} * @param function a {@code FailableBiFunction}
* @return a standard {@code BiFunction} * @return a standard {@code BiFunction}
*/ */
public static <I1, I2, O> BiFunction<I1, I2, O> asBiFunction(final FailableBiFunction<I1, I2, O, ?> pFunction) { public static <I1, I2, O> BiFunction<I1, I2, O> asBiFunction(final FailableBiFunction<I1, I2, O, ?> function) {
return (pInput1, pInput2) -> apply(pFunction, pInput1, pInput2); return (input1, input2) -> apply(function, input1, input2);
} }
/** /**
* Converts the given {@link FailablePredicate} into a standard {@link Predicate}. * Converts the given {@link FailablePredicate} into a standard {@link Predicate}.
* *
* @param <I> the type used by the predicates * @param <I> the type used by the predicates
* @param pPredicate a {@code FailablePredicate} * @param predicate a {@code FailablePredicate}
* @return a standard {@code Predicate} * @return a standard {@code Predicate}
*/ */
public static <I> Predicate<I> asPredicate(final FailablePredicate<I, ?> pPredicate) { public static <I> Predicate<I> asPredicate(final FailablePredicate<I, ?> predicate) {
return (pInput) -> test(pPredicate, pInput); return input -> test(predicate, input);
} }
/** /**
@ -241,32 +241,32 @@ public class Functions {
* *
* @param <I1> the type of the first argument used by the predicates * @param <I1> the type of the first argument used by the predicates
* @param <I2> the type of the second argument used by the predicates * @param <I2> the type of the second argument used by the predicates
* @param pPredicate a {@code FailableBiPredicate} * @param predicate a {@code FailableBiPredicate}
* @return a standard {@code BiPredicate} * @return a standard {@code BiPredicate}
*/ */
public static <I1, I2> BiPredicate<I1, I2> asBiPredicate(final FailableBiPredicate<I1, I2, ?> pPredicate) { public static <I1, I2> BiPredicate<I1, I2> asBiPredicate(final FailableBiPredicate<I1, I2, ?> predicate) {
return (pInput1, pInput2) -> test(pPredicate, pInput1, pInput2); return (input1, input2) -> test(predicate, input1, input2);
} }
/** /**
* Converts the given {@link FailableSupplier} into a standard {@link Supplier}. * Converts the given {@link FailableSupplier} into a standard {@link Supplier}.
* *
* @param <O> the type supplied by the suppliers * @param <O> the type supplied by the suppliers
* @param pSupplier a {@code FailableSupplier} * @param supplier a {@code FailableSupplier}
* @return a standard {@code Supplier} * @return a standard {@code Supplier}
*/ */
public static <O> Supplier<O> asSupplier(final FailableSupplier<O, ?> pSupplier) { public static <O> Supplier<O> asSupplier(final FailableSupplier<O, ?> supplier) {
return () -> get(pSupplier); return () -> get(supplier);
} }
/** /**
* Runs a runnable and rethrows any exception as a {@link RuntimeException}. * Runs a runnable and rethrows any exception as a {@link RuntimeException}.
* @param pRunnable The runnable to run * @param runnable The runnable to run
* @param <T> the type of checked exception the runnable may throw * @param <T> the type of checked exception the runnable may throw
*/ */
public static <T extends Throwable> void run(final FailableRunnable<T> pRunnable) { public static <T extends Throwable> void run(final FailableRunnable<T> runnable) {
try { try {
pRunnable.run(); runnable.run();
} catch (final Throwable t) { } catch (final Throwable t) {
throw rethrow(t); throw rethrow(t);
} }
@ -274,14 +274,14 @@ public class Functions {
/** /**
* Calls a callable and rethrows any exception as a {@link RuntimeException}. * Calls a callable and rethrows any exception as a {@link RuntimeException}.
* @param pCallable the callable to call * @param callable the callable to call
* @param <O> the return type of the callable * @param <O> the return type of the callable
* @param <T> the type of checked exception the callable may throw * @param <T> the type of checked exception the callable may throw
* @return the value returned from the callable * @return the value returned from the callable
*/ */
public static <O, T extends Throwable> O call(final FailableCallable<O, T> pCallable) { public static <O, T extends Throwable> O call(final FailableCallable<O, T> callable) {
try { try {
return pCallable.call(); return callable.call();
} catch (final Throwable t) { } catch (final Throwable t) {
throw rethrow(t); throw rethrow(t);
} }
@ -289,14 +289,14 @@ public class Functions {
/** /**
* Consumes a consumer and rethrows any exception as a {@link RuntimeException}. * Consumes a consumer and rethrows any exception as a {@link RuntimeException}.
* @param pConsumer the consumer to consume * @param consumer the consumer to consume
* @param pObject the object to consume by {@code pConsumer} * @param object the object to consume by {@code consumer}
* @param <O> the type the consumer accepts * @param <O> the type the consumer accepts
* @param <T> the type of checked exception the consumer may throw * @param <T> the type of checked exception the consumer may throw
*/ */
public static <O, T extends Throwable> void accept(final FailableConsumer<O, T> pConsumer, final O pObject) { public static <O, T extends Throwable> void accept(final FailableConsumer<O, T> consumer, final O object) {
try { try {
pConsumer.accept(pObject); consumer.accept(object);
} catch (final Throwable t) { } catch (final Throwable t) {
throw rethrow(t); throw rethrow(t);
} }
@ -304,16 +304,16 @@ public class Functions {
/** /**
* Consumes a consumer and rethrows any exception as a {@link RuntimeException}. * Consumes a consumer and rethrows any exception as a {@link RuntimeException}.
* @param pConsumer the consumer to consume * @param consumer the consumer to consume
* @param pObject1 the first object to consume by {@code pConsumer} * @param object1 the first object to consume by {@code consumer}
* @param pObject2 the second object to consume by {@code pConsumer} * @param object2 the second object to consume by {@code consumer}
* @param <O1> the type of the first argument the consumer accepts * @param <O1> the type of the first argument the consumer accepts
* @param <O2> the type of the second 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 * @param <T> the type of checked exception the consumer may throw
*/ */
public static <O1, O2, T extends Throwable> void accept(final FailableBiConsumer<O1, O2, T> pConsumer, final O1 pObject1, final O2 pObject2) { public static <O1, O2, T extends Throwable> void accept(final FailableBiConsumer<O1, O2, T> consumer, final O1 object1, final O2 object2) {
try { try {
pConsumer.accept(pObject1, pObject2); consumer.accept(object1, object2);
} catch (final Throwable t) { } catch (final Throwable t) {
throw rethrow(t); throw rethrow(t);
} }
@ -321,16 +321,16 @@ public class Functions {
/** /**
* Applies a function and rethrows any exception as a {@link RuntimeException}. * Applies a function and rethrows any exception as a {@link RuntimeException}.
* @param pFunction the function to apply * @param function the function to apply
* @param pInput the input to apply {@code pFunction} on * @param input the input to apply {@code function} on
* @param <I> the type of the argument the function accepts * @param <I> the type of the argument the function accepts
* @param <O> the return type of the function * @param <O> the return type of the function
* @param <T> the type of checked exception the function may throw * @param <T> the type of checked exception the function may throw
* @return the value returned from the function * @return the value returned from the function
*/ */
public static <I, O, T extends Throwable> O apply(final FailableFunction<I, O, T> pFunction, final I pInput) { public static <I, O, T extends Throwable> O apply(final FailableFunction<I, O, T> function, final I input) {
try { try {
return pFunction.apply(pInput); return function.apply(input);
} catch (final Throwable t) { } catch (final Throwable t) {
throw rethrow(t); throw rethrow(t);
} }
@ -338,18 +338,18 @@ public class Functions {
/** /**
* Applies a function and rethrows any exception as a {@link RuntimeException}. * Applies a function and rethrows any exception as a {@link RuntimeException}.
* @param pFunction the function to apply * @param function the function to apply
* @param pInput1 the first input to apply {@code pFunction} on * @param input1 the first input to apply {@code function} on
* @param pInput2 the second input to apply {@code pFunction} on * @param input2 the second input to apply {@code function} on
* @param <I1> the type of the first argument the function accepts * @param <I1> the type of the first argument the function accepts
* @param <I2> the type of the second argument the function accepts * @param <I2> the type of the second argument the function accepts
* @param <O> the return type of the function * @param <O> the return type of the function
* @param <T> the type of checked exception the function may throw * @param <T> the type of checked exception the function may throw
* @return the value returned from the function * @return the value returned from the function
*/ */
public static <I1, I2, O, T extends Throwable> O apply(final FailableBiFunction<I1, I2, O, T> pFunction, final I1 pInput1, final I2 pInput2) { public static <I1, I2, O, T extends Throwable> O apply(final FailableBiFunction<I1, I2, O, T> function, final I1 input1, final I2 input2) {
try { try {
return pFunction.apply(pInput1, pInput2); return function.apply(input1, input2);
} catch (final Throwable t) { } catch (final Throwable t) {
throw rethrow(t); throw rethrow(t);
} }
@ -357,15 +357,15 @@ public class Functions {
/** /**
* Tests a predicate and rethrows any exception as a {@link RuntimeException}. * Tests a predicate and rethrows any exception as a {@link RuntimeException}.
* @param pPredicate the predicate to test * @param predicate the predicate to test
* @param pObject the input to test by {@code pPredicate} * @param object the input to test by {@code predicate}
* @param <O> the type of argument the predicate tests * @param <O> the type of argument the predicate tests
* @param <T> the type of checked exception the predicate may throw * @param <T> the type of checked exception the predicate may throw
* @return the boolean value returned by the predicate * @return the boolean value returned by the predicate
*/ */
public static <O, T extends Throwable> boolean test(final FailablePredicate<O, T> pPredicate, final O pObject) { public static <O, T extends Throwable> boolean test(final FailablePredicate<O, T> predicate, final O object) {
try { try {
return pPredicate.test(pObject); return predicate.test(object);
} catch (final Throwable t) { } catch (final Throwable t) {
throw rethrow(t); throw rethrow(t);
} }
@ -373,17 +373,17 @@ public class Functions {
/** /**
* Tests a predicate and rethrows any exception as a {@link RuntimeException}. * Tests a predicate and rethrows any exception as a {@link RuntimeException}.
* @param pPredicate the predicate to test * @param predicate the predicate to test
* @param pObject1 the first input to test by {@code pPredicate} * @param object1 the first input to test by {@code predicate}
* @param pObject2 the second input to test by {@code pPredicate} * @param object2 the second input to test by {@code predicate}
* @param <O1> the type of the first argument the predicate tests * @param <O1> the type of the first argument the predicate tests
* @param <O2> the type of the second 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 * @param <T> the type of checked exception the predicate may throw
* @return the boolean value returned by the predicate * @return the boolean value returned by the predicate
*/ */
public static <O1, O2, T extends Throwable> boolean test(final FailableBiPredicate<O1, O2, T> pPredicate, final O1 pObject1, final O2 pObject2) { public static <O1, O2, T extends Throwable> boolean test(final FailableBiPredicate<O1, O2, T> predicate, final O1 object1, final O2 object2) {
try { try {
return pPredicate.test(pObject1, pObject2); return predicate.test(object1, object2);
} catch (final Throwable t) { } catch (final Throwable t) {
throw rethrow(t); throw rethrow(t);
} }
@ -391,14 +391,14 @@ public class Functions {
/** /**
* Invokes the supplier, and returns the result. * Invokes the supplier, and returns the result.
* @param pSupplier The supplier to invoke. * @param supplier The supplier to invoke.
* @param <O> The suppliers output type. * @param <O> The suppliers output type.
* @param <T> The type of checked exception, which the supplier can throw. * @param <T> The type of checked exception, which the supplier can throw.
* @return The object, which has been created by the supplier * @return The object, which has been created by the supplier
*/ */
public static <O, T extends Throwable> O get(final FailableSupplier<O, T> pSupplier) { public static <O, T extends Throwable> O get(final FailableSupplier<O, T> supplier) {
try { try {
return pSupplier.get(); return supplier.get();
} catch (final Throwable t) { } catch (final Throwable t) {
throw rethrow(t); throw rethrow(t);
} }
@ -411,13 +411,13 @@ public class Functions {
* {@link FailablePredicate}, {@link FailableFunction}, and * {@link FailablePredicate}, {@link FailableFunction}, and
* {@link FailableConsumer} may be applied, rather than * {@link FailableConsumer} may be applied, rather than
* {@link Predicate}, {@link Function}, {@link Consumer}, etc. * {@link Predicate}, {@link Function}, {@link Consumer}, etc.
* @param pStream The stream, which is being converted into a * @param stream The stream, which is being converted into a
* {@link FailableStream}. * {@link FailableStream}.
* @param <O> The streams element type. * @param <O> The streams element type.
* @return The created {@link FailableStream}. * @return The created {@link FailableStream}.
*/ */
public static <O> FailableStream<O> stream(final Stream<O> pStream) { public static <O> FailableStream<O> stream(final Stream<O> stream) {
return new FailableStream<>(pStream); return new FailableStream<>(stream);
} }
/** /**
@ -425,24 +425,24 @@ public class Functions {
* The {@link FailableStream} consists of the collections * The {@link FailableStream} consists of the collections
* elements. Shortcut for * elements. Shortcut for
* <pre> * <pre>
* Functions.stream(pCollection.stream()); * Functions.stream(collection.stream());
* </pre> * </pre>
* @param pCollection The collection, which is being converted into a * @param collection The collection, which is being converted into a
* {@link FailableStream}. * {@link FailableStream}.
* @param <O> The collections element type. (In turn, the result * @param <O> The collections element type. (In turn, the result
* streams element type.) * streams element type.)
* @return The created {@link FailableStream}. * @return The created {@link FailableStream}.
*/ */
public static <O> FailableStream<O> stream(final Collection<O> pCollection) { public static <O> FailableStream<O> stream(final Collection<O> collection) {
return new FailableStream<>(pCollection.stream()); return new FailableStream<>(collection.stream());
} }
/** /**
* A simple try-with-resources implementation, that can be used, if your * A simple try-with-resources implementation, that can be used, if your
* objects do not implement the {@link AutoCloseable} interface. The method * objects do not implement the {@link AutoCloseable} interface. The method
* executes the {@code pAction}. The method guarantees, that <em>all</em> * executes the {@code action}. The method guarantees, that <em>all</em>
* the {@code pResources} are being executed, in the given order, afterwards, * the {@code resources} are being executed, in the given order, afterwards,
* and regardless of success, or failure. If either the original action, or * 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 * any of the resource action fails, then the <em>first</em> failure (aka
* {@link Throwable} is rethrown. Example use: * {@link Throwable} is rethrown. Example use:
@ -450,39 +450,39 @@ public class Functions {
* final FileInputStream fis = new FileInputStream("my.file"); * final FileInputStream fis = new FileInputStream("my.file");
* Functions.tryWithResources(useInputStream(fis), null, () -> fis.close()); * Functions.tryWithResources(useInputStream(fis), null, () -> fis.close());
* }</pre> * }</pre>
* @param pAction The action to execute. This object <em>will</em> always * @param action The action to execute. This object <em>will</em> always
* be invoked. * be invoked.
* @param pErrorHandler An optional error handler, which will be invoked finally, * @param errorHandler An optional error handler, which will be invoked finally,
* if any error occurred. The error handler will receive the first * if any error occurred. The error handler will receive the first
* error, aka {@link Throwable}. * error, aka {@link Throwable}.
* @param pResources The resource actions to execute. <em>All</em> resource * @param resources The resource actions to execute. <em>All</em> resource
* actions will be invoked, in the given order. A resource action is an * actions will be invoked, in the given order. A resource action is an
* instance of {@link FailableRunnable}, which will be executed. * instance of {@link FailableRunnable}, which will be executed.
* @see #tryWithResources(FailableRunnable, FailableRunnable...) * @see #tryWithResources(FailableRunnable, FailableRunnable...)
*/ */
@SafeVarargs @SafeVarargs
public static void tryWithResources(final FailableRunnable<? extends Throwable> pAction, public static void tryWithResources(final FailableRunnable<? extends Throwable> action,
final FailableConsumer<Throwable, ? extends Throwable> pErrorHandler, final FailableConsumer<Throwable, ? extends Throwable> errorHandler,
final FailableRunnable<? extends Throwable>... pResources) { final FailableRunnable<? extends Throwable>... resources) {
final FailableConsumer<Throwable, ? extends Throwable> errorHandler; final FailableConsumer<Throwable, ? extends Throwable> actualErrorHandler;
if (pErrorHandler == null) { if (errorHandler == null) {
errorHandler = (t) -> rethrow(t); actualErrorHandler = (t) -> rethrow(t);
} else { } else {
errorHandler = pErrorHandler; actualErrorHandler = errorHandler;
} }
if (pResources != null) { if (resources != null) {
for (final FailableRunnable<? extends Throwable> failableRunnable : pResources) { for (final FailableRunnable<? extends Throwable> failableRunnable : resources) {
Objects.requireNonNull(failableRunnable, "runnable"); Objects.requireNonNull(failableRunnable, "runnable");
} }
} }
Throwable th = null; Throwable th = null;
try { try {
pAction.run(); action.run();
} catch (final Throwable t) { } catch (final Throwable t) {
th = t; th = t;
} }
if (pResources != null) { if (resources != null) {
for (final FailableRunnable<? extends Object> runnable : pResources) { for (final FailableRunnable<? extends Object> runnable : resources) {
try { try {
runnable.run(); runnable.run();
} catch (final Throwable t) { } catch (final Throwable t) {
@ -494,7 +494,7 @@ public class Functions {
} }
if (th != null) { if (th != null) {
try { try {
errorHandler.accept(th); actualErrorHandler.accept(th);
} catch (final Throwable t) { } catch (final Throwable t) {
throw rethrow(t); throw rethrow(t);
} }
@ -504,8 +504,8 @@ public class Functions {
/** /**
* A simple try-with-resources implementation, that can be used, if your * A simple try-with-resources implementation, that can be used, if your
* objects do not implement the {@link AutoCloseable} interface. The method * objects do not implement the {@link AutoCloseable} interface. The method
* executes the {@code pAction}. The method guarantees, that <em>all</em> * executes the {@code action}. The method guarantees, that <em>all</em>
* the {@code pResources} are being executed, in the given order, afterwards, * the {@code resources} are being executed, in the given order, afterwards,
* and regardless of success, or failure. If either the original action, or * 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 * any of the resource action fails, then the <em>first</em> failure (aka
* {@link Throwable} is rethrown. Example use: * {@link Throwable} is rethrown. Example use:
@ -513,17 +513,17 @@ public class Functions {
* final FileInputStream fis = new FileInputStream("my.file"); * final FileInputStream fis = new FileInputStream("my.file");
* Functions.tryWithResources(useInputStream(fis), () -> fis.close()); * Functions.tryWithResources(useInputStream(fis), () -> fis.close());
* }</pre> * }</pre>
* @param pAction The action to execute. This object <em>will</em> always * @param action The action to execute. This object <em>will</em> always
* be invoked. * be invoked.
* @param pResources The resource actions to execute. <em>All</em> resource * @param resources The resource actions to execute. <em>All</em> resource
* actions will be invoked, in the given order. A resource action is an * actions will be invoked, in the given order. A resource action is an
* instance of {@link FailableRunnable}, which will be executed. * instance of {@link FailableRunnable}, which will be executed.
* @see #tryWithResources(FailableRunnable, FailableConsumer, FailableRunnable...) * @see #tryWithResources(FailableRunnable, FailableConsumer, FailableRunnable...)
*/ */
@SafeVarargs @SafeVarargs
public static void tryWithResources(final FailableRunnable<? extends Throwable> pAction, public static void tryWithResources(final FailableRunnable<? extends Throwable> action,
final FailableRunnable<? extends Throwable>... pResources) { final FailableRunnable<? extends Throwable>... resources) {
tryWithResources(pAction, null, pResources); tryWithResources(action, null, resources);
} }
/** /**
@ -542,23 +542,23 @@ public class Functions {
* </pre> * </pre>
* *
* <p>instead of just calling the method. This pattern may help the Java compiler to * <p>instead of just calling the method. This pattern may help the Java compiler to
* recognize that at that point an exception will be thrown and the code flow * recognize that at that oint an exception will be thrown and the code flow
* analysis will not demand otherwise mandatory commands that could follow the * analysis will not demand otherwise mandatory commands that could follow the
* method call, like a {@code return} statement from a value returning method.</p> * method call, like a {@code return} statement from a value returning method.</p>
* *
* @param pThrowable The throwable to rethrow possibly wrapped into an unchecked exception * @param throwable The throwable to rethrow ossibly wrapped into an unchecked exception
* @return Never returns anything, this method never terminates normally. * @return Never returns anything, this method never terminates normally.
*/ */
public static RuntimeException rethrow(final Throwable pThrowable) { public static RuntimeException rethrow(final Throwable throwable) {
Objects.requireNonNull(pThrowable, "pThrowable"); Objects.requireNonNull(throwable, "throwable");
if (pThrowable instanceof RuntimeException) { if (throwable instanceof RuntimeException) {
throw (RuntimeException) pThrowable; throw (RuntimeException) throwable;
} else if (pThrowable instanceof Error) { } else if (throwable instanceof Error) {
throw (Error) pThrowable; throw (Error) throwable;
} else if (pThrowable instanceof IOException) { } else if (throwable instanceof IOException) {
throw new UncheckedIOException((IOException) pThrowable); throw new UncheckedIOException((IOException) throwable);
} else { } else {
throw new UndeclaredThrowableException(pThrowable); throw new UndeclaredThrowableException(throwable);
} }
} }
} }