From 91c6e936ba282232626741e5d9d392138d483e92 Mon Sep 17 00:00:00 2001 From: Tim Yates Date: Thu, 14 Feb 2013 09:27:43 +0000 Subject: [PATCH] Stream now implements Iterable v0.5.4 This means it can be used more easily with javarx for example --- build.gradle | 2 +- src/main/groovy/groovy/stream/Stream.groovy | 3 ++- .../groovy/groovy/stream/StreamTests.groovy | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 4fe68b6..6229eb2 100644 --- a/build.gradle +++ b/build.gradle @@ -15,7 +15,7 @@ sourceCompatibility=1.6 targetCompatibility=1.6 group = 'com.bloidonia' -version = '0.5.3' +version = '0.5.4' repositories { mavenCentral() diff --git a/src/main/groovy/groovy/stream/Stream.groovy b/src/main/groovy/groovy/stream/Stream.groovy index f9f4c8e..8283a77 100644 --- a/src/main/groovy/groovy/stream/Stream.groovy +++ b/src/main/groovy/groovy/stream/Stream.groovy @@ -100,7 +100,7 @@ package groovy.stream * * @author Tim Yates */ -public class Stream implements StreamInterface { +public class Stream implements StreamInterface, Iterable { private static enum StreamType { MAP, OTHER } private StreamInterface wrapped private StreamType type @@ -131,6 +131,7 @@ public class Stream implements StreamInterface { */ public int getStreamIndex() { wrapped.streamIndex } + public Iterator iterator() { wrapped } /** * The starting point for a Stream taking a Map of Iterables to * lazily return. The Stream will return all combinations of this map, diff --git a/src/test/groovy/groovy/stream/StreamTests.groovy b/src/test/groovy/groovy/stream/StreamTests.groovy index 63cf8ac..6cec3a9 100644 --- a/src/test/groovy/groovy/stream/StreamTests.groovy +++ b/src/test/groovy/groovy/stream/StreamTests.groovy @@ -1,5 +1,7 @@ package groovy.stream +import spock.lang.Unroll + public class StreamTests extends spock.lang.Specification { def "test Streaming a List Stream"() { setup: @@ -10,4 +12,20 @@ public class StreamTests extends spock.lang.Specification { then: result == [ 1, 2, 3 ] } + + @Unroll("#name are both an Iterator and an Iterable") + def "iterator/iterable tests"() { + expect: + stream instanceof Iterable + stream instanceof Iterator + stream.iterator().is( stream ) + + where: + name << [ 'closure streams', 'map streams', 'range streams' ] + stream << [ + Stream.from( { x++ } ).using( [ x:1 ] ), + Stream.from( a:1..3, b:2..4 ).map { a + b }, + Stream.from( 1..4 ).filter { it % 2 == 0 } + ] + } }