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

Reply via email to