You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by ju...@apache.org on 2010/08/23 17:34:58 UTC

svn commit: r988154 - in /jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/predicate: Predicates.java RowPredicate.java

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 <code>true</code>
+     * 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
+     * <code>true</code> 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 <code>true</code> 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 <code>null</code>.
+     */
+    private final String selectorName;
+
+    /**
+     * Creates a row predicate that checks the existence of the given
+     * selector (if given).
+     *
+     * @param selectorName selector name, or <code>null</code>
+     */
+    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 <code>true</code>.
+     */
+    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