Skip to content

Commit 665deb4

Browse files
committed
Add examples to Javadoc for Throwing*.of factory methods
Closes gh-28969
1 parent 1228f1c commit 665deb4

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

spring-core/src/main/java/org/springframework/util/function/ThrowingBiFunction.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ public R apply(T t, U u) {
104104
* {@link ThrowingBiFunction} where the {@link #apply(Object, Object)}
105105
* method wraps any checked exception thrown by the supplied lambda expression
106106
* or method reference.
107+
* <p>This method can be especially useful when working with method references.
108+
* It allows you to easily convert a method that throws a checked exception
109+
* into an instance compatible with a regular {@link BiFunction}.
110+
* <p>For example:
111+
* <pre class="code">
112+
* map.replaceAll(ThrowingBiFunction.of(Example::methodThatCanThrowCheckedException));
113+
* </pre>
107114
* @param <T> the type of the first argument to the function
108115
* @param <U> the type of the second argument to the function
109116
* @param <R> the type of the result of the function
@@ -119,6 +126,13 @@ static <T, U, R> ThrowingBiFunction<T, U, R> of(ThrowingBiFunction<T, U, R> func
119126
* {@link ThrowingBiFunction} where the {@link #apply(Object, Object)}
120127
* method wraps any thrown checked exceptions using the given
121128
* {@code exceptionWrapper}.
129+
* <p>This method can be especially useful when working with method references.
130+
* It allows you to easily convert a method that throws a checked exception
131+
* into an instance compatible with a regular {@link BiFunction}.
132+
* <p>For example:
133+
* <pre class="code">
134+
* map.replaceAll(ThrowingBiFunction.of(Example::methodThatCanThrowCheckedException, IllegalStateException::new));
135+
* </pre>
122136
* @param <T> the type of the first argument to the function
123137
* @param <U> the type of the second argument to the function
124138
* @param <R> the type of the result of the function

spring-core/src/main/java/org/springframework/util/function/ThrowingConsumer.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ public void accept(T t) {
9696
* {@link ThrowingConsumer} where the {@link #accept(Object)} method wraps
9797
* any checked exception thrown by the supplied lambda expression or method
9898
* reference.
99+
* <p>This method can be especially useful when working with method references.
100+
* It allows you to easily convert a method that throws a checked exception
101+
* into an instance compatible with a regular {@link Consumer}.
102+
* <p>For example:
103+
* <pre class="code">
104+
* list.forEach(ThrowingConsumer.of(Example::methodThatCanThrowCheckedException));
105+
* </pre>
99106
* @param <T> the type of the input to the operation
100107
* @param consumer the source consumer
101108
* @return a new {@link ThrowingConsumer} instance
@@ -108,6 +115,13 @@ static <T> ThrowingConsumer<T> of(ThrowingConsumer<T> consumer) {
108115
* Lambda friendly convenience method that can be used to create a
109116
* {@link ThrowingConsumer} where the {@link #accept(Object)} method wraps
110117
* any thrown checked exceptions using the given {@code exceptionWrapper}.
118+
* <p>This method can be especially useful when working with method references.
119+
* It allows you to easily convert a method that throws a checked exception
120+
* into an instance compatible with a regular {@link Consumer}.
121+
* <p>For example:
122+
* <pre class="code">
123+
* list.forEach(ThrowingConsumer.of(Example::methodThatCanThrowCheckedException, IllegalStateException::new));
124+
* </pre>
111125
* @param <T> the type of the input to the operation
112126
* @param consumer the source consumer
113127
* @param exceptionWrapper the exception wrapper to use

spring-core/src/main/java/org/springframework/util/function/ThrowingFunction.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ public R apply(T t) {
9999
* {@link ThrowingFunction} where the {@link #apply(Object)} method wraps
100100
* any checked exception thrown by the supplied lambda expression or method
101101
* reference.
102+
* <p>This method can be especially useful when working with method references.
103+
* It allows you to easily convert a method that throws a checked exception
104+
* into an instance compatible with a regular {@link Function}.
105+
* <p>For example:
106+
* <pre class="code">
107+
* stream.map(ThrowingFunction.of(Example::methodThatCanThrowCheckedException));
108+
* </pre>
102109
* @param <T> the type of the input to the function
103110
* @param <R> the type of the result of the function
104111
* @param function the source function
@@ -112,6 +119,13 @@ static <T, R> ThrowingFunction<T, R> of(ThrowingFunction<T, R> function) {
112119
* Lambda friendly convenience method that can be used to create a
113120
* {@link ThrowingFunction} where the {@link #apply(Object)} method wraps
114121
* any thrown checked exceptions using the given {@code exceptionWrapper}.
122+
* <p>This method can be especially useful when working with method references.
123+
* It allows you to easily convert a method that throws a checked exception
124+
* into an instance compatible with a regular {@link Function}.
125+
* <p>For example:
126+
* <pre class="code">
127+
* stream.map(ThrowingFunction.of(Example::methodThatCanThrowCheckedException, IllegalStateException::new));
128+
* </pre>
115129
* @param <T> the type of the input to the function
116130
* @param <R> the type of the result of the function
117131
* @param function the source function

spring-core/src/main/java/org/springframework/util/function/ThrowingSupplier.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ public T get() {
9494
* Lambda friendly convenience method that can be used to create a
9595
* {@link ThrowingSupplier} where the {@link #get()} method wraps any checked
9696
* exception thrown by the supplied lambda expression or method reference.
97+
* <p>This method can be especially useful when working with method references.
98+
* It allows you to easily convert a method that throws a checked exception
99+
* into an instance compatible with a regular {@link Supplier}.
100+
* <p>For example:
101+
* <pre class="code">
102+
* optional.orElseGet(ThrowingSupplier.of(Example::methodThatCanThrowCheckedException));
103+
* </pre>
97104
* @param <T> the type of results supplied by this supplier
98105
* @param supplier the source supplier
99106
* @return a new {@link ThrowingSupplier} instance
@@ -106,6 +113,13 @@ static <T> ThrowingSupplier<T> of(ThrowingSupplier<T> supplier) {
106113
* Lambda friendly convenience method that can be used to create
107114
* {@link ThrowingSupplier} where the {@link #get()} method wraps any
108115
* thrown checked exceptions using the given {@code exceptionWrapper}.
116+
* <p>This method can be especially useful when working with method references.
117+
* It allows you to easily convert a method that throws a checked exception
118+
* into an instance compatible with a regular {@link Supplier}.
119+
* <p>For example:
120+
* <pre class="code">
121+
* optional.orElseGet(ThrowingSupplier.of(Example::methodThatCanThrowCheckedException, IllegalStateException::new));
122+
* </pre>
109123
* @param <T> the type of results supplied by this supplier
110124
* @param supplier the source supplier
111125
* @param exceptionWrapper the exception wrapper to use

0 commit comments

Comments
 (0)