Skip to content

LANG-1435: Implemented defaultIfNull method that uses a supplier for default value#408

Closed
dekelpilli wants to merge 4 commits intoapache:masterfrom
dekelpilli:LANG-1435/supplier-default-if-null
Closed

LANG-1435: Implemented defaultIfNull method that uses a supplier for default value#408
dekelpilli wants to merge 4 commits intoapache:masterfrom
dekelpilli:LANG-1435/supplier-default-if-null

Conversation

@dekelpilli
Copy link
Copy Markdown

@dekelpilli dekelpilli commented Feb 24, 2019

From JIRA ticket:

Currently, if the default value in defaultIfNull is computationally slow (for example, it requires a database call, or complex calculations), it's not worth using the util method. This feature is to implement a method that would only perform this computationally slow action if the object value is actually null. This can be done using a Supplier.

In short, you'd call ObjectUtils.getDefaultIfNull(someNullableValue, DbUtil::doGet) instead of ObjectUtils.defaultIfNull(someNullableValue, DbUtil.doGet())

@MarkDacek
Copy link
Copy Markdown
Contributor

I'm curious whether this is worthwhile given the existence of ObjectUtils.firstNonNull. Is the supplier that much of a necessity?

The only plus I see is that it wouldn't be executed if the default isn't null. Is that a major performance increase for a significant number of use-cases?

@PascalSchumacher
Copy link
Copy Markdown
Contributor

Thanks for the pull request!

Java 11 offers Objects#requireNonNullElseGet (see https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Objects.html#requireNonNullElseGet(T,java.util.function.Supplier) for details), so I don't think we should add this.

@chtompki
Copy link
Copy Markdown
Member

chtompki commented Mar 1, 2019

Ok we’ll close this I think. Many thanks all.

@chtompki chtompki closed this Mar 1, 2019
@dekelpilli
Copy link
Copy Markdown
Author

I think it's useful, but I agree, if J11 has it then we shouldn't double up.

@mikkomaunu
Copy link
Copy Markdown
Contributor

Method suggested here serves slightly different use case and cannot always be substituted by java.lang.Objects#requireNonNullElseGet. As 'requireNonNull' in name hints, requireNonNullElseGet will throw NullPointerException if Supplier that returns null is utilized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants