Return-Path: X-Original-To: apmail-jackrabbit-oak-dev-archive@minotaur.apache.org Delivered-To: apmail-jackrabbit-oak-dev-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 6683110E76 for ; Tue, 1 Oct 2013 01:57:18 +0000 (UTC) Received: (qmail 9039 invoked by uid 500); 1 Oct 2013 01:57:18 -0000 Delivered-To: apmail-jackrabbit-oak-dev-archive@jackrabbit.apache.org Received: (qmail 8995 invoked by uid 500); 1 Oct 2013 01:57:17 -0000 Mailing-List: contact oak-dev-help@jackrabbit.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: oak-dev@jackrabbit.apache.org Delivered-To: mailing list oak-dev@jackrabbit.apache.org Received: (qmail 8987 invoked by uid 99); 1 Oct 2013 01:57:17 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 01 Oct 2013 01:57:17 +0000 X-ASF-Spam-Status: No, hits=-0.7 required=5.0 tests=RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of jukka.zitting@gmail.com designates 74.125.82.175 as permitted sender) Received: from [74.125.82.175] (HELO mail-we0-f175.google.com) (74.125.82.175) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 01 Oct 2013 01:57:10 +0000 Received: by mail-we0-f175.google.com with SMTP id t61so270023wes.6 for ; Mon, 30 Sep 2013 18:56:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=s0EssBdkU08lLFXMAAblcUshVN8Vn8NRUXprXQDFb5E=; b=hwVOJPpmNt9ucwcEV2yWIITP1Eaj9qZfRpsf/7UpmcpY+h68sb0ItiCGdl0SDlEMJb 970PR6FTxwvTZqXz+xIK10EUB8ySbovRmFpr4f7AeMpSwB88sZa2V+CDxv8YPLowEryA HZkiMQv6/BVtWS6jyYA4O//H3epcpVoov4aO2VHGSeUuUN2TdaDinYIYing2Yr5KKhvu RPe9aZ0LXt95exXtamQl1UZlYKsoWmsrM1skjUM5ldLtaM7PKs/aNALxnWKkQaK0DWEL 59CNNGjq7Rlp/cKrQtGlFlla84O2mCiERPTbGSo2F9cOiIUtxw1hbDCMwERnAM0myFKE dLMg== X-Received: by 10.180.13.210 with SMTP id j18mr16298742wic.51.1380592610778; Mon, 30 Sep 2013 18:56:50 -0700 (PDT) MIME-Version: 1.0 Received: by 10.180.9.129 with HTTP; Mon, 30 Sep 2013 18:56:30 -0700 (PDT) In-Reply-To: References: From: Jukka Zitting Date: Mon, 30 Sep 2013 21:56:30 -0400 Message-ID: Subject: Re: Tree.hasChild() / Tree.getChild() To: Oak devs Content-Type: text/plain; charset=ISO-8859-1 X-Virus-Checked: Checked by ClamAV on apache.org Hi, On Mon, Sep 30, 2013 at 4:22 PM, Tobias Bocanegra 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