jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1463890 - in /jackrabbit/trunk/jackrabbit-jcr-commons/src: main/java/org/apache/jackrabbit/commons/ main/java/org/apache/jackrabbit/commons/iterator/ test/java/org/apache/jackrabbit/commons/
Date Wed, 03 Apr 2013 09:29:19 GMT
Author: jukka
Date: Wed Apr  3 09:29:18 2013
New Revision: 1463890

URL: http://svn.apache.org/r1463890
Log:
JCR-3555: Add a static utility to transform JCR Iterators into Iterables

Patch by Lukas Eder

Removed:
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/Iterators.java
Modified:
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/NodeIterable.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/PropertyIterable.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/RowIterable.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/JcrUtilsTest.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=1463890&r1=1463889&r2=1463890&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 Apr  3 09:29:18 2013
@@ -49,9 +49,17 @@ import javax.jcr.RepositoryFactory;
 import javax.jcr.Session;
 import javax.jcr.Value;
 import javax.jcr.nodetype.NodeType;
+import javax.jcr.nodetype.NodeTypeIterator;
+import javax.jcr.observation.Event;
+import javax.jcr.observation.EventIterator;
+import javax.jcr.observation.EventListener;
+import javax.jcr.observation.EventListenerIterator;
 import javax.jcr.query.QueryResult;
 import javax.jcr.query.Row;
 import javax.jcr.query.RowIterator;
