jackrabbit-oak-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jukka Zitting <jukka.zitt...@gmail.com>
Subject Re: Tree.hasChild() / Tree.getChild()
Date Tue, 01 Oct 2013 01:56:30 GMT
Hi,

On Mon, Sep 30, 2013 at 4:22 PM, Tobias Bocanegra <tripod@apache.org> wrote:
> I was looking at some spots where the code can be optimized, and I
> found some occurrences where a Tree.hasChild() is followed by a
> Tree.getChild(). I think this pattern is used commonly.

Right. I think the pattern dates back to the time before we added the
Tree.exists() method and made Tree.getChild() return non-null every
time regardless of whether the identified child exists or is
accessible.

Instead of the old pattern ...

    if (tree.hasChild(name)) {
        Tree child = tree.getChild(name);
        ...;
    } else {
        ...;
    }

... a better pattern would now be ...

    Tree child = tree.getChild(name);
    if (child.exists()) {
        ...;
    } else {
        ...;
    }

> so I think that it could make sense to add an optimizable convenience method:
> [...]
>     @Nullable
>     Tree Tree.getChild(@Nonnull String name, boolean mustExist)

I'm not sure if this helps much beyond the exists() mechanism. The
case of mustExists == false is equivalent to the existing getChild()
method, and the access pattern with the mustExist == true case would
be:

    Tree child = tree.getChild(name, true);
    if (child != null) {
        ...;
    } else {
        ...;
    }

The only difference is between "child.exists()" and "child != null",
and AFAICT any optimizations done in such an extra getChild() method
could also be used to optimize the exists() method.

BR,

Jukka Zitting

Mime
View raw message