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 2009/05/20 14:52:49 UTC

svn commit: r776681 - in /jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons: JcrUtils.java iterator/NodeIterable.java

Author: jukka
Date: Wed May 20 12:52:49 2009
New Revision: 776681

URL: http://svn.apache.org/viewvc?rev=776681&view=rev
Log:
JCR-2120: java.lang.Iterable support for RangeIterators

Use a NodeIterable adapter on the result of the NodeIterator return value. This way potential RepositoryExceptions are thrown directly and don't need to be encapsulated in RuntimeExceptions. And the code is cleaner!

The only downside is that the returned Iterable always returns the same Iterator from the iterator() method, but that "feature" has been documented in the NodeIterable javadoc.

Added:
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/NodeIterable.java   (with props)
Modified:
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java?rev=776681&r1=776680&r2=776681&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java (original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java Wed May 20 12:52:49 2009
@@ -19,11 +19,14 @@
 import java.util.Iterator;
 
 import javax.jcr.Node;
+import javax.jcr.NodeIterator;
 import javax.jcr.Property;
 import javax.jcr.RepositoryException;
 import javax.jcr.query.QueryResult;
 import javax.jcr.query.Row;
 
+import org.apache.jackrabbit.commons.iterator.NodeIterable;
+
 /**
  * Collection of static utility methods for use with the JCR API.
  *
@@ -38,99 +41,67 @@
     }
 
     /**
-     * Returns the nodes in the shared set of the given node as an
-     * {@link Iterable} for use in a Java 5 for-each loop. The return value
-     * encapsulates the {@link Node#getSharedSet()} method call. Potential
-     * {@link RepositoryException}s are converted to {@link RuntimeException}s.
+     * Calls {@link Node#getSharedSet()} on the given node and returns
+     * the resulting {@link NodeIterator} as an {@link Iterable<Node>} instance
+     * for use in a Java 5 for-each loop.
      *
+     * @see NodeIterable
      * @param node shared node
      * @return nodes in the shared set
+     * @throws RepositoryException if the {@link Node#getSharedSet()} call fails
      */
-    public static Iterable<Node> getSharedSet(final Node node) {
-        return new Iterable<Node>() {
-            @SuppressWarnings("unchecked")
-            public Iterator<Node> iterator() {
-                try {
-                    return node.getSharedSet();
-                } catch (RepositoryException e) {
-                    throw new RuntimeException(
-                            "Unable to access child nodes of " + node, e);
-                }
-            }
-        };
+    public static Iterable<Node> getSharedSet(Node node)
+            throws RepositoryException {
+        return new NodeIterable(node.getSharedSet());
     }
 
     /**
-     * Returns the child nodes of the given node as an {@link Iterable}
-     * for use in a Java 5 for-each loop. The return value encapsulates
-     * the {@link Node#getNodes()} method call. Potential
-     * {@link RepositoryException}s are converted to {@link RuntimeException}s.
+     * Calls {@link Node#getNodes()} on the given node and returns the
+     * resulting {@link NodeIterator} as an {@link Iterable<Node>} instance
+     * for use in a Java 5 for-each loop.
      *
+     * @see NodeIterable
      * @param node parent node
      * @return child nodes
+     * @throws RepositoryException if the {@link Node#getNodes()} call fails
      */
-    public static Iterable<Node> getChildNodes(final Node node) {
-        return new Iterable<Node>() {
-            @SuppressWarnings("unchecked")
-            public Iterator<Node> iterator() {
-                try {
-                    return node.getNodes();
-                } catch (RepositoryException e) {
-                    throw new RuntimeException(
-                            "Unable to access child nodes of " + node, e);
-                }
-            }
-        };
+    public static Iterable<Node> getChildNodes(Node node)
+            throws RepositoryException {
+        return new NodeIterable(node.getNodes());
     }
 
     /**
-     * Returns matching child nodes of the given node as an {@link Iterable}
-     * for use in a Java 5 for-each loop. The return value encapsulates
-     * the {@link Node#getNodes(String)} method call. Potential
-     * {@link RepositoryException}s are converted to {@link RuntimeException}s.
+     * Calls {@link Node#getNodes(String)} on the given node with the given
+     * name pattern and returns the resulting {@link NodeIterator} as an
+     * {@link Iterable<Node>} instance for use in a Java 5 for-each loop.
      *
+     * @see NodeIterable
      * @param node parent node
      * @param pattern node name pattern
      * @return matching child nodes
+     * @throws RepositoryException
+     *         if the {@link Node#getNodes(String)} call fails
      */
-    public static Iterable<Node> getChildNodes(
-            final Node node, final String pattern) {
-        return new Iterable<Node>() {
-            @SuppressWarnings("unchecked")
-            public Iterator<Node> iterator() {
-                try {
-                    return node.getNodes(pattern);
-                } catch (RepositoryException e) {
-                    throw new RuntimeException(
-                            "Unable to access child nodes of " + node, e);
-                }
-            }
-        };
+    public static Iterable<Node> getChildNodes(Node node, String pattern)
+            throws RepositoryException {
+        return new NodeIterable(node.getNodes(pattern));
     }
 
     /**
-     * Returns matching child nodes of the given node as an {@link Iterable}
-     * for use in a Java 5 for-each loop. The return value encapsulates
-     * the {@link Node#getNodes(String[])} method call. Potential
-     * {@link RepositoryException}s are converted to {@link RuntimeException}s.
+     * Calls {@link Node#getNodes(String[])} on the given node with the given
+     * name globs and returns the resulting {@link NodeIterator} as an
+     * {@link Iterable<Node>} instance for use in a Java 5 for-each loop.
      *
+     * @see NodeIterable
      * @param node parent node
-     * @param globs node name globs
+     * @param pattern node name pattern
      * @return matching child nodes
+     * @throws RepositoryException
+     *         if the {@link Node#getNodes(String[])} call fails
      */
-    public static Iterable<Node> getChildNodes(
-            final Node node, final String[] globs) {
-        return new Iterable<Node>() {
-            @SuppressWarnings("unchecked")
-            public Iterator<Node> iterator() {
-                try {
-                    return node.getNodes(globs);
-                } catch (RepositoryException e) {
-                    throw new RuntimeException(
-                            "Unable to access child nodes of " + node, e);
-                }
-            }
-        };
+    public static Iterable<Node> getChildNodes(Node node, String[] globs)
+            throws RepositoryException {
+        return new NodeIterable(node.getNodes(globs));
     }
 
     /**

Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/NodeIterable.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/NodeIterable.java?rev=776681&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/NodeIterable.java (added)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/NodeIterable.java Wed May 20 12:52:49 2009
@@ -0,0 +1,57 @@
+/*
+ * 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.iterator;
+
+import java.util.Iterator;
+
+import javax.jcr.Node;
+import javax.jcr.NodeIterator;
+
+/**
+ * Adapter class that adapts a {@link NodeIterator} instance to an
+ * {@link Iterable<Node>} instance that always returns the same underlying
+ * iterator.
+ *
+ * @since Apache Jackrabbit 2.0
+ */
+public class NodeIterable implements Iterable<Node> {
+
+    /**
+     * The node iterator being adapted.
+     */
+    private final NodeIterator iterator;
+
+    /**
+     * Creates an iterable adapter for the given node iterator.
+     *
+     * @param iterator the node iterator to be adapted
+     */
+    public NodeIterable(NodeIterator iterator) {
+        this.iterator = iterator;
+    }
+
+    /**
+     * Returns the node iterator.
+     *
+     * @return node iterator
+     */
+    @SuppressWarnings("unchecked")
+    public Iterator<Node> iterator() {
+        return iterator;
+    }
+
+}

Propchange: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/NodeIterable.java
------------------------------------------------------------------------------
    svn:eol-style = native