If you want to use a default, I would put that in the orElse method
(unless you want to use a default intermediate result). If it's
something that needs to be calculated, use orElseGet.
On 18/07/2020 23:05, Juraj Jurčo wrote:
Hi Rob,
thanks for the proposal. Yes, you're right I can use this. I haven't seen
it before nor realize it.
It also evokes the question whether it's self explanatory. Maybe it was
only me who didn't know it, but I haven't seen either of my colleagues use
it.
I see you can use also defaults as:
Optional.ofNullable(object)
.map(ObjectType::getProperty).or(() -> Optional.of(DEFAULT_PROPERTY))
.orElse(null);
It seems to me a bit heavy, but I agree, it's possible.
So you would recommend not including it in the library?
Thanks,
Juraj+
On Sat, 18 Jul 2020 at 15:44, Rob Spoor <apa...@icemanx.nl> wrote:
Can't you just use java.util.Optional? That was added way back (ahum) in
Java 8.
Converting your example:
SubSubSubProperty value = Optional.ofNullable(object)
.map(ObjectType::getProperty)
.map(Property::getSubProperty)
.map(SubProperty::getSubSubProperty)
.map(SubSubProperty::getSubSubSubProperty)
.orElse(null);
On 18/07/2020 14:28, Juraj Jurčo wrote:
Hi guys,
I'm new in this list and I would like to contribute to Apache commons.
First of all I would like get some feedback if it makes sense to
contribute.
Java is slow in implementation of null-safe operator however sometimes
it's
really needed. So far I didn't find any implementation of this in any
library and I think it can be useful for many developers.
To the point, instead of writing something like:
if(object != null
&& object.getProperty() != null
&& object.getProperty().getSubProperty() != null
&& object.getProperty().getSubProperty().getSubSubProperty() !=
null ){
Object value =
object.getProperty().getSubProperty().getSubSubProperty().getSubSubSubProperty();
}
I would like to turn it into something like this:
Object value = ObjectUtils
.nullish(object)
.nullish(object::getProperty)
.nullish(Property::getSubProperty)
.nullish(SubProperty::getSubSubProperty)
.get(SubSubProperty::getSubSubSubProperty);
The idea is to return null in the case any property 'on the way' returns
null. I can implement it with defaults as well.
Do you have some objections to the implementation? Do you know if it
already exists somewhere?
Thanks for your feedback,
best, Juraj+
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
For additional commands, e-mail: dev-h...@commons.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
For additional commands, e-mail: dev-h...@commons.apache.org