# HG changeset patch # User henryjen # Date 1373312786 14400 # Node ID 06636235cd1268f0462596c46cd9a5b2f254ebd2 # Parent 9fa4af2af63db6e695aa0c57a767fba8e30cf9e8 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces Reviewed-by: psandoz, mduigou Contributed-by: brian goetz diff -r 9fa4af2af63d -r 06636235cd12 jdk/src/share/classes/java/util/stream/DoubleStream.java --- a/jdk/src/share/classes/java/util/stream/DoubleStream.java Fri Jul 12 15:01:08 2013 -0700 +++ b/jdk/src/share/classes/java/util/stream/DoubleStream.java Mon Jul 08 15:46:26 2013 -0400 @@ -662,7 +662,7 @@ * * @return a stream builder */ - public static StreamBuilder.OfDouble builder() { + public static Builder builder() { return new Streams.DoubleStreamBuilderImpl(); } @@ -766,4 +766,61 @@ a.spliterator(), b.spliterator()); return StreamSupport.doubleStream(split, a.isParallel() || b.isParallel()); } + + /** + * A mutable builder for a {@code DoubleStream}. + * + *

A stream builder has a lifecycle, where it starts in a building + * phase, during which elements can be added, and then transitions to a + * built phase, after which elements may not be added. The built phase + * begins when the {@link #build()} method is called, which creates an + * ordered stream whose elements are the elements that were added to the + * stream builder, in the order they were added. + * + * @see DoubleStream#builder() + * @since 1.8 + */ + public interface Builder extends DoubleConsumer { + + /** + * Adds an element to the stream being built. + * + * @throws IllegalStateException if the builder has already transitioned + * to the built state + */ + @Override + void accept(double t); + + /** + * Adds an element to the stream being built. + * + * @implSpec + * The default implementation behaves as if: + *

{@code
+         *     accept(t)
+         *     return this;
+         * }
+ * + * @param t the element to add + * @return {@code this} builder + * @throws IllegalStateException if the builder has already transitioned + * to the built state + */ + default Builder add(double t) { + accept(t); + return this; + } + + /** + * Builds the stream, transitioning this builder to the built state. + * An {@code IllegalStateException} is thrown if there are further + * attempts to operate on the builder after it has entered the built + * state. + * + * @return the built stream + * @throws IllegalStateException if the builder has already transitioned + * to the built state + */ + DoubleStream build(); + } } diff -r 9fa4af2af63d -r 06636235cd12 jdk/src/share/classes/java/util/stream/IntStream.java --- a/jdk/src/share/classes/java/util/stream/IntStream.java Fri Jul 12 15:01:08 2013 -0700 +++ b/jdk/src/share/classes/java/util/stream/IntStream.java Mon Jul 08 15:46:26 2013 -0400 @@ -664,7 +664,7 @@ * * @return a stream builder */ - public static StreamBuilder.OfInt builder() { + public static Builder builder() { return new Streams.IntStreamBuilderImpl(); } @@ -820,4 +820,61 @@ a.spliterator(), b.spliterator()); return StreamSupport.intStream(split, a.isParallel() || b.isParallel()); } + + /** + * A mutable builder for an {@code IntStream}. + * + *

A stream builder has a lifecycle, where it starts in a building + * phase, during which elements can be added, and then transitions to a + * built phase, after which elements may not be added. The built phase + * begins when the {@link #build()} method is called, which creates an + * ordered stream whose elements are the elements that were added to the + * stream builder, in the order they were added. + * + * @see IntStream#builder() + * @since 1.8 + */ + public interface Builder extends IntConsumer { + + /** + * Adds an element to the stream being built. + * + * @throws IllegalStateException if the builder has already transitioned + * to the built state + */ + @Override + void accept(int t); + + /** + * Adds an element to the stream being built. + * + * @implSpec + * The default implementation behaves as if: + *

{@code
+         *     accept(t)
+         *     return this;
+         * }
+ * + * @param t the element to add + * @return {@code this} builder + * @throws IllegalStateException if the builder has already transitioned + * to the built state + */ + default Builder add(int t) { + accept(t); + return this; + } + + /** + * Builds the stream, transitioning this builder to the built state. + * An {@code IllegalStateException} is thrown if there are further + * attempts to operate on the builder after it has entered the built + * state. + * + * @return the built stream + * @throws IllegalStateException if the builder has already transitioned to + * the built state + */ + IntStream build(); + } } diff -r 9fa4af2af63d -r 06636235cd12 jdk/src/share/classes/java/util/stream/LongStream.java --- a/jdk/src/share/classes/java/util/stream/LongStream.java Fri Jul 12 15:01:08 2013 -0700 +++ b/jdk/src/share/classes/java/util/stream/LongStream.java Mon Jul 08 15:46:26 2013 -0400 @@ -655,7 +655,7 @@ * * @return a stream builder */ - public static StreamBuilder.OfLong builder() { + public static Builder builder() { return new Streams.LongStreamBuilderImpl(); } @@ -826,4 +826,61 @@ a.spliterator(), b.spliterator()); return StreamSupport.longStream(split, a.isParallel() || b.isParallel()); } + + /** + * A mutable builder for a {@code LongStream}. + * + *

A stream builder has a lifecycle, where it starts in a building + * phase, during which elements can be added, and then transitions to a + * built phase, after which elements may not be added. The built phase + * begins when the {@link #build()} method is called, which creates an + * ordered stream whose elements are the elements that were added to the + * stream builder, in the order they were added. + * + * @see LongStream#builder() + * @since 1.8 + */ + public interface Builder extends LongConsumer { + + /** + * Adds an element to the stream being built. + * + * @throws IllegalStateException if the builder has already transitioned + * to the built state + */ + @Override + void accept(long t); + + /** + * Adds an element to the stream being built. + * + * @implSpec + * The default implementation behaves as if: + *

{@code
+         *     accept(t)
+         *     return this;
+         * }
+ * + * @param t the element to add + * @return {@code this} builder + * @throws IllegalStateException if the builder has already transitioned + * to the built state + */ + default Builder add(long t) { + accept(t); + return this; + } + + /** + * Builds the stream, transitioning this builder to the built state. + * An {@code IllegalStateException} is thrown if there are further + * attempts to operate on the builder after it has entered the built + * state. + * + * @return the built stream + * @throws IllegalStateException if the builder has already transitioned + * to the built state + */ + LongStream build(); + } } diff -r 9fa4af2af63d -r 06636235cd12 jdk/src/share/classes/java/util/stream/Stream.java --- a/jdk/src/share/classes/java/util/stream/Stream.java Fri Jul 12 15:01:08 2013 -0700 +++ b/jdk/src/share/classes/java/util/stream/Stream.java Mon Jul 08 15:46:26 2013 -0400 @@ -794,7 +794,7 @@ * @param type of elements * @return a stream builder */ - public static StreamBuilder builder() { + public static Builder builder() { return new Streams.StreamBuilderImpl<>(); } @@ -906,4 +906,65 @@ (Spliterator) a.spliterator(), (Spliterator) b.spliterator()); return StreamSupport.stream(split, a.isParallel() || b.isParallel()); } + + /** + * A mutable builder for a {@code Stream}. This allows the creation of a + * {@code Stream} by generating elements individually and adding them to the + * {@code Builder} (without the copying overhead that comes from using + * an {@code ArrayList} as a temporary buffer.) + * + *

A {@code Stream.Builder} has a lifecycle, where it starts in a building + * phase, during which elements can be added, and then transitions to a built + * phase, after which elements may not be added. The built phase begins + * when the {@link #build()} method is called, which creates an ordered + * {@code Stream} whose elements are the elements that were added to the stream + * builder, in the order they were added. + * + * @param the type of stream elements + * @see Stream#builder() + * @since 1.8 + */ + public interface Builder extends Consumer { + + /** + * Adds an element to the stream being built. + * + * @throws IllegalStateException if the builder has already transitioned to + * the built state + */ + @Override + void accept(T t); + + /** + * Adds an element to the stream being built. + * + * @implSpec + * The default implementation behaves as if: + *

{@code
+         *     accept(t)
+         *     return this;
+         * }
+ * + * @param t the element to add + * @return {@code this} builder + * @throws IllegalStateException if the builder has already transitioned to + * the built state + */ + default Builder add(T t) { + accept(t); + return this; + } + + /** + * Builds the stream, transitioning this builder to the built state. + * An {@code IllegalStateException} is thrown if there are further attempts + * to operate on the builder after it has entered the built state. + * + * @return the built stream + * @throws IllegalStateException if the builder has already transitioned to + * the built state + */ + Stream build(); + + } } diff -r 9fa4af2af63d -r 06636235cd12 jdk/src/share/classes/java/util/stream/StreamBuilder.java --- a/jdk/src/share/classes/java/util/stream/StreamBuilder.java Fri Jul 12 15:01:08 2013 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,265 +0,0 @@ -/* - * Copyright (c) 2013, 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.stream; - -import java.util.function.Consumer; -import java.util.function.DoubleConsumer; -import java.util.function.IntConsumer; -import java.util.function.LongConsumer; - -/** - * A mutable builder for a {@code Stream}. This allows the creation of a - * {@code Stream} by generating elements individually and adding them to the - * {@code StreamBuilder} (without the copying overhead that comes from using - * an {@code ArrayList} as a temporary buffer.) - * - *

A {@code StreamBuilder} has a lifecycle, where it starts in a building - * phase, during which elements can be added, and then transitions to a built - * phase, after which elements may not be added. The built phase begins - * when the {@link #build()} method is called, which creates an ordered - * {@code Stream} whose elements are the elements that were added to the stream - * builder, in the order they were added. - * - *

Primitive specializations of {@code StreamBuilder} are provided - * for {@link OfInt int}, {@link OfLong long}, and {@link OfDouble double} - * values. - * - * @param the type of stream elements - * @see Stream#builder() - * @since 1.8 - */ -public interface StreamBuilder extends Consumer { - - /** - * Adds an element to the stream being built. - * - * @throws IllegalStateException if the builder has already transitioned to - * the built state - */ - @Override - void accept(T t); - - /** - * Adds an element to the stream being built. - * - * @implSpec - * The default implementation behaves as if: - *

{@code
-     *     accept(t)
-     *     return this;
-     * }
- * - * @param t the element to add - * @return {@code this} builder - * @throws IllegalStateException if the builder has already transitioned to - * the built state - */ - default StreamBuilder add(T t) { - accept(t); - return this; - } - - /** - * Builds the stream, transitioning this builder to the built state. - * An {@code IllegalStateException} is thrown if there are further attempts - * to operate on the builder after it has entered the built state. - * - * @return the built stream - * @throws IllegalStateException if the builder has already transitioned to - * the built state - */ - Stream build(); - - /** - * A mutable builder for an {@code IntStream}. - * - *

A stream builder has a lifecycle, where it starts in a building - * phase, during which elements can be added, and then transitions to a - * built phase, after which elements may not be added. The built phase - * begins when the {@link #build()} method is called, which creates an - * ordered stream whose elements are the elements that were added to the - * stream builder, in the order they were added. - * - * @see IntStream#builder() - * @since 1.8 - */ - interface OfInt extends IntConsumer { - - /** - * Adds an element to the stream being built. - * - * @throws IllegalStateException if the builder has already transitioned - * to the built state - */ - @Override - void accept(int t); - - /** - * Adds an element to the stream being built. - * - * @implSpec - * The default implementation behaves as if: - *

{@code
-         *     accept(t)
-         *     return this;
-         * }
- * - * @param t the element to add - * @return {@code this} builder - * @throws IllegalStateException if the builder has already transitioned - * to the built state - */ - default StreamBuilder.OfInt add(int t) { - accept(t); - return this; - } - - /** - * Builds the stream, transitioning this builder to the built state. - * An {@code IllegalStateException} is thrown if there are further - * attempts to operate on the builder after it has entered the built - * state. - * - * @return the built stream - * @throws IllegalStateException if the builder has already transitioned to - * the built state - */ - IntStream build(); - } - - /** - * A mutable builder for a {@code LongStream}. - * - *

A stream builder has a lifecycle, where it starts in a building - * phase, during which elements can be added, and then transitions to a - * built phase, after which elements may not be added. The built phase - * begins when the {@link #build()} method is called, which creates an - * ordered stream whose elements are the elements that were added to the - * stream builder, in the order they were added. - * - * @see LongStream#builder() - * @since 1.8 - */ - interface OfLong extends LongConsumer { - - /** - * Adds an element to the stream being built. - * - * @throws IllegalStateException if the builder has already transitioned - * to the built state - */ - @Override - void accept(long t); - - /** - * Adds an element to the stream being built. - * - * @implSpec - * The default implementation behaves as if: - *

{@code
-         *     accept(t)
-         *     return this;
-         * }
- * - * @param t the element to add - * @return {@code this} builder - * @throws IllegalStateException if the builder has already transitioned - * to the built state - */ - default StreamBuilder.OfLong add(long t) { - accept(t); - return this; - } - - /** - * Builds the stream, transitioning this builder to the built state. - * An {@code IllegalStateException} is thrown if there are further - * attempts to operate on the builder after it has entered the built - * state. - * - * @return the built stream - * @throws IllegalStateException if the builder has already transitioned - * to the built state - */ - LongStream build(); - } - - /** - * A mutable builder for a {@code DoubleStream}. - * - *

A stream builder has a lifecycle, where it starts in a building - * phase, during which elements can be added, and then transitions to a - * built phase, after which elements may not be added. The built phase - * begins when the {@link #build()} method is called, which creates an - * ordered stream whose elements are the elements that were added to the - * stream builder, in the order they were added. - * - * @see LongStream#builder() - * @since 1.8 - */ - interface OfDouble extends DoubleConsumer { - - /** - * Adds an element to the stream being built. - * - * @throws IllegalStateException if the builder has already transitioned - * to the built state - */ - @Override - void accept(double t); - - /** - * Adds an element to the stream being built. - * - * @implSpec - * The default implementation behaves as if: - *

{@code
-         *     accept(t)
-         *     return this;
-         * }
- * - * @param t the element to add - * @return {@code this} builder - * @throws IllegalStateException if the builder has already transitioned - * to the built state - */ - default StreamBuilder.OfDouble add(double t) { - accept(t); - return this; - } - - /** - * Builds the stream, transitioning this builder to the built state. - * An {@code IllegalStateException} is thrown if there are further - * attempts to operate on the builder after it has entered the built - * state. - * - * @return the built stream - * @throws IllegalStateException if the builder has already transitioned - * to the built state - */ - DoubleStream build(); - } -} diff -r 9fa4af2af63d -r 06636235cd12 jdk/src/share/classes/java/util/stream/Streams.java --- a/jdk/src/share/classes/java/util/stream/Streams.java Fri Jul 12 15:01:08 2013 -0700 +++ b/jdk/src/share/classes/java/util/stream/Streams.java Mon Jul 08 15:46:26 2013 -0400 @@ -317,7 +317,7 @@ static final class StreamBuilderImpl extends AbstractStreamBuilderImpl> - implements StreamBuilder { + implements Stream.Builder { // The first element in the stream // valid if count == 1 T first; @@ -363,7 +363,7 @@ } } - public StreamBuilder add(T t) { + public Stream.Builder add(T t) { accept(t); return this; } @@ -409,7 +409,7 @@ static final class IntStreamBuilderImpl extends AbstractStreamBuilderImpl - implements StreamBuilder.OfInt, Spliterator.OfInt { + implements IntStream.Builder, Spliterator.OfInt { // The first element in the stream // valid if count == 1 int first; @@ -496,7 +496,7 @@ static final class LongStreamBuilderImpl extends AbstractStreamBuilderImpl - implements StreamBuilder.OfLong, Spliterator.OfLong { + implements LongStream.Builder, Spliterator.OfLong { // The first element in the stream // valid if count == 1 long first; @@ -583,7 +583,7 @@ static final class DoubleStreamBuilderImpl extends AbstractStreamBuilderImpl - implements StreamBuilder.OfDouble, Spliterator.OfDouble { + implements DoubleStream.Builder, Spliterator.OfDouble { // The first element in the stream // valid if count == 1 double first; diff -r 9fa4af2af63d -r 06636235cd12 jdk/test/java/util/stream/test/org/openjdk/tests/java/util/stream/StreamBuilderTest.java --- a/jdk/test/java/util/stream/test/org/openjdk/tests/java/util/stream/StreamBuilderTest.java Fri Jul 12 15:01:08 2013 -0700 +++ b/jdk/test/java/util/stream/test/org/openjdk/tests/java/util/stream/StreamBuilderTest.java Mon Jul 08 15:46:26 2013 -0400 @@ -35,7 +35,6 @@ import java.util.stream.LongStream; import java.util.stream.OpTestCase; import java.util.stream.Stream; -import java.util.stream.StreamBuilder; import java.util.stream.TestData; import static java.util.stream.Collectors.toList; @@ -89,7 +88,7 @@ @Test(dataProvider = "sizes") public void testAfterBuilding(int size) { - StreamBuilder sb = Stream.builder(); + Stream.Builder sb = Stream.builder(); IntStream.range(0, size).boxed().forEach(sb); sb.build(); @@ -101,15 +100,15 @@ @Test(dataProvider = "sizes") public void testStreamBuilder(int size) { testStreamBuilder(size, (s) -> { - StreamBuilder sb = Stream.builder(); + Stream.Builder sb = Stream.builder(); IntStream.range(0, s).boxed().forEach(sb); return sb.build(); }); testStreamBuilder(size, (s) -> { - StreamBuilder sb = Stream.builder(); + Stream.Builder sb = Stream.builder(); IntStream.range(0, s).boxed().forEach(i -> { - StreamBuilder _sb = sb.add(i); + Stream.Builder _sb = sb.add(i); assertTrue(sb == _sb); }); return sb.build(); @@ -151,7 +150,7 @@ @Test(dataProvider = "sizes") public void testIntAfterBuilding(int size) { - StreamBuilder.OfInt sb = IntStream.builder(); + IntStream.Builder sb = IntStream.builder(); IntStream.range(0, size).forEach(sb); sb.build(); @@ -163,15 +162,15 @@ @Test(dataProvider = "sizes") public void testIntStreamBuilder(int size) { testIntStreamBuilder(size, (s) -> { - StreamBuilder.OfInt sb = IntStream.builder(); + IntStream.Builder sb = IntStream.builder(); IntStream.range(0, s).forEach(sb); return sb.build(); }); testIntStreamBuilder(size, (s) -> { - StreamBuilder.OfInt sb = IntStream.builder(); + IntStream.Builder sb = IntStream.builder(); IntStream.range(0, s).forEach(i -> { - StreamBuilder.OfInt _sb = sb.add(i); + IntStream.Builder _sb = sb.add(i); assertTrue(sb == _sb); }); return sb.build(); @@ -213,7 +212,7 @@ @Test(dataProvider = "sizes") public void testLongAfterBuilding(int size) { - StreamBuilder.OfLong sb = LongStream.builder(); + LongStream.Builder sb = LongStream.builder(); LongStream.range(0, size).forEach(sb); sb.build(); @@ -225,15 +224,15 @@ @Test(dataProvider = "sizes") public void testLongStreamBuilder(int size) { testLongStreamBuilder(size, (s) -> { - StreamBuilder.OfLong sb = LongStream.builder(); + LongStream.Builder sb = LongStream.builder(); LongStream.range(0, s).forEach(sb); return sb.build(); }); testLongStreamBuilder(size, (s) -> { - StreamBuilder.OfLong sb = LongStream.builder(); + LongStream.Builder sb = LongStream.builder(); LongStream.range(0, s).forEach(i -> { - StreamBuilder.OfLong _sb = sb.add(i); + LongStream.Builder _sb = sb.add(i); assertTrue(sb == _sb); }); return sb.build(); @@ -274,7 +273,7 @@ @Test(dataProvider = "sizes") public void testDoubleAfterBuilding(int size) { - StreamBuilder.OfDouble sb = DoubleStream.builder(); + DoubleStream.Builder sb = DoubleStream.builder(); IntStream.range(0, size).asDoubleStream().forEach(sb); sb.build(); @@ -286,15 +285,15 @@ @Test(dataProvider = "sizes") public void testDoubleStreamBuilder(int size) { testDoubleStreamBuilder(size, (s) -> { - StreamBuilder.OfDouble sb = DoubleStream.builder(); + DoubleStream.Builder sb = DoubleStream.builder(); IntStream.range(0, s).asDoubleStream().forEach(sb); return sb.build(); }); testDoubleStreamBuilder(size, (s) -> { - StreamBuilder.OfDouble sb = DoubleStream.builder(); + DoubleStream.Builder sb = DoubleStream.builder(); IntStream.range(0, s).asDoubleStream().forEach(i -> { - StreamBuilder.OfDouble _sb = sb.add(i); + DoubleStream.Builder _sb = sb.add(i); assertTrue(sb == _sb); }); return sb.build();