[
https://issues.apache.org/jira/browse/LANG-1775?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17979309#comment-17979309
]
Gary D. Gregory edited comment on LANG-1775 at 6/16/25 8:01 PM:
----------------------------------------------------------------
[~balsingh]
This duplicates existing functionality in the 'function' package.
In the future, please examine the current code base and provide unit tests for
new or updated code.
was (Author: garydgregory):
[~balsingh]
This duplicates existing functionality in the 'function' package.
> Utils for lambdas
> -----------------
>
> Key: LANG-1775
> URL: https://issues.apache.org/jira/browse/LANG-1775
> Project: Commons Lang
> Issue Type: Improvement
> Components: General
> Reporter: Baljit Singh
> Priority: Major
> Attachments: LambdaUtils.java
>
>
> Currently, there are no utilities to help facilitate usage of lambdas (such
> as the same old problem of handling nulls, etc.). I find myself frequently
> writing these utilities in my projects. I think others can benefit from them
> as well. Here are some example utils,
> public class LambdaUtils {
> /**
> * Consumes the object if not {@code null}. This is slightly more efficient
> than
> * {@code Optional.ofNullable(t).ifPresent(consumer)}.
> *
> * @param t the value to consume
> * @param consumer the consumer
> * @param <T> the type of value
> */
> public static <T> void acceptIfNotNull(
> @Nullable final T t,
> final Consumer<? super T> consumer) {
> if (t != null) {
> consumer.accept(t);
> }
> }
> /**
> * Maps the input if not {@code null}; otherwise, returns {@code null}.
> This is slightly more
> * efficient than {@code Optional.ofNullable(t).map(mapper).orElse(null)}
> *
> * @param k the input
> * @param mapper the mapper
> * @param <K> the type of input
> * @param <V> the type of mapped value
> * @return mapped value or {@code null} if the input was {@code null}
> */
> @Nullable
> public static <K, V> V mapIfNotNull(
> @Nullable final K k,
> final Function<? super K, ? extends V> mapper) {
> return k == null ? null : mapper.apply(k);
> }
> /**
> * First maps the input and then consumes the mapped value if the input
> object is not
> * {@code null}. This is a combination of {@link #acceptIfNotNull(Object,
> Consumer)} applied after
> * {@link #mapIfNotNull(Object, Function)}.
> *
> * @param k the input
> * @param mapper the mapper
> * @param consumer the consumer
> * @param <K> the type of input
> * @param <V> the type of mapped value
> */
> public static <K, V> void acceptMappedIfNotNull(
> @Nullable final K k,
> final Function<? super K, ? extends V> mapper,
> final Consumer<? super V> consumer) {
> acceptIfNotNull(mapIfNotNull(k, mapper), consumer);
> }
> /**
> * Returns a function that always returns the given value for any input.
> *
> * @param v the value to return from the function
> * @param <K> the type of key
> * @param <V> the type of value
> * @return function instances
> */
> public static <K, V> Function<K, V> constant(@Nullable final V v) {
> return k -> v;
> }
> /**
> * Returns a supplier that always returns the given value.
> *
> * @param v the value to return from the supplier
> * @param <V> the type of value
> * @return supplier instance
> */
> public static <V> Supplier<V> supply(@Nullable final V v) {
> return () -> v;
> }
> }
--
This message was sent by Atlassian Jira
(v8.20.10#820010)