From 665deb4d82aa0296405a3e20d70000ce8f24b920 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 9 Sep 2022 09:33:50 -0700 Subject: [PATCH] Add examples to Javadoc for Throwing*.of factory methods Closes gh-28969 --- .../util/function/ThrowingBiFunction.java | 14 ++++++++++++++ .../util/function/ThrowingConsumer.java | 14 ++++++++++++++ .../util/function/ThrowingFunction.java | 14 ++++++++++++++ .../util/function/ThrowingSupplier.java | 14 ++++++++++++++ 4 files changed, 56 insertions(+) diff --git a/spring-core/src/main/java/org/springframework/util/function/ThrowingBiFunction.java b/spring-core/src/main/java/org/springframework/util/function/ThrowingBiFunction.java index fd1717ea4fb8..5f62862a5928 100644 --- a/spring-core/src/main/java/org/springframework/util/function/ThrowingBiFunction.java +++ b/spring-core/src/main/java/org/springframework/util/function/ThrowingBiFunction.java @@ -104,6 +104,13 @@ public R apply(T t, U u) { * {@link ThrowingBiFunction} where the {@link #apply(Object, Object)} * method wraps any checked exception thrown by the supplied lambda expression * or method reference. + *

This method can be especially useful when working with method references. + * It allows you to easily convert a method that throws a checked exception + * into an instance compatible with a regular {@link BiFunction}. + *

For example: + *

+	 * map.replaceAll(ThrowingBiFunction.of(Example::methodThatCanThrowCheckedException));
+	 * 
* @param the type of the first argument to the function * @param the type of the second argument to the function * @param the type of the result of the function @@ -119,6 +126,13 @@ static ThrowingBiFunction of(ThrowingBiFunction func * {@link ThrowingBiFunction} where the {@link #apply(Object, Object)} * method wraps any thrown checked exceptions using the given * {@code exceptionWrapper}. + *

This method can be especially useful when working with method references. + * It allows you to easily convert a method that throws a checked exception + * into an instance compatible with a regular {@link BiFunction}. + *

For example: + *

+	 * map.replaceAll(ThrowingBiFunction.of(Example::methodThatCanThrowCheckedException, IllegalStateException::new));
+	 * 
* @param the type of the first argument to the function * @param the type of the second argument to the function * @param the type of the result of the function diff --git a/spring-core/src/main/java/org/springframework/util/function/ThrowingConsumer.java b/spring-core/src/main/java/org/springframework/util/function/ThrowingConsumer.java index 09848791bb39..b42d91f86b92 100644 --- a/spring-core/src/main/java/org/springframework/util/function/ThrowingConsumer.java +++ b/spring-core/src/main/java/org/springframework/util/function/ThrowingConsumer.java @@ -96,6 +96,13 @@ public void accept(T t) { * {@link ThrowingConsumer} where the {@link #accept(Object)} method wraps * any checked exception thrown by the supplied lambda expression or method * reference. + *

This method can be especially useful when working with method references. + * It allows you to easily convert a method that throws a checked exception + * into an instance compatible with a regular {@link Consumer}. + *

For example: + *

+	 * list.forEach(ThrowingConsumer.of(Example::methodThatCanThrowCheckedException));
+	 * 
* @param the type of the input to the operation * @param consumer the source consumer * @return a new {@link ThrowingConsumer} instance @@ -108,6 +115,13 @@ static ThrowingConsumer of(ThrowingConsumer consumer) { * Lambda friendly convenience method that can be used to create a * {@link ThrowingConsumer} where the {@link #accept(Object)} method wraps * any thrown checked exceptions using the given {@code exceptionWrapper}. + *

This method can be especially useful when working with method references. + * It allows you to easily convert a method that throws a checked exception + * into an instance compatible with a regular {@link Consumer}. + *

For example: + *

+	 * list.forEach(ThrowingConsumer.of(Example::methodThatCanThrowCheckedException, IllegalStateException::new));
+	 * 
* @param the type of the input to the operation * @param consumer the source consumer * @param exceptionWrapper the exception wrapper to use diff --git a/spring-core/src/main/java/org/springframework/util/function/ThrowingFunction.java b/spring-core/src/main/java/org/springframework/util/function/ThrowingFunction.java index 5d68f4e0f346..5759ac22bfff 100644 --- a/spring-core/src/main/java/org/springframework/util/function/ThrowingFunction.java +++ b/spring-core/src/main/java/org/springframework/util/function/ThrowingFunction.java @@ -99,6 +99,13 @@ public R apply(T t) { * {@link ThrowingFunction} where the {@link #apply(Object)} method wraps * any checked exception thrown by the supplied lambda expression or method * reference. + *

This method can be especially useful when working with method references. + * It allows you to easily convert a method that throws a checked exception + * into an instance compatible with a regular {@link Function}. + *

For example: + *

+	 * stream.map(ThrowingFunction.of(Example::methodThatCanThrowCheckedException));
+	 * 
* @param the type of the input to the function * @param the type of the result of the function * @param function the source function @@ -112,6 +119,13 @@ static ThrowingFunction of(ThrowingFunction function) { * Lambda friendly convenience method that can be used to create a * {@link ThrowingFunction} where the {@link #apply(Object)} method wraps * any thrown checked exceptions using the given {@code exceptionWrapper}. + *

This method can be especially useful when working with method references. + * It allows you to easily convert a method that throws a checked exception + * into an instance compatible with a regular {@link Function}. + *

For example: + *

+	 * stream.map(ThrowingFunction.of(Example::methodThatCanThrowCheckedException, IllegalStateException::new));
+	 * 
* @param the type of the input to the function * @param the type of the result of the function * @param function the source function diff --git a/spring-core/src/main/java/org/springframework/util/function/ThrowingSupplier.java b/spring-core/src/main/java/org/springframework/util/function/ThrowingSupplier.java index 89887fbd4b30..e740c0aabb09 100644 --- a/spring-core/src/main/java/org/springframework/util/function/ThrowingSupplier.java +++ b/spring-core/src/main/java/org/springframework/util/function/ThrowingSupplier.java @@ -94,6 +94,13 @@ public T get() { * Lambda friendly convenience method that can be used to create a * {@link ThrowingSupplier} where the {@link #get()} method wraps any checked * exception thrown by the supplied lambda expression or method reference. + *

This method can be especially useful when working with method references. + * It allows you to easily convert a method that throws a checked exception + * into an instance compatible with a regular {@link Supplier}. + *

For example: + *

+	 * optional.orElseGet(ThrowingSupplier.of(Example::methodThatCanThrowCheckedException));
+	 * 
* @param the type of results supplied by this supplier * @param supplier the source supplier * @return a new {@link ThrowingSupplier} instance @@ -106,6 +113,13 @@ static ThrowingSupplier of(ThrowingSupplier supplier) { * Lambda friendly convenience method that can be used to create * {@link ThrowingSupplier} where the {@link #get()} method wraps any * thrown checked exceptions using the given {@code exceptionWrapper}. + *

This method can be especially useful when working with method references. + * It allows you to easily convert a method that throws a checked exception + * into an instance compatible with a regular {@link Supplier}. + *

For example: + *

+	 * optional.orElseGet(ThrowingSupplier.of(Example::methodThatCanThrowCheckedException, IllegalStateException::new));
+	 * 
* @param the type of results supplied by this supplier * @param supplier the source supplier * @param exceptionWrapper the exception wrapper to use