[
https://issues.apache.org/jira/browse/LANG-1784?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Gary D. Gregory resolved LANG-1784.
-----------------------------------
Fix Version/s: 3.19.0
Resolution: Fixed
[~rich dougherty]
This is now in git master in the classes
{{org.apache.commons.lang3.function.Functions}} and
{{org.apache.commons.lang3.function.Failable}}. You are credited in
{{changes.xml}}. Please verify your use case and close.
Note that the methods are named {{applyNonNull()}} to use the same "Non" naming
as other methods like {{java.util.Objects.requireNonNull()}}.
> Add Functions|Failable methods for null-safe mapping and chaining
> -----------------------------------------------------------------
>
> Key: LANG-1784
> URL: https://issues.apache.org/jira/browse/LANG-1784
> Project: Commons Lang
> Issue Type: New Feature
> Reporter: Rich Dougherty
> Priority: Major
> Fix For: 3.19.0
>
>
> This is a ticket to track a proposed enhancement - I'll create a PR as well
> soon.
> I'd like to propose adding a few utility methods to ObjectUtils that make it
> easier to deal with sequences of operations on potentially-null values. I
> have often wanted this feature in my own programming, so I'm hoping this
> contribution might be useful to others as well!
> *The Problem*
> It's a very common pattern in Java to need to traverse a chain of method
> calls, where any link in the chain might be null. For example,
> a.getB().getC().getD().
> Handling the null checks can be verbose. Other languages have built-in
> support for this, like Kotlin's safe navigation operator (?.), which allows
> for a concise a?.b?.c?.d.
> (See: [https://en.wikipedia.org/wiki/Safe_navigation_operator])
> In Java, the two most common approaches are nested conditionals or using
> java.util.Optional.
> Using conditionals:
> {code:java}
> // Requires intermediate local variables to avoid multiple calls (e.g., to
> map.get)
> Person person = peopleMap.get(key);
> Pet pet = (person != null) ? person.getPet() : null;
> String petName = (pet != null) ? pet.getName() : null;
> {code}
> Using Optional:
> {code:java}
> // Idiomatic, but can be longer and requires intermediate object allocation
> String petName = Optional.ofNullable(peopleMap.get(key))
> .map(Person::getPet)
> .map(Pet::getName)
> .orElse(null);
> {code}
> *Proposed Solution*
> -I'll put up a PR with a candidate implementation that helps with this
> problem.- _Pull request is here:
> [https://github.com/apache/commons-lang/pull/1435]_
> The basic idea is to add methods, e.g. ObjectUtils.applyIfNotNull(...), with
> overloads for one, two, or three functions.
> Example with new method:
> {code:java}
> // Concise, single-line, and avoids extra allocations
> String petName = ObjectUtils.applyIfNotNull(peopleMap.get(key),
> Person::getPet, Pet::getName);
> {code}
> The methods check for null at each step of the function chain. If any value
> is null (either the initial input or the result of an intermediate function),
> the chain is short-circuited and null is returned immediately.
> I'll put up a PR to make this a bit clearer and to get feedback.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)