changeset 6039:712690d71c75

Refactor Stream to extends BaseStream; Streamable now generic in stream type
author briangoetz
date Fri, 21 Sep 2012 11:58:54 -0400
parents a8f40cad0f02
children 56ec158d6c32
files src/share/classes/java/util/Arrays.java src/share/classes/java/util/Collection.java src/share/classes/java/util/Map.java src/share/classes/java/util/streams/BaseStream.java src/share/classes/java/util/streams/MapStream.java src/share/classes/java/util/streams/MapStreamable.java src/share/classes/java/util/streams/Stream.java src/share/classes/java/util/streams/StreamBuilder.java src/share/classes/java/util/streams/Streamable.java src/share/classes/java/util/streams/ops/TreeUtils.java test-ng/tests/org/openjdk/tests/java/util/streams/ops/MatchOpTest.java
diffstat 11 files changed, 62 insertions(+), 72 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/Arrays.java	Thu Sep 20 14:06:11 2012 -0400
+++ b/src/share/classes/java/util/Arrays.java	Fri Sep 21 11:58:54 2012 -0400
@@ -3768,8 +3768,8 @@
         return Streams.parallel((L) asList(array), offset, length );
     }
 
-    public static<T> Streamable<T> asStreamable(final T[] array, final int offset, final int length) {
-        return new Streamable<T>() {
+    public static<T> Streamable<Stream<T>> asStreamable(final T[] array, final int offset, final int length) {
+        return new Streamable<Stream<T>>() {
             @Override
             public Stream<T> stream() {
                 return Arrays.stream(array, offset, length);
@@ -3782,7 +3782,7 @@
         };
     }
 
-    public static<T> Streamable<T> asStreamable(T[] array) {
+    public static<T> Streamable<Stream<T>> asStreamable(T[] array) {
         return asStreamable(array, 0, array.length);
     }
 
--- a/src/share/classes/java/util/Collection.java	Thu Sep 20 14:06:11 2012 -0400
+++ b/src/share/classes/java/util/Collection.java	Fri Sep 21 11:58:54 2012 -0400
@@ -130,7 +130,7 @@
  * @since 1.2
  */
 
-public interface Collection<E> extends Sized, Streamable<E>, Traversable<E>, Stream.Destination<E> {
+public interface Collection<E> extends Sized, Streamable<Stream<E>>, Traversable<E>, Stream.Destination<E> {
     // Query Operations
 
     /**
--- a/src/share/classes/java/util/Map.java	Thu Sep 20 14:06:11 2012 -0400
+++ b/src/share/classes/java/util/Map.java	Fri Sep 21 11:58:54 2012 -0400
@@ -27,7 +27,7 @@
 
 import java.util.functions.BiBlock;
 import java.util.streams.MapStream;
-import java.util.streams.MapStreamable;
+import java.util.streams.Streamable;
 import java.util.streams.Streams;
 
 /**
@@ -119,7 +119,7 @@
  * @see Set
  * @since 1.2
  */
-public interface Map<K,V> extends Sized, MapStreamable<K,V>, MapTraversable<K,V>, MapStream.Destination<K,V> {
+public interface Map<K,V> extends Sized, Streamable<MapStream<K,V>>, MapTraversable<K,V>, MapStream.Destination<K,V> {
 
     // Query Operations
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/share/classes/java/util/streams/BaseStream.java	Fri Sep 21 11:58:54 2012 -0400
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package java.util.streams;
+
+import java.util.Iterator;
+
+/**
+ * BaseStream
+ *
+ * @author Brian Goetz
+ */
+public interface BaseStream<T_ELT, T_ITER extends Iterator<T_ELT>> {
+    /**
+     * Return the iterator for the elements of this stream. The same iterator
+     * instance is returned for every invocation.  Once the elements of the stream are
+     * consumed it is not possible to "rewind" the stream.
+     *
+     * @return the element iterator for this stream.
+     */
+    T_ITER iterator();
+
+    boolean isParallel();
+
+    StreamShape getShape();
+}
--- a/src/share/classes/java/util/streams/MapStream.java	Thu Sep 20 14:06:11 2012 -0400
+++ b/src/share/classes/java/util/streams/MapStream.java	Fri Sep 21 11:58:54 2012 -0400
@@ -38,18 +38,7 @@
  *
  * @author Brian Goetz
  */
-public interface MapStream<K, V> {
-    /**
-     * Returns the iterator for this stream. Each stream has a single iterator
-     * that is shared by all consumers. Once the elements of the stream are
-     * consumed it is not possible to "rewind" the stream.
-     * @return
-     */
-    MapIterator<K, V> iterator();
-
-    boolean isParallel();
-
-    public StreamShape getShape();
+public interface MapStream<K, V> extends BaseStream<Mapping<K, V>, MapIterator<K, V>> {
 
     /**
      * Return the mapping which is semantically "first" in the stream. If the
--- a/src/share/classes/java/util/streams/MapStreamable.java	Thu Sep 20 14:06:11 2012 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,35 +0,0 @@
-/*
- * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-package java.util.streams;
-
-/**
- * MapStreamable
- *
- * @author Brian Goetz
- */
-public interface MapStreamable<K,V> {
-    MapStream<K,V> stream();
-    MapStream<K,V> parallel() default { return stream(); }
-}
--- a/src/share/classes/java/util/streams/Stream.java	Thu Sep 20 14:06:11 2012 -0400
+++ b/src/share/classes/java/util/streams/Stream.java	Fri Sep 21 11:58:54 2012 -0400
@@ -37,7 +37,7 @@
  *
  * @author Brian Goetz
  */
-public interface Stream<T> {
+public interface Stream<T> extends BaseStream<T, Iterator<T>> {
 
     /**
      * Stream elements are distinct. No two elements contained in the stream
@@ -72,18 +72,6 @@
      */
     public static final int FLAG_UNKNOWN_MASK_V1 = ~FLAG_MASK;
 
-    /**
-     * Return the iterator for the elements of this stream. The same iterator
-     * instance is returned for every invocation.
-     *
-     * @return the element iterator for this stream.
-     */
-    Iterator<T> iterator();
-
-    boolean isParallel();
-
-    public StreamShape getShape();
-
 
     Stream<T> filter(Predicate<? super T> predicate);
 
--- a/src/share/classes/java/util/streams/StreamBuilder.java	Thu Sep 20 14:06:11 2012 -0400
+++ b/src/share/classes/java/util/streams/StreamBuilder.java	Fri Sep 21 11:58:54 2012 -0400
@@ -32,7 +32,7 @@
  *
  * @author Brian Goetz
  */
-public interface StreamBuilder<T> extends Sized, Streamable<T>, Traversable<T>, Collection<T>, Sink.OfValue<T> {
+public interface StreamBuilder<T> extends Sized, Streamable<Stream<T>>, Traversable<T>, Collection<T>, Sink.OfValue<T> {
 
     @Override
     void forEach(Block<? super T> block);
--- a/src/share/classes/java/util/streams/Streamable.java	Thu Sep 20 14:06:11 2012 -0400
+++ b/src/share/classes/java/util/streams/Streamable.java	Fri Sep 21 11:58:54 2012 -0400
@@ -31,7 +31,7 @@
  *
  * @author Brian Goetz
  */
-public interface Streamable<T> {
-    Stream<T> stream();
-    Stream<T> parallel() default { return stream(); }
+public interface Streamable<T extends BaseStream> {
+    T stream();
+    T parallel() default { return stream(); }
 }
--- a/src/share/classes/java/util/streams/ops/TreeUtils.java	Thu Sep 20 14:06:11 2012 -0400
+++ b/src/share/classes/java/util/streams/ops/TreeUtils.java	Fri Sep 21 11:58:54 2012 -0400
@@ -227,7 +227,7 @@
         }
     }
 
-    public static interface Node<T> extends Traversable<T>, Streamable<T>, Sized, Iterable<T> {
+    public static interface Node<T> extends Traversable<T>, Streamable<Stream<T>>, Sized, Iterable<T> {
         void copyTo(T[] array, int offset);
 
         Spliterator<T> spliterator();
--- a/test-ng/tests/org/openjdk/tests/java/util/streams/ops/MatchOpTest.java	Thu Sep 20 14:06:11 2012 -0400
+++ b/test-ng/tests/org/openjdk/tests/java/util/streams/ops/MatchOpTest.java	Fri Sep 21 11:58:54 2012 -0400
@@ -28,6 +28,7 @@
 import org.testng.annotations.Test;
 
 import java.util.functions.Predicate;
+import java.util.streams.Stream;
 import java.util.streams.Streamable;
 import java.util.streams.ops.MatchOp;
 import java.util.streams.ops.TerminalOp;
@@ -46,7 +47,7 @@
     private static final Predicate<Integer>[] INTEGER_PREDICATES
             = (Predicate<Integer>[]) new Predicate<?>[] { pTrue, pFalse, pEven, pOdd };
 
-    private<T> void assertPredicates(Streamable<T> source, MatchKind matchKind, Predicate<T>[] predicates, boolean... answers) {
+    private<T> void assertPredicates(Streamable<Stream<T>> source, MatchKind matchKind, Predicate<T>[] predicates, boolean... answers) {
         for (int i=0; i<predicates.length; i++) {
             boolean match;
             switch (matchKind) {