From 87c74aaa7065ebaebaba44f42a636ba8fb302d6d Mon Sep 17 00:00:00 2001 From: kohsuke Date: Thu, 26 Feb 2009 22:10:24 +0000 Subject: [PATCH] more iterator-related convenience methods git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@15824 71c3de6d-444a-0410-be80-ed276b4c234a --- core/src/main/java/hudson/util/Iterators.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/core/src/main/java/hudson/util/Iterators.java b/core/src/main/java/hudson/util/Iterators.java index dac4ca2db9..b55fe74b8f 100644 --- a/core/src/main/java/hudson/util/Iterators.java +++ b/core/src/main/java/hudson/util/Iterators.java @@ -29,6 +29,7 @@ import java.util.List; import java.util.NoSuchElementException; import java.util.ListIterator; import java.util.AbstractList; +import java.util.Arrays; /** * Varios {@link Iterator} implementations. @@ -203,6 +204,22 @@ public class Iterators { return reverseSequence(start,end,1); } + /** + * Casts {@link Iterator} by taking advantage of its covariant-ness. + */ + @SuppressWarnings({"unchecked"}) + public static Iterator cast(Iterator itr) { + return (Iterator)itr; + } + + /** + * Casts {@link Iterable} by taking advantage of its covariant-ness. + */ + @SuppressWarnings({"unchecked"}) + public static Iterable cast(Iterable itr) { + return (Iterable)itr; + } + /** * Creates a read-only mutator that disallows {@link Iterator#remove()}. */ @@ -221,4 +238,22 @@ public class Iterators { } }; } + + /** + * Returns an {@link Iterable} that iterates over all the given {@link Iterable}s. + * + *

+ * That is, this creates {A,B,C,D} from {A,B},{C,D}. + */ + public static Iterable sequence( final Iterable... iterables ) { + return new Iterable() { + public Iterator iterator() { + return new FlattenIterator>(Arrays.asList(iterables)) { + protected Iterator expand(Iterable iterable) { + return cast(iterable).iterator(); + } + }; + } + }; + } }