Return-Path: Delivered-To: apmail-jackrabbit-commits-archive@www.apache.org Received: (qmail 84689 invoked from network); 23 Aug 2010 15:36:35 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 23 Aug 2010 15:36:35 -0000 Received: (qmail 35706 invoked by uid 500); 23 Aug 2010 15:36:35 -0000 Delivered-To: apmail-jackrabbit-commits-archive@jackrabbit.apache.org Received: (qmail 35622 invoked by uid 500); 23 Aug 2010 15:36:35 -0000 Mailing-List: contact commits-help@jackrabbit.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@jackrabbit.apache.org Delivered-To: mailing list commits@jackrabbit.apache.org Received: (qmail 35613 invoked by uid 99); 23 Aug 2010 15:36:35 -0000 Received: from Unknown (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 23 Aug 2010 15:36:35 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 23 Aug 2010 15:36:17 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id EF27423889FA; Mon, 23 Aug 2010 15:34:58 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r988154 - in /jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/predicate: Predicates.java RowPredicate.java Date: Mon, 23 Aug 2010 15:34:58 -0000 To: commits@jackrabbit.apache.org From: jukka@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100823153458.EF27423889FA@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: jukka Date: Mon Aug 23 15:34:58 2010 New Revision: 988154 URL: http://svn.apache.org/viewvc?rev=988154&view=rev Log: JCR-2722: Add a filtered RangeIterator Add a few predicate utilities Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/predicate/Predicates.java (with props) jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/predicate/RowPredicate.java (with props) Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/predicate/Predicates.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/predicate/Predicates.java?rev=988154&view=auto ============================================================================== --- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/predicate/Predicates.java (added) +++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/predicate/Predicates.java Mon Aug 23 15:34:58 2010 @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.jackrabbit.commons.predicate; + +/** + * Static utility class to help working with {@link Predicate}s. + * + * @since Apache Jackrabbit 2.2 + */ +public class Predicates { + + /** + * Creates an AND predicate over all the given component predicates. + * All the component predicates must evaluate to true + * for the AND predicate to do so. + * + * @param predicates component predicates + * @return AND predicate + */ + public static Predicate and(final Predicate... predicates) { + return new Predicate() { + public boolean evaluate(Object object) { + for (Predicate predicate : predicates) { + if (!predicate.evaluate(object)) { + return false; + } + } + return true; + } + }; + } + + /** + * Creates an OR predicate over all the given component predicates. + * At least one of the component predicates must evaluate to + * true for the OR predicate to do so. + * + * @param predicates component predicates + * @return OR predicate + */ + public static Predicate or(final Predicate... predicates) { + return new Predicate() { + public boolean evaluate(Object object) { + for (Predicate predicate : predicates) { + if (predicate.evaluate(object)) { + return true; + } + } + return false; + } + }; + } + + /** + * Creates a NOT predicate for the given component predicate. + * The NOT predicate evaluates to true when the component + * predicate doesn't, and vice versa. + * + * @param predicate component predicate + * @return NOT predicate + */ + public static Predicate not(final Predicate predicate) { + return new Predicate() { + public boolean evaluate(Object object) { + return !predicate.evaluate(object); + } + }; + } + +} Propchange: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/predicate/Predicates.java ------------------------------------------------------------------------------ svn:eol-style = native Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/predicate/RowPredicate.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/predicate/RowPredicate.java?rev=988154&view=auto ============================================================================== --- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/predicate/RowPredicate.java (added) +++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/predicate/RowPredicate.java Mon Aug 23 15:34:58 2010 @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.jackrabbit.commons.predicate; + +import javax.jcr.Node; +import javax.jcr.RepositoryException; +import javax.jcr.query.Row; + +/** + * Predicate for checking whether a given object is a {@link Row} and + * optionally whether it contains a given selector. Subclasses can extend + * this class to provide more complex checking of the row or the selected + * node. + * + * @since Apache Jackrabbit 2.2 + */ +public class RowPredicate implements Predicate { + + /** + * Selector name, or null. + */ + private final String selectorName; + + /** + * Creates a row predicate that checks the existence of the given + * selector (if given). + * + * @param selectorName selector name, or null + */ + public RowPredicate(String selectorName) { + this.selectorName = selectorName; + } + + /** + * Creates a row predicate. + */ + public RowPredicate() { + this(null); + } + + /** + * Checks whether the given object is a {@link Row} and calls the + * protected {@link #evaluate(Row)} method to evaluate the row. + */ + public boolean evaluate(Object object) { + if (object instanceof Row) { + try { + return evaluate((Row) object); + } catch (RepositoryException e) { + throw new RuntimeException("Failed to evaluate " + object, e); + } + } else { + return false; + } + } + + /** + * Evaluates the given row. If a selector name is specified, then + * the corresponding node in this row is evaluated by calling the + * protected {@link #evaluate(Node)} method. + */ + protected boolean evaluate(Row row) throws RepositoryException { + if (selectorName != null) { + return evaluate(row.getNode(selectorName)); + } else { + return true; + } + } + + /** + * Evaluates the given node. The default implementation always + * returns true. + */ + protected boolean evaluate(Node node) throws RepositoryException { + return true; + } + +} Propchange: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/predicate/RowPredicate.java ------------------------------------------------------------------------------ svn:eol-style = native