Index: src/main/java/org/apache/jackrabbit/commons/JcrUtils.java =================================================================== --- src/main/java/org/apache/jackrabbit/commons/JcrUtils.java (revision 1463860) +++ src/main/java/org/apache/jackrabbit/commons/JcrUtils.java (working copy) @@ -49,9 +49,17 @@ 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 @@ } /** + * Transform any type of {@link Iterator} into an {@link Iterable} + * for single use in a Java 5 for-each loop. + *
+ * While general purpose Iterables tend to be reusable,
+ * this wrapper Iterable consumes the argument
+ * Iterator, leaving it in a non-reusable state. The returned
+ * Iterable will throw an IllegalStateException if
+ * its iterator() method is invoked a second time.
+ *
+ * @param iterator
+ * The input Iterator
+ * @return The wrapping Iterable
+ */
+ public static Iterable in(final Iterator iterator) {
+ return new Iterable() {
+ private boolean stale = false;
+
+ @Override
+ public synchronized Iterator 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}
+ * for single use in a Java 5 for-each loop.
+ *
+ * While general purpose
+ * While general purpose
+ * While general purpose
+ * While general purpose
+ * While general purpose
+ * While general purpose
+ * While general purpose
+ * While general purpose
- * By static importing
- *
- * An example:
- *
- * Iterables tend to be reusable,
+ * this wrapper Iterable consumes the argument
+ * Iterator, leaving it in a non-reusable state. The returned
+ * Iterable will throw an IllegalStateException if
+ * its iterator() method is invoked a second time.
+ *
+ * @param iterator
+ * The input Iterator
+ * @return The wrapping Iterable
+ */
+ @SuppressWarnings("unchecked")
+ public static IterableIterables tend to be reusable,
+ * this wrapper Iterable consumes the argument
+ * Iterator, leaving it in a non-reusable state. The returned
+ * Iterable will throw an IllegalStateException if
+ * its iterator() method is invoked a second time.
+ *
+ * @param iterator
+ * The input Iterator
+ * @return The wrapping Iterable
+ */
+ @SuppressWarnings("unchecked")
+ public static IterableIterables tend to be reusable,
+ * this wrapper Iterable consumes the argument
+ * Iterator, leaving it in a non-reusable state. The returned
+ * Iterable will throw an IllegalStateException if
+ * its iterator() method is invoked a second time.
+ *
+ * @param iterator
+ * The input Iterator
+ * @return The wrapping Iterable
+ */
+ @SuppressWarnings("unchecked")
+ public static IterableIterables tend to be reusable,
+ * this wrapper Iterable consumes the argument
+ * Iterator, leaving it in a non-reusable state. The returned
+ * Iterable will throw an IllegalStateException if
+ * its iterator() method is invoked a second time.
+ *
+ * @param iterator
+ * The input Iterator
+ * @return The wrapping Iterable
+ */
+ @SuppressWarnings("unchecked")
+ public static IterableIterables tend to be reusable,
+ * this wrapper Iterable consumes the argument
+ * Iterator, leaving it in a non-reusable state. The returned
+ * Iterable will throw an IllegalStateException if
+ * its iterator() method is invoked a second time.
+ *
+ * @param iterator
+ * The input Iterator
+ * @return The wrapping Iterable
+ */
+ @SuppressWarnings("unchecked")
+ public static IterableIterables tend to be reusable,
+ * this wrapper Iterable consumes the argument
+ * Iterator, leaving it in a non-reusable state. The returned
+ * Iterable will throw an IllegalStateException if
+ * its iterator() method is invoked a second time.
+ *
+ * @param iterator
+ * The input Iterator
+ * @return The wrapping Iterable
+ */
+ @SuppressWarnings("unchecked")
+ public static IterableIterables tend to be reusable,
+ * this wrapper Iterable consumes the argument
+ * Iterator, leaving it in a non-reusable state. The returned
+ * Iterable will throw an IllegalStateException if
+ * its iterator() method is invoked a second time.
+ *
+ * @param iterator
+ * The input Iterator
+ * @return The wrapping Iterable
+ */
+ @SuppressWarnings("unchecked")
+ public static IterableIterables tend to be reusable,
+ * this wrapper Iterable consumes the argument
+ * Iterator, leaving it in a non-reusable state. The returned
+ * Iterable will throw an IllegalStateException if
+ * its iterator() method is invoked a second time.
+ *
+ * @param iterator
+ * The input Iterator
+ * @return The wrapping Iterable
+ */
+ @SuppressWarnings("unchecked")
+ public static IterableIterator wrappers
- * that are contained in this package.
- * org.apache.jackrabbit.commons.iterator.Iterators.iterable, you
- * will be able to transform any {@link Iterator} from the JCR API into a
- * corresponding {@link Iterable} for use in a Java 5 foreach loop.
- *
- *
- * @since Apache Jackrabbit 2.7
- */
-public class Iterators {
-
- /**
- * Transform any type of {@link Iterator} into an {@link Iterable}
- *
- * @param iterator
- * The input
- * import static org.apache.jackrabbit.commons.iterator.Iterators.iterable;
- *
- * // And then:
- * for (Node n : iterable(parent.getNodes())) {
- * // Do stuff with n
- * }
- * Iterator
- * @return The wrapping Iterable
- */
- public static Iterable iterable(final Iterator iterator) {
- return new Iterable() {
- @Override
- public Iterator iterator() {
- return iterator;
- }
- };
- }
-
- /**
- * Transform an {@link AccessControlPolicyIterator} into an {@link Iterable}
- *
- * @param iterator
- * The input Iterator
- * @return The wrapping Iterable
- */
- @SuppressWarnings("unchecked")
- public static IterableIterator
- * @return The wrapping Iterable
- */
- @SuppressWarnings("unchecked")
- public static IterableIterator
- * @return The wrapping Iterable
- */
- @SuppressWarnings("unchecked")
- public static IterableIterator
- * @return The wrapping Iterable
- */
- public static IterableIterator
- * @return The wrapping Iterable
- */
- @SuppressWarnings("unchecked")
- public static IterableIterator
- * @return The wrapping Iterable
- */
- public static IterableIterator
- * @return The wrapping Iterable
- */
- public static IterableIterator
- * @return The wrapping Iterable
- */
- @SuppressWarnings("unchecked")
- public static Iterable