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 12:32:18 UTC

svn commit: r776654 - /jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java

Author: jukka
Date: Wed May 20 10:32:17 2009
New Revision: 776654

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

Added methods for getting Iterables out of query results.

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=776654&r1=776653&r2=776654&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 10:32:17 2009
@@ -21,6 +21,8 @@
 import javax.jcr.Node;
 import javax.jcr.Property;
 import javax.jcr.RepositoryException;
+import javax.jcr.query.QueryResult;
+import javax.jcr.query.Row;
 
 /**
  * Collection of static utility methods for use with the JCR API.
@@ -304,4 +306,50 @@
         };
     }
 
+    /**
+     * 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.
+     *
+     * @param result query result
+     * @return nodes in the query result
+     */
+    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);
+                }
+            }
+        };
+    }
+
+    /**
+     * 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.
+     *
+     * @param result query result
+     * @return rows in the query result
+     */
+    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);
+                }
+            }
+        };
+    }
+
 }



Re: svn commit: r776654 - /jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java

Posted by Jukka Zitting <ju...@gmail.com>.
Hi,

On Wed, May 20, 2009 at 2:22 PM, Julian Reschke <ju...@gmx.de> wrote:
> Jukka, is it really a good idea to catch the RepositoryException here,
> mapping it to an unchecked exception?

Good point. The alternative I was thinking about doing is:

    @SuppressWarnings("unchecked")
    public static Iterable<Node> getNodes(Node node)
            throws RepositoryException {
        final Iterator<Node> iterator = node.getNodes();
        return new Iterable<Node>() {
            public Iterator<Node> iterator() {
                return iterator;
            }
        };
    }

The only problem with this one is that it only ever returns the same
iterator, but I guess we could simply document that in the javadoc.

BR,

Jukka Zitting

Re: svn commit: r776654 - /jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java

Posted by Jukka Zitting <ju...@gmail.com>.
Hi,

On Wed, May 20, 2009 at 2:28 PM, Tobias Bocanegra <tr...@day.com> wrote:
> i don't now about the exact semantics of the Iterable interface - i.e.
> if a new iterator needs to be fetched for each call. but for the sake
> of the "foreach" loop, this should work.

Yep, I'll change it as suggested.

BR,

Jukka Zitting

Re: svn commit: r776654 - /jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java

Posted by Tobias Bocanegra <tr...@day.com>.
On Wed, May 20, 2009 at 2:22 PM, Julian Reschke <ju...@gmx.de> wrote:
> jukka@apache.org wrote:
>>
>> +    /**
>> +     * 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.
>> +     *
>> +     * @param result query result
>> +     * @return nodes in the query result
>> +     */
>> +    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);
>> +                }
>> +            }
>> +        };
>> +    }
>
> Jukka, is it really a good idea to catch the RepositoryException here,
> mapping it to an unchecked exception?

i don't like this either. how about:

public static Iterable<Node> getNodes(final QueryResult result) throws
RepositoryException {
     final Iterator iter = result.getNodes();
     return new Iterable<Node>() {
           @SuppressWarnings("unchecked")
           public Iterator<Node> iterator() {
               return iter;
           }
      };

i don't now about the exact semantics of the Iterable interface - i.e.
if a new iterator needs to be fetched for each call. but for the sake
of the "foreach" loop, this should work.

regards, toby

Re: svn commit: r776654 - /jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java

Posted by Julian Reschke <ju...@gmx.de>.
jukka@apache.org wrote:
> +    /**
> +     * 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.
> +     *
> +     * @param result query result
> +     * @return nodes in the query result
> +     */
> +    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);
> +                }
> +            }
> +        };
> +    }

Jukka, is it really a good idea to catch the RepositoryException here, 
mapping it to an unchecked exception?

BR, Julian