changeset 6375:fdf914a9f584

- StreamShape is now an interface. - StreamShapeFactory is responsible for creating default implementations. This is the place where we could switch on plugability using ServiceLoader (and then we have to starting thinking about class loaders :-( ). - Two StreamShapes instances are considered the same if their stream types are the same.
author psandoz
date Tue, 06 Nov 2012 11:02:38 -0800
parents 425d175eca55
children e045cd5ee294
files src/share/classes/java/util/streams/AbstractPipeline.java src/share/classes/java/util/streams/BaseStream.java src/share/classes/java/util/streams/ReferencePipeline.java src/share/classes/java/util/streams/Stream.java src/share/classes/java/util/streams/StreamShape.java src/share/classes/java/util/streams/StreamShapeFactory.java src/share/classes/java/util/streams/Streams.java src/share/classes/java/util/streams/ValuePipeline.java src/share/classes/java/util/streams/ops/ForEachOp.java src/share/classes/java/util/streams/ops/IntermediateOp.java src/share/classes/java/util/streams/ops/MatchOp.java src/share/classes/java/util/streams/ops/SortedOp.java src/share/classes/java/util/streams/ops/StatefulOp.java src/share/classes/java/util/streams/ops/StreamOp.java test-ng/tests/org/openjdk/tests/java/util/streams/ops/FlagOpTest.java test-ng/tests/org/openjdk/tests/java/util/streams/ops/StreamOpTestCase.java test-ng/tests/org/openjdk/tests/java/util/streams/ops/ToArrayOpTest.java test-ng/tests/org/openjdk/tests/java/util/streams/ops/UnorderedStreamTest.java
diffstat 18 files changed, 351 insertions(+), 255 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/streams/AbstractPipeline.java	Mon Nov 05 16:10:08 2012 -0800
+++ b/src/share/classes/java/util/streams/AbstractPipeline.java	Tue Nov 06 11:02:38 2012 -0800
@@ -75,11 +75,13 @@
  * @param <E_OUT> Type of output elements.
  * @author Brian Goetz
  */
-public abstract class AbstractPipeline<E_IN, E_OUT> {
+public abstract class AbstractPipeline<E_IN, E_OUT> implements BaseStream<E_OUT> {
     protected final AbstractPipeline<?, E_IN> upstream;
     protected final IntermediateOp<E_IN, E_OUT> op;
+    protected final int depth;
+
+    // Head state of pipeline
     protected final Spliterator<?> spliterator;
-    protected final int depth;
     protected final int sourceFlags;
     protected final StreamShape shape;
 
@@ -98,8 +100,9 @@
     protected AbstractPipeline(Spliterator<?> spliterator, int sourceFlags, StreamShape shape) {
         this.upstream = null;
         this.op = null;
+        this.depth = 0;
+
         this.spliterator = Objects.requireNonNull(spliterator);
-        this.depth = 0;
         this.sourceFlags = sourceFlags;
         this.shape = shape;
     }
@@ -113,18 +116,16 @@
     protected AbstractPipeline(AbstractPipeline<?, E_IN> upstream, IntermediateOp<E_IN, E_OUT> op) {
         this.upstream = Objects.requireNonNull(upstream);
         this.op = Objects.requireNonNull(op);
+        this.depth = upstream.depth + 1;
+
         this.spliterator = upstream.spliterator;
-        this.depth = upstream.depth + 1;
         this.sourceFlags = upstream.sourceFlags;
         this.shape = upstream.shape;
-        assert upstream.getShape() == op.inputShape();
+
+        assert upstream.getShape().getStreamType() == op.inputShape().getStreamType();
         assert (upstream.depth == 0) ^ (upstream.op != null);
     }
 
-    protected<E_NEXT> Stream<E_NEXT> chainValue(IntermediateOp<E_OUT, E_NEXT> op) {
-        return new ValuePipeline<>(this, op);
-    }
-
     protected<R> R evaluate(TerminalOp<E_OUT, R> terminal) {
         // @@@ NYI If the source size estimate is small, don't bother going parallel
         if (StreamOpFlags.PARALLEL.isKnown(sourceFlags)) {
@@ -155,13 +156,13 @@
                 upToOp++;
 
             if (upToOp < ops.length) {
-                StatefulOp<?, ?> op = (StatefulOp) ops[upToOp];
-                iNode = evaluateParallel(iNode, iSource, iSourceFlags, opsFlags, ops, fromOp, upToOp, op);
+                IntermediateOp<?, ?> statefulOp = ops[upToOp];
+                iNode = evaluateParallel(iNode, iSource, iSourceFlags, opsFlags, ops, fromOp, upToOp, statefulOp);
 
                 // Get the combined stream and ops flags for the stateful op
                 int sourceAndOpsFlags = StreamOpFlags.combineStreamFlags(
                         iSourceFlags,
-                        StreamOpFlags.combineOpFlags(op.getOpFlags(), opsFlags[upToOp]));
+                        StreamOpFlags.combineOpFlags(statefulOp.getOpFlags(), opsFlags[upToOp]));
                 // Get the source flags for the intermediate stream
                 iSourceFlags = StreamOpFlags.flagsToStreamFlags(sourceAndOpsFlags);
                 // Create stream accessor from node using the stream flags
@@ -470,6 +471,24 @@
         return iterator;
     }
 
+    public StreamShape getInputShape() {
+        return op == null ? shape : op.inputShape();
+    }
+
+    // Chaining methods
+
+    @SuppressWarnings("unchecked")
+    public <E_NEXT, S extends BaseStream<E_NEXT>> S pipeline(IntermediateOp<E_OUT, E_NEXT> op) {
+        return (S) op.outputShape().chain(this, op);
+    }
+
+    public <R> R pipeline(TerminalOp<E_OUT, R> terminal) {
+        assert getShape().getStreamType() == terminal.inputShape().getStreamType();
+        return evaluate(terminal);
+    }
+
+    // BaseStream
+
     public boolean isParallel() {
         return StreamOpFlags.PARALLEL.isKnown(sourceFlags);
     }
@@ -477,13 +496,4 @@
     public StreamShape getShape() {
         return op == null ? shape : op.outputShape();
     }
-
-    public StreamShape getInputShape() {
-        return shape;
-    }
-
-    public<R> R pipeline(TerminalOp<E_OUT, R> terminal) {
-        assert getShape() == terminal.inputShape();
-        return evaluate(terminal);
-    }
 }
--- a/src/share/classes/java/util/streams/BaseStream.java	Mon Nov 05 16:10:08 2012 -0800
+++ b/src/share/classes/java/util/streams/BaseStream.java	Tue Nov 06 11:02:38 2012 -0800
@@ -29,12 +29,12 @@
 /**
  * BaseStream
  *
- * @param <T_ELT> Type of stream elements.
- * @param <T_ITER> Type of stream element iterator.
+ * @param <T> Type of stream elements.
+ * @param <S> Type of stream.
  * 
  * @author Brian Goetz
  */
-public interface BaseStream<T_ELT, T_ITER extends Iterator<T_ELT>> {
+public interface BaseStream<T> {
     /**
      * Return the iterator for the elements of this stream. The same iterator
      * instance is returned for every invocation.  Once the elements of the
@@ -42,7 +42,7 @@
      *
      * @return the element iterator for this stream.
      */
-    T_ITER iterator();
+    Iterator<T> iterator();
 
     /**
      * Returns {@code true} if this stream may be split for parallel
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/share/classes/java/util/streams/ReferencePipeline.java	Tue Nov 06 11:02:38 2012 -0800
@@ -0,0 +1,179 @@
+/*
+ * 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.Collection;
+import java.util.Comparator;
+import java.util.Map;
+import java.util.Optional;
+import java.util.functions.*;
+import java.util.streams.ops.*;
+
+/**
+ * A pipeline of elements that are references to objects of type <code>T</code>.
+ *
+ * @param <T> Type of elements in the upstream source.
+ * @param <U> Type of elements in produced by this stage.
+ *
+ * @author Brian Goetz
+ */
+public class ReferencePipeline<T, U> extends AbstractPipeline<T, U> implements Stream<U>  {
+
+    public<S> ReferencePipeline(Spliterator<S> spliterator, int sourceFlags) {
+        super(spliterator, sourceFlags, StreamShapeFactory.REFERENCE);
+    }
+
+    public ReferencePipeline(AbstractPipeline<?, T> upstream, IntermediateOp<T, U> op) {
+        super(upstream, op);
+    }
+
+    @Override
+    public Stream<U> filter(Predicate<? super U> predicate) {
+        return pipeline(new FilterOp<>(predicate));
+    }
+
+    @Override
+    public <R> Stream<R> map(Mapper<? extends R, ? super U> mapper) {
+        return pipeline(new MapOp<>(mapper));
+    }
+
+    @Override
+    public <R> Stream<R> flatMap(FlatMapper<? extends R, ? super U> mapper) {
+        return pipeline(new FlatMapOp<>(mapper));
+    }
+
+    @Override
+    public Stream<U> uniqueElements() {
+        return pipeline(UniqOp.<U>singleton());
+    }
+
+    @Override
+    public Stream<U> sorted(Comparator<? super U> comparator) {
+        return pipeline(new SortedOp<>(comparator));
+    }
+
+    @Override
+    public Stream<U> cumulate(BinaryOperator<U> operator) {
+        return pipeline(new CumulateOp<>(operator));
+    }
+
+    @Override
+    public void forEach(Block<? super U> block) {
+        pipeline(ForEachOp.make(block));
+    }
+
+    @Override
+    public Stream<U> tee(Block<? super U> block) {
+        return pipeline(new TeeOp<>(block));
+    }
+
+    @Override
+    public Stream<U> limit(int limit) {
+        return pipeline(new LimitOp<U>(limit));
+    }
+
+    @Override
+    public Stream<U> skip(int n) {
+        return pipeline(new SkipOp<U>(n));
+    }
+
+    @Override
+    public Stream<U> concat(Stream<? extends U> other) {
+        return pipeline(new ConcatOp<>(other));
+    }
+
+    @Override
+    public <A extends Destination<? super U>> A into(A target) {
+        target.addAll(this);
+        return target;
+    }
+
+    @Override
+    public <K> Map<K,Collection<U>> groupBy(Mapper<? extends K, ? super U> classifier) {
+        return pipeline(new GroupByOp<>(classifier));
+    }
+
+    @Override
+    public <K, W> Map<K, W> reduceBy(Mapper<? extends K, ? super U> classifier,
+                                     Factory<W> baseFactory,
+                                     Combiner<W, W, U> reducer) {
+        return pipeline(new ReduceByOp<>(classifier, baseFactory, reducer));
+    }
+
+    @Override
+    public Object[] toArray() {
+        return pipeline(ToArrayOp.<U>singleton());
+    }
+
+    @Override
+    public boolean anyMatch(Predicate<? super U> predicate) {
+        return pipeline(MatchOp.make(predicate, MatchOp.MatchKind.ANY));
+    }
+
+    @Override
+    public boolean allMatch(Predicate<? super U> predicate) {
+        return pipeline(MatchOp.make(predicate, MatchOp.MatchKind.ALL));
+    }
+
+    @Override
+    public boolean noneMatch(Predicate<? super U> predicate) {
+        return pipeline(MatchOp.make(predicate, MatchOp.MatchKind.NONE));
+    }
+
+    @Override
+    public Optional<U> findFirst() {
+        return pipeline(FindFirstOp.<U>singleton());
+    }
+
+    @Override
+    public Optional<U> findAny() {
+        return pipeline(FindAnyOp.<U>singleton());
+    }
+
+    @Override
+    public Stream<U> sequential() {
+        return pipeline(CollectorOps.<U>sequentialCollector());
+    }
+
+    @Override
+    public Stream<U> unordered() {
+        return pipeline(new FlagDeclaringOp<U>(StreamOpFlags.NOT_ORDERED));
+    }
+
+    @Override
+    public U reduce(final U seed, final BinaryOperator<U> op) {
+        return pipeline(new FoldOp<>(seed, op, op));
+    }
+
+    @Override
+    public Optional<U> reduce(BinaryOperator<U> op) {
+        return pipeline(new SeedlessFoldOp<>(op));
+    }
+
+    @Override
+    public <V> V fold(Factory<V> baseFactory, Combiner<V, V, U> reducer, BinaryOperator<V> combiner) {
+        return pipeline(new FoldOp<>(baseFactory, reducer, combiner));
+    }
+}
--- a/src/share/classes/java/util/streams/Stream.java	Mon Nov 05 16:10:08 2012 -0800
+++ b/src/share/classes/java/util/streams/Stream.java	Tue Nov 06 11:02:38 2012 -0800
@@ -37,7 +37,7 @@
  *
  * @author Brian Goetz
  */
-public interface Stream<T> extends BaseStream<T, Iterator<T>> {
+public interface Stream<T> extends BaseStream<T> {
 
     Stream<T> filter(Predicate<? super T> predicate);
 
--- a/src/share/classes/java/util/streams/StreamShape.java	Mon Nov 05 16:10:08 2012 -0800
+++ b/src/share/classes/java/util/streams/StreamShape.java	Tue Nov 06 11:02:38 2012 -0800
@@ -31,13 +31,27 @@
 *
 * @author Brian Goetz
 */
-public enum StreamShape {
-    VALUE {
-        @Override
-        <U, V> AbstractPipeline<U, V> chain(AbstractPipeline<?, U> upstream, IntermediateOp<U, V> op) {
-            return new ValuePipeline<>(upstream, op);
-        }
-    };
+public interface StreamShape<S extends BaseStream> {
 
-    abstract<U, V> AbstractPipeline<U, V> chain(AbstractPipeline<?, U> upstream, IntermediateOp<U, V> op);
+    /**
+     *
+     * @return the stream type of this shape.
+     */
+    Class<S> getStreamType();
+
+    /**
+     * Create a new stream by chaining an intermediate operation to an upstream pipeline.
+     * <p>
+     * The output shape if the upstream pipeline must be the same as the input shape of
+     * the intermediate operation.
+     * </p>
+     * @param upstream the upstream pipeline.
+     * @param op the intermediate operation.
+     * @param <U> the type of elements output from the upstream pipeline and input to the new stream.
+     * @param <V> the type of elements output from the new stream.
+     * @return a the new stream.
+     */
+    <U, V> S chain(AbstractPipeline<?, U> upstream, IntermediateOp<U, V> op);
+
+    // @@@ methods for creating, conc'ing and collecting Nodes.
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/share/classes/java/util/streams/StreamShapeFactory.java	Tue Nov 06 11:02:38 2012 -0800
@@ -0,0 +1,79 @@
+/*
+ * 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.WeakHashMap;
+import java.util.streams.ops.IntermediateOp;
+
+public final class StreamShapeFactory {
+
+    private static final StreamShapeFactory FACTORY = new StreamShapeFactory();
+
+    public static final StreamShape<Stream> REFERENCE = lookup(Stream.class);
+
+    private final WeakHashMap<Class<? extends BaseStream>, StreamShape> streamShapeRegistry = new WeakHashMap<>();
+
+    private StreamShapeFactory() {
+        // Register defaults
+        StreamShape<Stream> referenceStreamShape = new StreamShape<Stream>() {
+            @Override
+            public Class<Stream> getStreamType() {
+                return Stream.class;
+            }
+
+            @Override
+            public <U, V> Stream<V> chain(AbstractPipeline<?, U> upstream, IntermediateOp<U, V> op) {
+                return new ReferencePipeline<>(upstream, op);
+            }
+        };
+
+        set(referenceStreamShape);
+    }
+
+    private void set(StreamShape<? extends BaseStream> shape) {
+        synchronized (streamShapeRegistry) {
+            if (streamShapeRegistry.containsKey(shape.getStreamType())) {
+                // @@@ Ignore or throw exception?
+                // throw new IllegalStateException(String.format("StreamShape of type %s is already registered", type.getName()));
+            }
+            else {
+                streamShapeRegistry.put(shape.getStreamType(), shape);
+            }
+        }
+    }
+
+    private <S extends BaseStream> StreamShape get(Class<? extends BaseStream> type) {
+        synchronized (streamShapeRegistry) {
+            return streamShapeRegistry.get(type);
+        }
+    }
+
+    //
+
+    @SuppressWarnings("unchecked")
+    public static <S extends BaseStream> StreamShape<S> lookup(Class<? extends BaseStream> type) {
+        return (StreamShape<S>) FACTORY.get(type);
+    }
+}
--- a/src/share/classes/java/util/streams/Streams.java	Mon Nov 05 16:10:08 2012 -0800
+++ b/src/share/classes/java/util/streams/Streams.java	Tue Nov 06 11:02:38 2012 -0800
@@ -44,7 +44,7 @@
     // Stream
 
     public static<U, T extends Sized & Iterable<U>> Stream<U> stream(T entity, int flags) {
-        return new ValuePipeline<>(new Spliterator.Sequential<U>() {
+        return new ReferencePipeline<>(new Spliterator.Sequential<U>() {
             @Override
             public Iterator<U> iterator() {
                 return entity.iterator();
@@ -63,7 +63,7 @@
     }
 
     public static<U, T extends Iterable<U>> Stream<U> stream(T entity, Sized sizeProvider, int flags) {
-        return new ValuePipeline<>(new Spliterator.Sequential<U>() {
+        return new ReferencePipeline<>(new Spliterator.Sequential<U>() {
             @Override
             public Iterator<U> iterator() {
                 return entity.iterator();
@@ -82,7 +82,7 @@
     }
 
     public static<U, T extends Iterable<U>> Stream<U> stream(T entity, int flags) {
-        return new ValuePipeline<>(new Spliterator.Sequential<U>() {
+        return new ReferencePipeline<>(new Spliterator.Sequential<U>() {
             @Override
             public Iterator<U> iterator() {
                 return entity.iterator();
@@ -97,7 +97,7 @@
     }
 
     public static<U, T extends Iterator<U>> Stream<U> stream(T iterator, int flags) {
-        return new ValuePipeline<>(new Spliterator.Sequential<U>() {
+        return new ReferencePipeline<>(new Spliterator.Sequential<U>() {
             @Override
             public Iterator<U> iterator() {
                 return iterator;
@@ -114,11 +114,11 @@
     public static<U> Stream<U> stream(Spliterator.Sequential<U> spliterator, int flags) {
         if (spliterator.getSizeIfKnown() >= 0)
             flags |= StreamOpFlags.IS_SIZED;
-        return new ValuePipeline<>(spliterator, flags);
+        return new ReferencePipeline<>(spliterator, flags);
     }
 
 //    public static <T> Stream<T> stream(ArrayProxy<T> proxy) {
-//        return new ValuePipeline<>(new ArrayProxyStreamAccessor<>(proxy));
+//        return new ReferencePipeline<>(new ArrayProxyStreamAccessor<>(proxy));
 //    }
 
     public static <T> Spliterator<T> spliterator(T[] source) {
@@ -135,7 +135,7 @@
 
     public static <T> Stream<T> stream(T[] source, int offset, int length) {
         // Note use of full-service Spliterator here -- harmless because PARALLEL flag is not set
-        return new ValuePipeline<>(new ArraySpliterator<>(source, offset, length),
+        return new ReferencePipeline<>(new ArraySpliterator<>(source, offset, length),
                                    StreamOpFlags.IS_SIZED | StreamOpFlags.IS_ORDERED);
     }
 
@@ -144,18 +144,18 @@
     }
 
     public static <T> Stream<T> parallel(T[] source, int offset, int length) {
-        return new ValuePipeline<>(new ArraySpliterator<>(source, offset, length),
+        return new ReferencePipeline<>(new ArraySpliterator<>(source, offset, length),
                                    StreamOpFlags.IS_SIZED | StreamOpFlags.IS_ORDERED | StreamOpFlags.IS_PARALLEL);
     }
 
     public static<T> Stream<T> parallel(Spliterator<T> spliterator, int flags) {
         if (spliterator.getSizeIfKnown() >= 0)
             flags |= StreamOpFlags.IS_SIZED;
-        return new ValuePipeline<>(spliterator, flags | StreamOpFlags.IS_PARALLEL);
+        return new ReferencePipeline<>(spliterator, flags | StreamOpFlags.IS_PARALLEL);
     }
 
 //    public static <T> Stream<T> parallel(ArrayProxy<T> proxy) {
-//        return new ValuePipeline<>(new ArrayProxyParallelStreamAccessor<>(proxy));
+//        return new ReferencePipeline<>(new ArrayProxyParallelStreamAccessor<>(proxy));
 //    }
 
     @SuppressWarnings("unchecked")
--- a/src/share/classes/java/util/streams/ValuePipeline.java	Mon Nov 05 16:10:08 2012 -0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,184 +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;
-
-import java.util.Collection;
-import java.util.Comparator;
-import java.util.Map;
-import java.util.Optional;
-import java.util.functions.*;
-import java.util.streams.ops.*;
-
-/**
- * ValuePipeline
- *
- * @param <T> Type of elements in the upstream source.
- * @param <U> Type of elements in produced by this stage.
- *
- * @author Brian Goetz
- */
-public class ValuePipeline<T, U> extends AbstractPipeline<T,U> implements Stream<U>  {
-
-    public<S> ValuePipeline(Spliterator<S> spliterator, int sourceFlags) {
-        super(spliterator, sourceFlags, StreamShape.VALUE);
-    }
-
-    public ValuePipeline(AbstractPipeline<?, T> upstream, IntermediateOp<T, U> op) {
-        super(upstream, op);
-    }
-
-    public<V> Stream<V> pipeline(IntermediateOp<U, V> op) {
-        // @@@ delegate to shape to do instantiation
-        return new ValuePipeline<>(this, op);
-    }
-
-    @Override
-    public Stream<U> filter(Predicate<? super U> predicate) {
-        return chainValue(new FilterOp<>(predicate));
-    }
-
-    @Override
-    public <R> Stream<R> map(Mapper<? extends R, ? super U> mapper) {
-        return chainValue(new MapOp<>(mapper));
-    }
-
-    @Override
-    public <R> Stream<R> flatMap(FlatMapper<? extends R, ? super U> mapper) {
-        return chainValue(new FlatMapOp<>(mapper));
-    }
-
-    @Override
-    public Stream<U> uniqueElements() {
-        return chainValue(UniqOp.<U>singleton());
-    }
-
-    @Override
-    public Stream<U> sorted(Comparator<? super U> comparator) {
-        return chainValue(new SortedOp<>(comparator));
-    }
-
-    @Override
-    public Stream<U> cumulate(BinaryOperator<U> operator) {
-        return chainValue(new CumulateOp<>(operator));
-    }
-
-    @Override
-    public void forEach(Block<? super U> block) {
-        pipeline(ForEachOp.make(block));
-    }
-
-    @Override
-    public Stream<U> tee(Block<? super U> block) {
-        return chainValue(new TeeOp<>(block));
-    }
-
-    @Override
-    public Stream<U> limit(int limit) {
-        return chainValue(new LimitOp<U>(limit));
-    }
-
-    @Override
-    public Stream<U> skip(int n) {
-        return chainValue(new SkipOp<U>(n));
-    }
-
-    @Override
-    public Stream<U> concat(Stream<? extends U> other) {
-        return chainValue(new ConcatOp<>(other));
-    }
-
-    @Override
-    public <A extends Destination<? super U>> A into(A target) {
-        target.addAll(this);
-        return target;
-    }
-
-    @Override
-    public <K> Map<K,Collection<U>> groupBy(Mapper<? extends K, ? super U> classifier) {
-        return pipeline(new GroupByOp<>(classifier));
-    }
-
-    @Override
-    public <K, W> Map<K, W> reduceBy(Mapper<? extends K, ? super U> classifier,
-                                     Factory<W> baseFactory,
-                                     Combiner<W, W, U> reducer) {
-        return pipeline(new ReduceByOp<>(classifier, baseFactory, reducer));
-    }
-
-    @Override
-    public Object[] toArray() {
-        return pipeline(ToArrayOp.<U>singleton());
-    }
-
-    @Override
-    public boolean anyMatch(Predicate<? super U> predicate) {
-        return pipeline(MatchOp.make(predicate, MatchOp.MatchKind.ANY));
-    }
-
-    @Override
-    public boolean allMatch(Predicate<? super U> predicate) {
-        return pipeline(MatchOp.make(predicate, MatchOp.MatchKind.ALL));
-    }
-
-    @Override
-    public boolean noneMatch(Predicate<? super U> predicate) {
-        return pipeline(MatchOp.make(predicate, MatchOp.MatchKind.NONE));
-    }
-
-    @Override
-    public Optional<U> findFirst() {
-        return pipeline(FindFirstOp.<U>singleton());
-    }
-
-    @Override
-    public Optional<U> findAny() {
-        return pipeline(FindAnyOp.<U>singleton());
-    }
-
-    @Override
-    public Stream<U> sequential() {
-        return chainValue(CollectorOps.<U>sequentialCollector());
-    }
-
-    @Override
-    public Stream<U> unordered() {
-        return chainValue(new FlagDeclaringOp<U>(StreamOpFlags.NOT_ORDERED));
-    }
-
-    @Override
-    public U reduce(final U seed, final BinaryOperator<U> op) {
-        return pipeline(new FoldOp<>(seed, op, op));
-    }
-
-    @Override
-    public Optional<U> reduce(BinaryOperator<U> op) {
-        return pipeline(new SeedlessFoldOp<>(op));
-    }
-
-    @Override
-    public <V> V fold(Factory<V> baseFactory, Combiner<V, V, U> reducer, BinaryOperator<V> combiner) {
-        return pipeline(new FoldOp<>(baseFactory, reducer, combiner));
-    }
-}
--- a/src/share/classes/java/util/streams/ops/ForEachOp.java	Mon Nov 05 16:10:08 2012 -0800
+++ b/src/share/classes/java/util/streams/ops/ForEachOp.java	Tue Nov 06 11:02:38 2012 -0800
@@ -56,7 +56,7 @@
             public Void getAndClearState() {
                 return null;
             }
-        }, StreamShape.VALUE);
+        }, StreamShapeFactory.REFERENCE);
     }
 
     @Override
--- a/src/share/classes/java/util/streams/ops/IntermediateOp.java	Mon Nov 05 16:10:08 2012 -0800
+++ b/src/share/classes/java/util/streams/ops/IntermediateOp.java	Tue Nov 06 11:02:38 2012 -0800
@@ -25,9 +25,7 @@
 package java.util.streams.ops;
 
 import java.util.Iterator;
-import java.util.streams.PipelineHelper;
-import java.util.streams.Sink;
-import java.util.streams.StreamShape;
+import java.util.streams.*;
 
 /**
  * An operation performed upon elements from an input stream to produce elements to
@@ -41,6 +39,12 @@
 public interface IntermediateOp<E_IN, E_OUT> extends StreamOp<E_IN, Node<E_OUT>> {
 
     /**
+     *
+     * @return the output shape of this operation.
+     */
+    StreamShape outputShape() default { return StreamShapeFactory.REFERENCE; }
+
+    /**
      * Get the properties of the operation.
      * <p>The properties correspond to the properties the output stream is
      * known to have or is not known to have when this operation is applied, in
@@ -81,8 +85,6 @@
      */
     Sink<E_IN> wrapSink(int flags, Sink<E_OUT> sink);
 
-    StreamShape outputShape() default { return StreamShape.VALUE; }
-
     @Override
     <P_IN> Node<E_OUT> evaluateSequential(PipelineHelper<P_IN, E_IN> helper) default {
         // @@@ Can we determine the size from the pipeline and this operation?
@@ -90,4 +92,5 @@
         helper.into(wrapSink(helper.getStreamFlags(), nb));
         return nb.build();
     }
+
 }
--- a/src/share/classes/java/util/streams/ops/MatchOp.java	Mon Nov 05 16:10:08 2012 -0800
+++ b/src/share/classes/java/util/streams/ops/MatchOp.java	Tue Nov 06 11:02:38 2012 -0800
@@ -29,10 +29,7 @@
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.functions.Mapper;
 import java.util.functions.Predicate;
-import java.util.streams.ParallelPipelineHelper;
-import java.util.streams.PipelineHelper;
-import java.util.streams.Spliterator;
-import java.util.streams.StreamShape;
+import java.util.streams.*;
 
 /**
  * MatchOp
@@ -51,7 +48,7 @@
     }
 
     public static<T> MatchOp<T> make(Predicate<? super T> predicate, MatchKind matchKind) {
-        return new MatchOp<T>(matchKind, it -> matchKind.match(it, predicate), StreamShape.VALUE);
+        return new MatchOp<T>(matchKind, it -> matchKind.match(it, predicate), StreamShapeFactory.REFERENCE);
     }
 
     @Override
--- a/src/share/classes/java/util/streams/ops/SortedOp.java	Mon Nov 05 16:10:08 2012 -0800
+++ b/src/share/classes/java/util/streams/ops/SortedOp.java	Tue Nov 06 11:02:38 2012 -0800
@@ -25,7 +25,6 @@
 package java.util.streams.ops;
 
 import java.util.*;
-import java.util.streams.PipelineHelper;
 import java.util.streams.Sink;
 import java.util.streams.StreamOpFlags;
 
--- a/src/share/classes/java/util/streams/ops/StatefulOp.java	Mon Nov 05 16:10:08 2012 -0800
+++ b/src/share/classes/java/util/streams/ops/StatefulOp.java	Tue Nov 06 11:02:38 2012 -0800
@@ -1,7 +1,5 @@
 package java.util.streams.ops;
 
-import java.util.streams.PipelineHelper;
-
 /**
  * A stateful operation. State is accumulated as elements are processed.
  * <p>The parallel evaluation returns a conc-tree of output elements.</p>
--- a/src/share/classes/java/util/streams/ops/StreamOp.java	Mon Nov 05 16:10:08 2012 -0800
+++ b/src/share/classes/java/util/streams/ops/StreamOp.java	Tue Nov 06 11:02:38 2012 -0800
@@ -29,6 +29,7 @@
 import java.util.streams.ParallelPipelineHelper;
 import java.util.streams.PipelineHelper;
 import java.util.streams.StreamShape;
+import java.util.streams.StreamShapeFactory;
 
 /**
  * An Op whose results can be evaluated directly.  This includes terminal operations, as well as intermediate
@@ -40,7 +41,11 @@
  */
 public interface StreamOp<E_IN, R> {
 
-    StreamShape inputShape() default { return StreamShape.VALUE; }
+    /**
+     *
+     * @return the input shape of this operation.
+     */
+    StreamShape inputShape() default { return StreamShapeFactory.REFERENCE; }
 
     /**
      * Evaluate the result of the operation in parallel.
--- a/test-ng/tests/org/openjdk/tests/java/util/streams/ops/FlagOpTest.java	Mon Nov 05 16:10:08 2012 -0800
+++ b/test-ng/tests/org/openjdk/tests/java/util/streams/ops/FlagOpTest.java	Tue Nov 06 11:02:38 2012 -0800
@@ -29,8 +29,6 @@
 
 import java.util.*;
 import java.util.functions.Factory;
-import java.util.streams.ParallelPipelineHelper;
-import java.util.streams.PipelineHelper;
 import java.util.streams.Sink;
 import java.util.streams.StreamOpFlags;
 import java.util.streams.ops.*;
--- a/test-ng/tests/org/openjdk/tests/java/util/streams/ops/StreamOpTestCase.java	Mon Nov 05 16:10:08 2012 -0800
+++ b/test-ng/tests/org/openjdk/tests/java/util/streams/ops/StreamOpTestCase.java	Tue Nov 06 11:02:38 2012 -0800
@@ -473,19 +473,18 @@
     }
 
     @SuppressWarnings({ "rawtypes", "unchecked" })
-    private static<T> AbstractPipeline<?, T> chain(AbstractPipeline pipe, IntermediateOp<?, T> op) {
-        switch (op.outputShape()) {
-            case VALUE: return new ValuePipeline(pipe, op);
-            default: throw new IllegalStateException(op.outputShape().toString());
+    private static<T> AbstractPipeline<?, T> chain(AbstractPipeline upstream, IntermediateOp<?, T> op) {
+        BaseStream pipe = upstream.pipeline(op);
+        if (pipe instanceof AbstractPipeline) {
+            return (AbstractPipeline<?, T>) pipe;
+        } else {
+            throw new IllegalStateException("Chained pipeline is not an instance of AbstractPipeline: " + pipe);
         }
     }
 
     @SuppressWarnings({ "rawtypes", "unchecked" })
     private static<U> U chain(AbstractPipeline pipe, TerminalOp<?, U> op) {
-        switch (pipe.getShape()) {
-            case VALUE: return (U) pipe.pipeline(op);
-            default: throw new IllegalStateException(pipe.getShape().toString());
-        }
+        return (U) pipe.pipeline(op);
     }
 
     @SuppressWarnings({ "rawtypes", "unchecked" })
--- a/test-ng/tests/org/openjdk/tests/java/util/streams/ops/ToArrayOpTest.java	Mon Nov 05 16:10:08 2012 -0800
+++ b/test-ng/tests/org/openjdk/tests/java/util/streams/ops/ToArrayOpTest.java	Tue Nov 06 11:02:38 2012 -0800
@@ -32,7 +32,6 @@
 import java.util.List;
 import java.util.streams.StreamOpFlags;
 import java.util.streams.Streams;
-import java.util.streams.ValuePipeline;
 import java.util.streams.ops.*;
 
 import static org.openjdk.tests.java.util.LambdaTestHelpers.*;
--- a/test-ng/tests/org/openjdk/tests/java/util/streams/ops/UnorderedStreamTest.java	Mon Nov 05 16:10:08 2012 -0800
+++ b/test-ng/tests/org/openjdk/tests/java/util/streams/ops/UnorderedStreamTest.java	Tue Nov 06 11:02:38 2012 -0800
@@ -42,7 +42,7 @@
         s = s.unordered();
 
         @SuppressWarnings("unchecked")
-        Stream<Integer> st = new ValuePipeline<Integer, Integer>(
+        Stream<Integer> st = new ReferencePipeline<Integer, Integer>(
                 (AbstractPipeline<Integer, Integer>)s,
                 new FlagOpTest.TestFlagExpectedOp<>(0,
                                                     EnumSet.noneOf(StreamOpFlags.class),