+import javax.jcr.security.AccessControlPolicyIterator;
+import javax.jcr.version.Version;
+import javax.jcr.version.VersionIterator;
 
 /**
  * Collection of static utility methods for use with the JCR API.
@@ -721,6 +729,188 @@ public class JcrUtils {
     }
 
     /**
+     * Transform any type of {@link Iterator} into an {@link Iterable}
+     * <strong>for single use</strong> in a Java 5 for-each loop.
+     * <p>
+     * <strong>While general purpose <code>Iterables</code> tend to be
reusable,
+     * this wrapper <code>Iterable</code> consumes the argument
+     * <code>Iterator</code>, leaving it in a non-reusable state. The returned
+     * <code>Iterable</code> will throw an <code>IllegalStateException</code>
if
+     * its <code>iterator()</code> method is invoked a second time.</strong>
+     *
+     * @param iterator
+     *            The input <code>Iterator</code>
+     * @return The wrapping <code>Iterable</code>
+     */
+    public static <I> Iterable<I> in(final Iterator<I> iterator) {
+        return new Iterable<I>() {
+            private boolean stale = false;
+
+            @Override
+            public synchronized Iterator<I> iterator() {
+                if (stale) {
+                    throw new IllegalStateException("Cannot reuse Iterable intended for single
use");
+                }
+
+                stale = true;
+                return iterator;
+            }
+        };
+    }
+
+    /**
+     * Transform an {@link AccessControlPolicyIterator} into an {@link Iterable}
+     * <strong>for single use</strong> in a Java 5 for-each loop.
+     * <p>
+     * <strong>While general purpose <code>Iterables</code> tend to be
reusable,
+     * this wrapper <code>Iterable</code> consumes the argument
+     * <code>Iterator</code>, leaving it in a non-reusable state. The returned
+     * <code>Iterable</code> will throw an <code>IllegalStateException</code>
if
+     * its <code>iterator()</code> method is invoked a second time.</strong>
+     *
+     * @param iterator
+     *            The input <code>Iterator</code>
+     * @return The wrapping <code>Iterable</code>
+     */
+    @SuppressWarnings("unchecked")
+    public static Iterable<AccessControlPolicyIterator> in(AccessControlPolicyIterator
iterator) {
+        return in((Iterator<AccessControlPolicyIterator>) iterator);
+    }
+
+    /**
+     * Transform an {@link EventIterator} into an {@link Iterable}
+     * <strong>for single use</strong> in a Java 5 for-each loop.
+     * <p>
+     * <strong>While general purpose <code>Iterables</code> tend to be
reusable,
+     * this wrapper <code>Iterable</code> consumes the argument
+     * <code>Iterator</code>, leaving it in a non-reusable state. The returned
+     * <code>Iterable</code> will throw an <code>IllegalStateException</code>
if
+     * its <code>iterator()</code> method is invoked a second time.</strong>
+     *
+     * @param iterator
+     *            The input <code>Iterator</code>
+     * @return The wrapping <code>Iterable</code>
+     */
+    @SuppressWarnings("unchecked")
+    public static Iterable<Event> in(EventIterator iterator) {
+        return in((Iterator<Event>) iterator);
+    }
+
+    /**
+     * Transform an {@link EventListenerIterator} into an {@link Iterable}
+     * <strong>for single use</strong> in a Java 5 for-each loop.
+     * <p>
+     * <strong>While general purpose <code>Iterables</code> tend to be
reusable,
+     * this wrapper <code>Iterable</code> consumes the argument
+     * <code>Iterator</code>, leaving it in a non-reusable state. The returned
+     * <code>Iterable</code> will throw an <code>IllegalStateException</code>
if
+     * its <code>iterator()</code> method is invoked a second time.</strong>
+     *
+     * @param iterator
+     *            The input <code>Iterator</code>
+     * @return The wrapping <code>Iterable</code>
+     */
+    @SuppressWarnings("unchecked")
+    public static Iterable<EventListener> in(EventListenerIterator iterator) {
+        return in((Iterator<EventListener>) iterator);
+    }
+
+    /**
+     * Transform an {@link NodeIterator} into an {@link Iterable}
+     * <strong>for single use</strong> in a Java 5 for-each loop.
+     * <p>
+     * <strong>While general purpose <code>Iterables</code> tend to be
reusable,
+     * this wrapper <code>Iterable</code> consumes the argument
+     * <code>Iterator</code>, leaving it in a non-reusable state. The returned
+     * <code>Iterable</code> will throw an <code>IllegalStateException</code>
if
+     * its <code>iterator()</code> method is invoked a second time.</strong>
+     *
+     * @param iterator
+     *            The input <code>Iterator</code>
+     * @return The wrapping <code>Iterable</code>
+     */
+    @SuppressWarnings("unchecked")
+    public static Iterable<Node> in(NodeIterator iterator) {
+        return in((Iterator<Node>) iterator);
+    }
+
+    /**
+     * Transform an {@link NodeTypeIterator} into an {@link Iterable}
+     * <strong>for single use</strong> in a Java 5 for-each loop.
+     * <p>
+     * <strong>While general purpose <code>Iterables</code> tend to be
reusable,
+     * this wrapper <code>Iterable</code> consumes the argument
+     * <code>Iterator</code>, leaving it in a non-reusable state. The returned
+     * <code>Iterable</code> will throw an <code>IllegalStateException</code>
if
+     * its <code>iterator()</code> method is invoked a second time.</strong>
+     *
+     * @param iterator
+     *            The input <code>Iterator</code>
+     * @return The wrapping <code>Iterable</code>
+     */
+    @SuppressWarnings("unchecked")
+    public static Iterable<NodeType> in(NodeTypeIterator iterator) {
+        return in((Iterator<NodeType>) iterator);
+    }
+
+    /**
+     * Transform an {@link PropertyIterator} into an {@link Iterable}
+     * <strong>for single use</strong> in a Java 5 for-each loop.
+     * <p>
+     * <strong>While general purpose <code>Iterables</code> tend to be
reusable,
+     * this wrapper <code>Iterable</code> consumes the argument
+     * <code>Iterator</code>, leaving it in a non-reusable state. The returned
+     * <code>Iterable</code> will throw an <code>IllegalStateException</code>
if
+     * its <code>iterator()</code> method is invoked a second time.</strong>
+     *
+     * @param iterator
+     *            The input <code>Iterator</code>
+     * @return The wrapping <code>Iterable</code>
+     */
+    @SuppressWarnings("unchecked")
+    public static Iterable<Property> in(PropertyIterator iterator) {
+        return in((Iterator<Property>) iterator);
+    }
+
+    /**
+     * Transform an {@link RowIterator} into an {@link Iterable}
+     * <strong>for single use</strong> in a Java 5 for-each loop.
+     * <p>
+     * <strong>While general purpose <code>Iterables</code> tend to be
reusable,
+     * this wrapper <code>Iterable</code> consumes the argument
+     * <code>Iterator</code>, leaving it in a non-reusable state. The returned
+     * <code>Iterable</code> will throw an <code>IllegalStateException</code>
if
+     * its <code>iterator()</code> method is invoked a second time.</strong>
+     *
+     * @param iterator
+     *            The input <code>Iterator</code>
+     * @return The wrapping <code>Iterable</code>
+     */
+    @SuppressWarnings("unchecked")
+    public static Iterable<Row> in(RowIterator iterator) {
+        return in((Iterator<Row>) iterator);
+    }
+
+    /**
+     * Transform an {@link VersionIterator} into an {@link Iterable}
+     * <strong>for single use</strong> in a Java 5 for-each loop.
+     * <p>
+     * <strong>While general purpose <code>Iterables</code> tend to be
reusable,
+     * this wrapper <code>Iterable</code> consumes the argument
+     * <code>Iterator</code>, leaving it in a non-reusable state. The returned
+     * <code>Iterable</code> will throw an <code>IllegalStateException</code>
if
+     * its <code>iterator()</code> method is invoked a second time.</strong>
+     *
+     * @param iterator
+     *            The input <code>Iterator</code>
+     * @return The wrapping <code>Iterable</code>
+     */
+    @SuppressWarnings("unchecked")
+    public static Iterable<Version> in(VersionIterator iterator) {
+        return in((Iterator<Version>) iterator);
+    }
+
+    /**
      * Returns the named child of the given node, creating the child if
      * it does not already exist. If the child node gets added, then its
      * type will be determined by the child node definitions associated

Modified: 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=1463890&r1=1463889&r2=1463890&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/NodeIterable.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/NodeIterable.java
Wed Apr  3 09:29:18 2013
@@ -21,13 +21,17 @@ import java.util.Iterator;
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
 
+import org.apache.jackrabbit.commons.JcrUtils;
+
 /**
  * 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
+ * @deprecated - Use {@link JcrUtils#in(NodeIterator)} instead
  */
+@Deprecated
 public class NodeIterable implements Iterable<Node> {
 
     /**

Modified: 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=1463890&r1=1463889&r2=1463890&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/PropertyIterable.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/PropertyIterable.java
Wed Apr  3 09:29:18 2013
@@ -21,13 +21,17 @@ import java.util.Iterator;
 import javax.jcr.Property;
 import javax.jcr.PropertyIterator;
 
+import org.apache.jackrabbit.commons.JcrUtils;
+
 /**
  * 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
+ * @deprecated - Use {@link JcrUtils#in(PropertyIterator)} instead
  */
+@Deprecated
 public class PropertyIterable implements Iterable<Property> {
 
     /**

Modified: 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=1463890&r1=1463889&r2=1463890&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/RowIterable.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/RowIterable.java
Wed Apr  3 09:29:18 2013
@@ -21,13 +21,17 @@ import java.util.Iterator;
 import javax.jcr.query.Row;
 import javax.jcr.query.RowIterator;
 
+import org.apache.jackrabbit.commons.JcrUtils;
+
 /**
  * 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
+ * @deprecated - Use {@link JcrUtils#in(RowIterator)} instead
  */
+@Deprecated
 public class RowIterable implements Iterable<Row> {
 
     /**

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/JcrUtilsTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/JcrUtilsTest.java?rev=1463890&r1=1463889&r2=1463890&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/JcrUtilsTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/JcrUtilsTest.java
Wed Apr  3 09:29:18 2013
@@ -16,8 +16,10 @@
  */
 package org.apache.jackrabbit.commons;
 
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Hashtable;
+import java.util.Iterator;
 import java.util.Map;
 
 import javax.jcr.PropertyType;
@@ -76,4 +78,19 @@ public class JcrUtilsTest extends MockCa
         assertEquals(PropertyType.DATE, JcrUtils.getPropertyType(
                 PropertyType.TYPENAME_DATE.toUpperCase()));
     }
+
+    public void testIn() {
+        Iterable<String> iterable = JcrUtils.in(Arrays.asList("A").iterator());
+        Iterator<String> iterator = iterable.iterator();
+        assertNotNull(iterator);
+        assertTrue(iterator.hasNext());
+        assertEquals("A", iterator.next());
+        assertFalse(iterator.hasNext());
+
+        try {
+            iterable.iterator();
+            fail("Second execution of Iterable.iterator() should throw an exception");
+        } catch (IllegalStateException expected) {
+        }
+    }
 }



Mime
View raw message