Hi,

I would like to propose an enhancement to the ObjectUtils class in lang:

Background: I have seen multiple places in code where we have to check
if a value is null before using it in a setter or other method, like:

    if (valueX != null) {
        bean.setValue(valueX);
       someObject.compute(valueX, "bar");
    }

I was wondering if there is interest to add a method like the
following in ObjectUtils:

    public static <T> void applyIfNonNull(final Consumer<T> consumer,
final T object)
    public static <T> void applyFirstNonNull(final Consumer<T>
consumer, final T... objects)

With this the two statements above could be applied as:

    ObjectUtils.applyIfNonNull(bean::setValue, valueX);
    ObjectUtils.appyIfNonNull(v -> someObject.compute(v, "bar"), valueX);

Of course, the benefit of this increases with more such null checks we
need in the code that can be replaced by single statements.

I am happy to open a JIRA and GH pull request if there is interest in
this minor enhancement.

Bindul

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
For additional commands, e-mail: dev-h...@commons.apache.org

Reply via email to