Return-Path: X-Original-To: apmail-jackrabbit-commits-archive@www.apache.org Delivered-To: apmail-jackrabbit-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 9A987F544 for ; Wed, 3 Apr 2013 07:18:38 +0000 (UTC) Received: (qmail 77550 invoked by uid 500); 3 Apr 2013 07:18:38 -0000 Delivered-To: apmail-jackrabbit-commits-archive@jackrabbit.apache.org Received: (qmail 77265 invoked by uid 500); 3 Apr 2013 07:18:29 -0000 Mailing-List: contact commits-help@jackrabbit.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@jackrabbit.apache.org Delivered-To: mailing list commits@jackrabbit.apache.org Received: (qmail 77124 invoked by uid 99); 3 Apr 2013 07:18:23 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 03 Apr 2013 07:18:23 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 03 Apr 2013 07:18:19 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 667A32388847; Wed, 3 Apr 2013 07:17:58 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1463843 - /jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/Iterators.java Date: Wed, 03 Apr 2013 07:17:58 -0000 To: commits@jackrabbit.apache.org From: jukka@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130403071758.667A32388847@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: jukka Date: Wed Apr 3 07:17:58 2013 New Revision: 1463843 URL: http://svn.apache.org/r1463843 Log: JCR-3555: Add a static utility to transform JCR Iterators into Iterables Patch by Lukas Eder Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/Iterators.java Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/Iterators.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/Iterators.java?rev=1463843&view=auto ============================================================================== --- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/Iterators.java (added) +++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/Iterators.java Wed Apr 3 07:17:58 2013 @@ -0,0 +1,170 @@ +/* + * 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.Node; +import javax.jcr.NodeIterator; +import javax.jcr.Property; +import javax.jcr.PropertyIterator; +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.Row; +import javax.jcr.query.RowIterator; +import javax.jcr.security.AccessControlPolicyIterator; +import javax.jcr.version.Version; +import javax.jcr.version.VersionIterator; + +/** + * A utility class providing static access to Iterator wrappers + * that are contained in this package. + *

+ * By static importing + * 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. + *

+ * An example: + *

+ *

+ * import static org.apache.jackrabbit.commons.iterator.Iterators.iterable;
+ *
+ * // And then:
+ * for (Node n : iterable(parent.getNodes())) {
+ *   // Do stuff with n
+ * }
+ * 
+ * + * @since Apache Jackrabbit 2.7 + */ +public class Iterators { + + /** + * Transform any type of {@link Iterator} into an {@link Iterable} + * + * @param iterator + * The input 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 Iterable iterable(AccessControlPolicyIterator iterator) { + return iterable((Iterator) iterator); + } + + /** + * Transform an {@link EventIterator} into an {@link Iterable} + * + * @param iterator + * The input Iterator + * @return The wrapping Iterable + */ + @SuppressWarnings("unchecked") + public static Iterable iterable(EventIterator iterator) { + return iterable((Iterator) iterator); + } + + /** + * Transform an {@link EventListenerIterator} into an {@link Iterable} + * + * @param iterator + * The input Iterator + * @return The wrapping Iterable + */ + @SuppressWarnings("unchecked") + public static Iterable iterable(EventListenerIterator iterator) { + return iterable((Iterator) iterator); + } + + /** + * Transform an {@link NodeIterator} into an {@link Iterable} + * + * @param iterator + * The input Iterator + * @return The wrapping Iterable + */ + public static Iterable iterable(NodeIterator iterator) { + return new NodeIterable(iterator); + } + + /** + * Transform an {@link NodeTypeIterator} into an {@link Iterable} + * + * @param iterator + * The input Iterator + * @return The wrapping Iterable + */ + @SuppressWarnings("unchecked") + public static Iterable iterable(NodeTypeIterator iterator) { + return iterable((Iterator) iterator); + } + + /** + * Transform an {@link PropertyIterator} into an {@link Iterable} + * + * @param iterator + * The input Iterator + * @return The wrapping Iterable + */ + public static Iterable iterable(PropertyIterator iterator) { + return new PropertyIterable(iterator); + } + + /** + * Transform an {@link RowIterator} into an {@link Iterable} + * + * @param iterator + * The input Iterator + * @return The wrapping Iterable + */ + public static Iterable iterable(RowIterator iterator) { + return new RowIterable(iterator); + } + + /** + * Transform an {@link VersionIterator} into an {@link Iterable} + * + * @param iterator + * The input Iterator + * @return The wrapping Iterable + */ + @SuppressWarnings("unchecked") + public static Iterable iterable(VersionIterator iterator) { + return iterable((Iterator) iterator); + } + +}