Skip to content

Commit c49ed13

Browse files
committed
[LANG-1784] Add Functions|Failable methods for null-safe mapping and
chaining #1435 Use the same naming style of Objects.requireNonNull()
1 parent 07914b3 commit c49ed13

File tree

4 files changed

+110
-110
lines changed

4 files changed

+110
-110
lines changed

src/main/java/org/apache/commons/lang3/function/Failable.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,9 @@ public static <E extends Throwable> double applyAsDouble(final FailableDoubleBin
181181
* result of the applying function.
182182
*
183183
* <pre>{@code
184-
* Failable.applyNotNull("a", String::toUpperCase) = "A"
185-
* Failable.applyNotNull(null, String::toUpperCase) = null
186-
* Failable.applyNotNull("a", s -> null) = null
184+
* Failable.applyNonNull("a", String::toUpperCase) = "A"
185+
* Failable.applyNonNull(null, String::toUpperCase) = null
186+
* Failable.applyNonNull("a", s -> null) = null
187187
* }</pre>
188188
* <p>
189189
* Useful when working with expressions that may return {@code null} as it allows a single-line expression without using temporary local variables or
@@ -197,11 +197,11 @@ public static <E extends Throwable> double applyAsDouble(final FailableDoubleBin
197197
* @param mapper The function to apply, must not be {@code null}.
198198
* @return The result of the function (which may be {@code null}) or {@code null} if the input value is {@code null}.
199199
* @throws E Thrown by the given function.
200-
* @see #applyNotNull(Object, FailableFunction, FailableFunction)
201-
* @see #applyNotNull(Object, FailableFunction, FailableFunction, FailableFunction)
200+
* @see #applyNonNull(Object, FailableFunction, FailableFunction)
201+
* @see #applyNonNull(Object, FailableFunction, FailableFunction, FailableFunction)
202202
* @since 3.19.0
203203
*/
204-
public static <T, R, E extends Throwable> R applyNotNull(final T value, final FailableFunction<? super T, ? extends R, E> mapper) throws E {
204+
public static <T, R, E extends Throwable> R applyNonNull(final T value, final FailableFunction<? super T, ? extends R, E> mapper) throws E {
205205
return value != null ? Objects.requireNonNull(mapper, "mapper").apply(value) : null;
206206
}
207207

@@ -210,10 +210,10 @@ public static <T, R, E extends Throwable> R applyNotNull(final T value, final Fa
210210
* {@code null}, otherwise this method returns {@code null}.
211211
*
212212
* <pre>{@code
213-
* Failable.applyNotNull(" a ", String::toUpperCase, String::trim) = "A"
214-
* Failable.applyNotNull(null, String::toUpperCase, String::trim) = null
215-
* Failable.applyNotNull(" a ", s -> null, String::trim) = null
216-
* Failable.applyNotNull(" a ", String::toUpperCase, s -> null) = null
213+
* Failable.applyNonNull(" a ", String::toUpperCase, String::trim) = "A"
214+
* Failable.applyNonNull(null, String::toUpperCase, String::trim) = null
215+
* Failable.applyNonNull(" a ", s -> null, String::trim) = null
216+
* Failable.applyNonNull(" a ", String::toUpperCase, s -> null) = null
217217
* }</pre>
218218
* <p>
219219
* Useful when working with expressions that may return {@code null} as it allows a single-line expression without using temporary local variables or
@@ -231,25 +231,25 @@ public static <T, R, E extends Throwable> R applyNotNull(final T value, final Fa
231231
* @return The result of the final function (which may be {@code null}) or {@code null} if the input value or any intermediate value is {@code null}.
232232
* @throws E1 Thrown by the first function.
233233
* @throws E2 Thrown by the second function.
234-
* @see #applyNotNull(Object, FailableFunction)
235-
* @see #applyNotNull(Object, FailableFunction, FailableFunction, FailableFunction)
234+
* @see #applyNonNull(Object, FailableFunction)
235+
* @see #applyNonNull(Object, FailableFunction, FailableFunction, FailableFunction)
236236
* @since 3.19.0
237237
*/
238-
public static <T, U, R, E1 extends Throwable, E2 extends Throwable> R applyNotNull(final T value1,
238+
public static <T, U, R, E1 extends Throwable, E2 extends Throwable> R applyNonNull(final T value1,
239239
final FailableFunction<? super T, ? extends U, E1> mapper1, final FailableFunction<? super U, ? extends R, E2> mapper2) throws E1, E2 {
240-
return applyNotNull(applyNotNull(value1, mapper1), mapper2);
240+
return applyNonNull(applyNonNull(value1, mapper1), mapper2);
241241
}
242242

243243
/**
244244
* Applies values to a chain of functions, where a {@code null} can short-circuit each step. A function is only applied if the previous value is not
245245
* {@code null}, otherwise this method returns {@code null}.
246246
*
247247
* <pre>{@code
248-
* Failable.applyNotNull(" abc ", String::toUpperCase, String::trim, StringUtils::reverse) = "CBA"
249-
* Failable.applyNotNull(null, String::toUpperCase, String::trim, StringUtils::reverse) = null
250-
* Failable.applyNotNull(" abc ", s -> null, String::trim, StringUtils::reverse) = null
251-
* Failable.applyNotNull(" abc ", String::toUpperCase, s -> null, StringUtils::reverse) = null
252-
* Failable.applyNotNull(" abc ", String::toUpperCase, String::trim, s -> null) = null
248+
* Failable.applyNonNull(" abc ", String::toUpperCase, String::trim, StringUtils::reverse) = "CBA"
249+
* Failable.applyNonNull(null, String::toUpperCase, String::trim, StringUtils::reverse) = null
250+
* Failable.applyNonNull(" abc ", s -> null, String::trim, StringUtils::reverse) = null
251+
* Failable.applyNonNull(" abc ", String::toUpperCase, s -> null, StringUtils::reverse) = null
252+
* Failable.applyNonNull(" abc ", String::toUpperCase, String::trim, s -> null) = null
253253
* }</pre>
254254
* <p>
255255
* Useful when working with expressions that may return {@code null} as it allows a single-line expression without using temporary local variables or
@@ -271,14 +271,14 @@ public static <T, U, R, E1 extends Throwable, E2 extends Throwable> R applyNotNu
271271
* @throws E1 Thrown by the first function.
272272
* @throws E2 Thrown by the second function.
273273
* @throws E3 Thrown by the third function.
274-
* @see #applyNotNull(Object, FailableFunction)
275-
* @see #applyNotNull(Object, FailableFunction, FailableFunction)
274+
* @see #applyNonNull(Object, FailableFunction)
275+
* @see #applyNonNull(Object, FailableFunction, FailableFunction)
276276
* @since 3.19.0
277277
*/
278-
public static <T, U, V, R, E1 extends Throwable, E2 extends Throwable, E3 extends Throwable> R applyNotNull(final T value1,
278+
public static <T, U, V, R, E1 extends Throwable, E2 extends Throwable, E3 extends Throwable> R applyNonNull(final T value1,
279279
final FailableFunction<? super T, ? extends U, E1> mapper1, final FailableFunction<? super U, ? extends V, E2> mapper2,
280280
final FailableFunction<? super V, ? extends R, E3> mapper3) throws E1, E2, E3 {
281-
return applyNotNull(applyNotNull(applyNotNull(value1, mapper1), mapper2), mapper3);
281+
return applyNonNull(applyNonNull(applyNonNull(value1, mapper1), mapper2), mapper3);
282282
}
283283

284284
/**

src/main/java/org/apache/commons/lang3/function/Functions.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ public static <T, R> R apply(final Function<T, R> function, final T object) {
4747
* result of the applying function.
4848
*
4949
* <pre>{@code
50-
* Functions.applyNotNull("a", String::toUpperCase) = "A"
51-
* Functions.applyNotNull(null, String::toUpperCase) = null
52-
* Functions.applyNotNull("a", s -> null) = null
50+
* Functions.applyNonNull("a", String::toUpperCase) = "A"
51+
* Functions.applyNonNull(null, String::toUpperCase) = null
52+
* Functions.applyNonNull("a", s -> null) = null
5353
* }</pre>
5454
* <p>
5555
* Useful when working with expressions that may return {@code null} as it allows a single-line expression without using temporary local variables or
@@ -61,11 +61,11 @@ public static <T, R> R apply(final Function<T, R> function, final T object) {
6161
* @param value The value to apply the function to, may be {@code null}.
6262
* @param mapper The function to apply, must not be {@code null}.
6363
* @return The result of the function (which may be {@code null}) or {@code null} if the input value is {@code null}.
64-
* @see #applyNotNull(Object, Function, Function)
65-
* @see #applyNotNull(Object, Function, Function, Function)
64+
* @see #applyNonNull(Object, Function, Function)
65+
* @see #applyNonNull(Object, Function, Function, Function)
6666
* @since 3.19.0
6767
*/
68-
public static <T, R> R applyNotNull(final T value, final Function<? super T, ? extends R> mapper) {
68+
public static <T, R> R applyNonNull(final T value, final Function<? super T, ? extends R> mapper) {
6969
return value != null ? Objects.requireNonNull(mapper, "mapper").apply(value) : null;
7070
}
7171

@@ -74,10 +74,10 @@ public static <T, R> R applyNotNull(final T value, final Function<? super T, ? e
7474
* {@code null}, otherwise this method returns {@code null}.
7575
*
7676
* <pre>{@code
77-
* Functions.applyNotNull(" a ", String::toUpperCase, String::trim) = "A"
78-
* Functions.applyNotNull(null, String::toUpperCase, String::trim) = null
79-
* Functions.applyNotNull(" a ", s -> null, String::trim) = null
80-
* Functions.applyNotNull(" a ", String::toUpperCase, s -> null) = null
77+
* Functions.applyNonNull(" a ", String::toUpperCase, String::trim) = "A"
78+
* Functions.applyNonNull(null, String::toUpperCase, String::trim) = null
79+
* Functions.applyNonNull(" a ", s -> null, String::trim) = null
80+
* Functions.applyNonNull(" a ", String::toUpperCase, s -> null) = null
8181
* }</pre>
8282
* <p>
8383
* Useful when working with expressions that may return {@code null} as it allows a single-line expression without using temporary local variables or
@@ -91,24 +91,24 @@ public static <T, R> R applyNotNull(final T value, final Function<? super T, ? e
9191
* @param mapper1 The first function to apply, must not be {@code null}.
9292
* @param mapper2 The second function to apply, must not be {@code null}.
9393
* @return The result of the final function (which may be {@code null}) or {@code null} if the input value or any intermediate value is {@code null}.
94-
* @see #applyNotNull(Object, Function)
95-
* @see #applyNotNull(Object, Function, Function, Function)
94+
* @see #applyNonNull(Object, Function)
95+
* @see #applyNonNull(Object, Function, Function, Function)
9696
* @since 3.19.0
9797
*/
98-
public static <T, U, R> R applyNotNull(final T value1, final Function<? super T, ? extends U> mapper1, final Function<? super U, ? extends R> mapper2) {
99-
return applyNotNull(applyNotNull(value1, mapper1), mapper2);
98+
public static <T, U, R> R applyNonNull(final T value1, final Function<? super T, ? extends U> mapper1, final Function<? super U, ? extends R> mapper2) {
99+
return applyNonNull(applyNonNull(value1, mapper1), mapper2);
100100
}
101101

102102
/**
103103
* Applies values to a chain of functions, where a {@code null} can short-circuit each step. A function is only applied if the previous value is not
104104
* {@code null}, otherwise this method returns {@code null}.
105105
*
106106
* <pre>{@code
107-
* Functions.applyNotNull(" abc ", String::toUpperCase, String::trim, StringUtils::reverse) = "CBA"
108-
* Functions.applyNotNull(null, String::toUpperCase, String::trim, StringUtils::reverse) = null
109-
* Functions.applyNotNull(" abc ", s -> null, String::trim, StringUtils::reverse) = null
110-
* Functions.applyNotNull(" abc ", String::toUpperCase, s -> null, StringUtils::reverse) = null
111-
* Functions.applyNotNull(" abc ", String::toUpperCase, String::trim, s -> null) = null
107+
* Functions.applyNonNull(" abc ", String::toUpperCase, String::trim, StringUtils::reverse) = "CBA"
108+
* Functions.applyNonNull(null, String::toUpperCase, String::trim, StringUtils::reverse) = null
109+
* Functions.applyNonNull(" abc ", s -> null, String::trim, StringUtils::reverse) = null
110+
* Functions.applyNonNull(" abc ", String::toUpperCase, s -> null, StringUtils::reverse) = null
111+
* Functions.applyNonNull(" abc ", String::toUpperCase, String::trim, s -> null) = null
112112
* }</pre>
113113
* <p>
114114
* Useful when working with expressions that may return {@code null} as it allows a single-line expression without using temporary local variables or
@@ -124,13 +124,13 @@ public static <T, U, R> R applyNotNull(final T value1, final Function<? super T,
124124
* @param mapper2 The second function to apply, must not be {@code null}.
125125
* @param mapper3 The third function to apply, must not be {@code null}.
126126
* @return The result of the final function (which may be {@code null}) or {@code null} if the input value or any intermediate value is {@code null}.
127-
* @see #applyNotNull(Object, Function)
128-
* @see #applyNotNull(Object, Function, Function)
127+
* @see #applyNonNull(Object, Function)
128+
* @see #applyNonNull(Object, Function, Function)
129129
* @since 3.19.0
130130
*/
131-
public static <T, U, V, R> R applyNotNull(final T value1, final Function<? super T, ? extends U> mapper1, final Function<? super U, ? extends V> mapper2,
131+
public static <T, U, V, R> R applyNonNull(final T value1, final Function<? super T, ? extends U> mapper1, final Function<? super U, ? extends V> mapper2,
132132
final Function<? super V, ? extends R> mapper3) {
133-
return applyNotNull(applyNotNull(applyNotNull(value1, mapper1), mapper2), mapper3);
133+
return applyNonNull(applyNonNull(applyNonNull(value1, mapper1), mapper2), mapper3);
134134
}
135135

136136
/**

0 commit comments

Comments
 (0)