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:26:05 UTC

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

Author: jukka
Date: Wed May 20 13:26:05 2009
New Revision: 776691

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

Use a PropertyIterable adapter for PropertyIterators

Added:
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/PropertyIterable.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=776691&r1=776690&r2=776691&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:26:05 2009
@@ -26,6 +26,7 @@
 import javax.jcr.query.Row;
 
 import org.apache.jackrabbit.commons.iterator.NodeIterable;
+import org.apache.jackrabbit.commons.iterator.PropertyIterable;
 
 /**
  * Collection of static utility methods for use with the JCR API.
@@ -105,176 +106,121 @@
     }
 
     /**
-     * Returns the properties of the given node as an {@link Iterable}
-     * for use in a Java 5 for-each loop. The return value encapsulates
-     * the {@link Node#getProperties()} method call. Potential
-     * {@link RepositoryException}s are converted to {@link RuntimeException}s.
+     * Calls {@link Node#getProperties()} 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 PropertyIterable
      * @param node node
      * @return properties of the node
+     * @throws RepositoryException
+     *         if the {@link Node#getProperties()} call fails
      */
-    public static Iterable<Property> getProperties(final Node node) {
-        return new Iterable<Property>() {
-            @SuppressWarnings("unchecked")
-            public Iterator<Property> iterator() {
-                try {
-                    return node.getProperties();
-                } catch (RepositoryException e) {
-                    throw new RuntimeException(
-                            "Unable to access properties of " + node, e);
-                }
-            }
-        };
+    public static Iterable<Property> getProperties(Node node)
+            throws RepositoryException {
+        return new PropertyIterable(node.getProperties());
     }
 
     /**
-     * Returns matching properties of the given node as an {@link Iterable}
-     * for use in a Java 5 for-each loop. The return value encapsulates
-     * the {@link Node#getProperties(String)} method call. Potential
-     * {@link RepositoryException}s are converted to {@link RuntimeException}s.
+     * Calls {@link Node#getProperties(String)} on the given node with the
+     * given name pattern and returns the resulting {@link PropertyIterator}
+     * as an {@link Iterable<Property>} instance for use in a Java 5
+     * for-each loop.
      *
+     * @see PropertyIterable
      * @param node node
      * @param pattern property name pattern
      * @return matching properties of the node
+     * @throws RepositoryException
+     *         if the {@link Node#getProperties(String)} call fails
      */
-    public static Iterable<Property> getProperties(
-            final Node node, final String pattern) {
-        return new Iterable<Property>() {
-            @SuppressWarnings("unchecked")
-            public Iterator<Property> iterator() {
-                try {
-                    return node.getProperties(pattern);
-                } catch (RepositoryException e) {
-                    throw new RuntimeException(
-                            "Unable to access properties of " + node, e);
-                }
-            }
-        };
+    public static Iterable<Property> getProperties(Node node, String pattern)
+            throws RepositoryException {
+        return new PropertyIterable(node.getProperties(pattern));
     }
 
     /**
-     * Returns matching properties of the given node as an {@link Iterable}
-     * for use in a Java 5 for-each loop. The return value encapsulates
-     * the {@link Node#getProperty(String[])} method call. Potential
-     * {@link RepositoryException}s are converted to {@link RuntimeException}s.
+     * Calls {@link Node#getProperty(String[])} on the given node with the
+     * given name globs and returns the resulting {@link PropertyIterator}
+     * as an {@link Iterable<Property>} instance for use in a Java 5
+     * for-each loop.
      *
+     * @see PropertyIterable
      * @param node node
-     * @param globs property name globs
+     * @param pattern property name globs
      * @return matching properties of the node
+     * @throws RepositoryException
+     *         if the {@link Node#getProperty(String[])} call fails
      */
-    public static Iterable<Property> getProperties(
-            final Node node, final String[] globs) {
-        return new Iterable<Property>() {
-            @SuppressWarnings("unchecked")
-            public Iterator<Property> iterator() {
-                try {
-                    // TODO: method name will be changed in JCR 2.0
-                    return node.getProperty(globs);
-                } catch (RepositoryException e) {
-                    throw new RuntimeException(
-                            "Unable to access properties of " + node, e);
-                }
-            }
-        };
+    public static Iterable<Property> getProperties(Node node, String[] globs)
+            throws RepositoryException {
+        return new PropertyIterable(node.getProperty(globs));
     }
 
     /**
-     * Returns the references that point to the given node as an
-     * {@link Iterable} for use in a Java 5 for-each loop. The return value
-     * encapsulates the {@link Node#getReferences()} method call. Potential
-     * {@link RepositoryException}s are converted to {@link RuntimeException}s.
+     * Calls {@link Node#getReferences()} on the given node and returns the
+     * resulting {@link PropertyIterator} as an {@link Iterable<Property>}
+     * instance for use in a Java 5 for-each loop.
      *
+     * @see PropertyIterable
      * @param node reference target
      * @return references that point to the given node
+     * @throws RepositoryException
+     *         if the {@link Node#getReferences()} call fails
      */
-    public static Iterable<Property> getReferences(final Node node) {
-        return new Iterable<Property>() {
-            @SuppressWarnings("unchecked")
-            public Iterator<Property> iterator() {
-                try {
-                    return node.getReferences();
-                } catch (RepositoryException e) {
-                    throw new RuntimeException(
-                            "Unable to access references of " + node, e);
-                }
-            }
-        };
+    public static Iterable<Property> getReferences(Node node)
+            throws RepositoryException {
+        return new PropertyIterable(node.getReferences());
     }
 
     /**
-     * Returns specifically named references that point to the given node as
-     * an {@link Iterable} for use in a Java 5 for-each loop. The return value
-     * encapsulates the {@link Node#getReferences(String)} method call.
-     * Potential {@link RepositoryException}s are converted to
-     * {@link RuntimeException}s.
+     * Calls {@link Node#getReferences(String)} on the given node and returns
+     * the resulting {@link PropertyIterator} as an {@link Iterable<Property>}
+     * instance for use in a Java 5 for-each loop.
      *
+     * @see PropertyIterable
      * @param node reference target
      * @param name reference property name
      * @return references with the given name that point to the given node
+     * @throws RepositoryException
+     *         if the {@link Node#getReferences(String)} call fails
      */
-    public static Iterable<Property> getReferences(
-            final Node node, final String name) {
-        return new Iterable<Property>() {
-            @SuppressWarnings("unchecked")
-            public Iterator<Property> iterator() {
-                try {
-                    return node.getReferences(name);
-                } catch (RepositoryException e) {
-                    throw new RuntimeException(
-                            "Unable to access references of " + node, e);
-                }
-            }
-        };
+    public static Iterable<Property> getReferences(Node node, String name)
+            throws RepositoryException {
+        return new PropertyIterable(node.getReferences(name));
     }
 
     /**
-     * Returns the weak references that point to the given node as an
-     * {@link Iterable} for use in a Java 5 for-each loop. The return value
-     * encapsulates the {@link Node#getWeakReferences()} method call.
-     * Potential {@link RepositoryException}s are converted to
-     * {@link RuntimeException}s.
+     * Calls {@link Node#getWeakReferences()} on the given node and returns the
+     * resulting {@link PropertyIterator} as an {@link Iterable<Property>}
+     * instance for use in a Java 5 for-each loop.
      *
+     * @see PropertyIterable
      * @param node reference target
      * @return weak references that point to the given node
+     * @throws RepositoryException
+     *         if the {@link Node#getWeakReferences()} call fails
      */
-    public static Iterable<Property> getWeakReferences(final Node node) {
-        return new Iterable<Property>() {
-            @SuppressWarnings("unchecked")
-            public Iterator<Property> iterator() {
-                try {
-                    return node.getWeakReferences();
-                } catch (RepositoryException e) {
-                    throw new RuntimeException(
-                            "Unable to access references of " + node, e);
-                }
-            }
-        };
+    public static Iterable<Property> getWeakReferences(Node node)
+            throws RepositoryException {
+        return new PropertyIterable(node.getWeakReferences());
     }
 
     /**
-     * Returns specifically named weak references that point to the given node
-     * as an {@link Iterable} for use in a Java 5 for-each loop. The return
-     * value encapsulates the {@link Node#getWeakReferences(String)} method
-     * call. Potential {@link RepositoryException}s are converted to
-     * {@link RuntimeException}s.
+     * Calls {@link Node#getReferences(String)} on the given node and returns
+     * the resulting {@link PropertyIterator} as an {@link Iterable<Property>}
+     * instance for use in a Java 5 for-each loop.
      *
+     * @see PropertyIterable
      * @param node reference target
      * @param name reference property name
      * @return weak references with the given name that point to the given node
+     * @throws RepositoryException
+     *         if the {@link Node#getWeakReferences(String)} call fails
      */
-    public static Iterable<Property> getWeakReferences(
-            final Node node, final String name) {
-        return new Iterable<Property>() {
-            @SuppressWarnings("unchecked")
-            public Iterator<Property> iterator() {
-                try {
-                    return node.getWeakReferences(name);
-                } catch (RepositoryException e) {
-                    throw new RuntimeException(
-                            "Unable to access references of " + node, e);
-                }
-            }
-        };
+    public static Iterable<Property> getWeakReferences(Node node, String name)
+            throws RepositoryException {
+        return new PropertyIterable(node.getWeakReferences(name));
     }
 
     /**

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

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