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 15:30:42 UTC

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

Author: jukka
Date: Wed May 20 13:30:42 2009
New Revision: 776693

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

Use a RowIterable adapter for RowIterators

Added:
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/RowIterable.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=776693&r1=776692&r2=776693&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 13:30:42 2009
@@ -16,17 +16,18 @@
  */
 package org.apache.jackrabbit.commons;
 
-import java.util.Iterator;
-
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
 import javax.jcr.Property;
+import javax.jcr.PropertyIterator;
 import javax.jcr.RepositoryException;
 import javax.jcr.query.QueryResult;
 import javax.jcr.query.Row;
+import javax.jcr.query.RowIterator;
 
 import org.apache.jackrabbit.commons.iterator.NodeIterable;
 import org.apache.jackrabbit.commons.iterator.PropertyIterable;
+import org.apache.jackrabbit.commons.iterator.RowIterable;
 
 /**
  * Collection of static utility methods for use with the JCR API.
@@ -224,49 +225,35 @@
     }
 
     /**
-     * Returns the nodes in the given query result as an {@link Iterable}
-     * for use in a Java 5 for-each loop. The return value encapsulates
-     * the {@link QueryResult#getNodes()} method call. Potential
-     * {@link RepositoryException}s are converted to {@link RuntimeException}s.
+     * Calls {@link QueryResult#getNodes()} on the given query result and
+     * returns the resulting {@link NodeIterator} as an {@link Iterable<Node>}
+     * instance for use in a Java 5 for-each loop.
      *
+     * @see NodeIterable
      * @param result query result
      * @return nodes in the query result
+     * @throws RepositoryException
+     *         if the {@link QueryResult#getNodes()} call fails
      */
-    public static Iterable<Node> getNodes(final QueryResult result) {
-        return new Iterable<Node>() {
-            @SuppressWarnings("unchecked")
-            public Iterator<Node> iterator() {
-                try {
-                    return result.getNodes();
-                } catch (RepositoryException e) {
-                    throw new RuntimeException(
-                            "Unable to access nodes in " + result, e);
-                }
-            }
-        };
+    public static Iterable<Node> getNodes(QueryResult result)
+            throws RepositoryException {
+        return new NodeIterable(result.getNodes());
     }
 
     /**
-     * Returns the rows in the given query result as an {@link Iterable}
-     * for use in a Java 5 for-each loop. The return value encapsulates
-     * the {@link QueryResult#getRows()} method call. Potential
-     * {@link RepositoryException}s are converted to {@link RuntimeException}s.
+     * Calls {@link QueryResult#getRows()} on the given query result and
+     * returns the resulting {@link RowIterator} as an {@link Iterable<Row>}
+     * instance for use in a Java 5 for-each loop.
      *
+     * @see RowIterable
      * @param result query result
      * @return rows in the query result
+     * @throws RepositoryException
+     *         if the {@link QueryResult#getRows()} call fails
      */
-    public static Iterable<Row> getRows(final QueryResult result) {
-        return new Iterable<Row>() {
-            @SuppressWarnings("unchecked")
-            public Iterator<Row> iterator() {
-                try {
-                    return result.getRows();
-                } catch (RepositoryException e) {
-                    throw new RuntimeException(
-                            "Unable to access rows in " + result, e);
-                }
-            }
-        };
+    public static Iterable<Row> getRows(QueryResult result)
+            throws RepositoryException {
+        return new RowIterable(result.getRows());
     }
 
 }

Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/RowIterable.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/RowIterable.java?rev=776693&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/RowIterable.java (added)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/RowIterable.java Wed May 20 13:30:42 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.query.Row;
+import javax.jcr.query.RowIterator;
+
+/**
+ * Adapter class that adapts a {@link RowIterator} instance to an
+ * {@link Iterable<Row>} instance that always returns the same underlying
+ * iterator.
+ *
+ * @since Apache Jackrabbit 2.0
+ */
+public class RowIterable implements Iterable<Row> {
+
+    /**
+     * The row iterator being adapted.
+     */
+    private final RowIterator iterator;
+
+    /**
+     * Creates an iterable adapter for the given row iterator.
+     *
+     * @param iterator the row iterator to be adapted
+     */
+    public RowIterable(RowIterator iterator) {
+        this.iterator = iterator;
+    }
+
+    /**
+     * Returns the row iterator.
+     *
+     * @return row iterator
+     */
+    @SuppressWarnings("unchecked")
+    public Iterator<Row> iterator() {
+        return iterator;
+    }
+
+}

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