[
https://issues.apache.org/jira/browse/LANG-1733?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Jongjin Bae updated LANG-1733:
------------------------------
Description:
I have a new suggestion about null handling.
I usually check a object is null or not before using it to avoid NPE.
It is pretty obvious, but It is quite cumbersome and has some overhead.
So I want to introduce the following null-safety methods in ObjectUtils class
and make people easy to handle null without using if/else statement or Optional
class, etc.
```
public static <T, R> R applyIfNotNull(final T object, final Function<T, R>
function) {
return object != null ? function.apply(object) : null;
}
public static <T> void acceptIfNotNull(final T object, final Consumer<T>
consumer) {
if (object != null) {
consumer.accept(object);
}
}
```
What do you think about it?
If it looks good, I will implement this feature.
> `null` handling feature in ObjectUtils
> --------------------------------------
>
> Key: LANG-1733
> URL: https://issues.apache.org/jira/browse/LANG-1733
> Project: Commons Lang
> Issue Type: New Feature
> Reporter: Jongjin Bae
> Priority: Major
>
> I have a new suggestion about null handling.
> I usually check a object is null or not before using it to avoid NPE.
> It is pretty obvious, but It is quite cumbersome and has some overhead.
> So I want to introduce the following null-safety methods in ObjectUtils class
> and make people easy to handle null without using if/else statement or
> Optional class, etc.
> ```
> public static <T, R> R applyIfNotNull(final T object, final Function<T, R>
> function) {
> return object != null ? function.apply(object) : null;
> }
> public static <T> void acceptIfNotNull(final T object, final Consumer<T>
> consumer) {
> if (object != null) {
> consumer.accept(object);
> }
> }
> ```
> What do you think about it?
> If it looks good, I will implement this feature.
>
--
This message was sent by Atlassian Jira
(v8.20.10#820010)