richdougherty opened a new pull request, #1435:
URL: https://github.com/apache/commons-lang/pull/1435

   This PR contains several new proposed methods to add to `ObjectUtils` that 
support chaining functions for objects which might be null.
   
   ## 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 
`Optional`.
   
   Using conditionals:
   
   ```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;
   ```
   
   Using `Optional`:
   
   ```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);
   ```
   
   ## Proposed solution
   
   The proposed solution is to 
   
   The basic idea is to add methods, e.g. `ObjectUtils.applyIfNotNull(...)`, 
with overloads for one, two, or three functions.
   
   Example with new method:
   
   ```java
   // Concise, single-line, and avoids extra allocations
   String petName = ObjectUtils.applyIfNotNull(peopleMap.get(key), 
Person::getPet, Pet::getName);
   ```
   
   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.
   
   ## Discussion
   
   - Happy to rename these if you have preferences for something else. I tried 
a few options, e.g. `mapNonNull` or `chain`, but this is what I settled on.
   - I did write `FailableFunction` versions as well, but in the end removed 
them to keep things simple to start with.
   
   ## Checklist
   
   <!--
     Licensed to the Apache Software Foundation (ASF) under one
     or more contributor license agreements.  See the NOTICE file
     distributed with this work for additional information
     regarding copyright ownership.  The ASF licenses this file
     to you under the Apache License, Version 2.0 (the
     "License"); you may not use this file except in compliance
     with the License.  You may obtain a copy of the License at
   
       https://www.apache.org/licenses/LICENSE-2.0
   
     Unless required by applicable law or agreed to in writing,
     software distributed under the License is distributed on an
     "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     KIND, either express or implied.  See the License for the
     specific language governing permissions and limitations
     under the License.
   -->
   
   Thanks for your contribution to [Apache 
Commons](https://commons.apache.org/)! Your help is appreciated!
   
   Before you push a pull request, review this list:
   
   - [x] Read the [contribution guidelines](CONTRIBUTING.md) for this project.
   - [x] Read the [ASF Generative Tooling 
Guidance](https://www.apache.org/legal/generative-tooling.html) if you use 
Artificial Intelligence (AI).
   - [x] I used AI to create any part of, or all of, this pull request.
   - [x] Run a successful build using the default 
[Maven](https://maven.apache.org/) goal with `mvn`; that's `mvn` on the command 
line by itself.
   - [x] Write unit tests that match behavioral changes, where the tests fail 
if the changes to the runtime are not applied. This may not always be possible, 
but it is a best-practice.
   - [x] Write a pull request description that is detailed enough to understand 
what the pull request does, how, and why.
   - [x] Each commit in the pull request should have a meaningful subject line 
and body. Note that a maintainer may squash commits during the merge process.
   
   ## Comments
   
   - I've previously signed the CLA - my Apache username is 
`[email protected]`. Or you can see me on the contributor list here: 
https://commons.apache.org/proper/commons-collections/team.html
   - Generative AI was used for code review, not coding.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to