OpenJDK / lambda / lambda / jdk
changeset 8789:569088178418
Remove Streams.zip.
author | psandoz |
---|---|
date | Tue, 18 Jun 2013 14:39:51 +0200 |
parents | dfdfae8aab5d |
children | dc643a58768e |
files | src/share/classes/java/util/stream/Streams.java test/java/util/stream/test/org/openjdk/tests/java/util/stream/ZipOpTest.java |
diffstat | 2 files changed, 0 insertions(+), 170 deletions(-) [+] |
line wrap: on
line diff
--- a/src/share/classes/java/util/stream/Streams.java Mon Jun 17 16:06:51 2013 -0400 +++ b/src/share/classes/java/util/stream/Streams.java Tue Jun 18 14:39:51 2013 +0200 @@ -151,88 +151,6 @@ } /** - * Creates a lazy and sequential combined {@code Stream} whose elements are - * the result of combining the elements of two streams. The resulting - * stream is ordered if both of the input streams are ordered. The size of - * the resulting stream will be the smaller of the sizes of the two input - * streams; any elements remaining in the larger of the two streams will not - * be consumed. - * - * @param <A> the type of elements of the first {@code Stream} - * @param <B> the type of elements of the second {@code Stream} - * @param <C> the type of elements of the zipped {@code Stream} - * @param a the first {@code Stream} to combine - * @param b the second {@code Stream} to combine - * @param zipper a function applied to an element from the first - * {@code Stream} and an element from the second {@code Stream} to - * produce an element for the combined {@code Stream} - * @return a combined {@code Stream} - */ - public static <A, B, C> Stream<C> zip(Stream<? extends A> a, - Stream<? extends B> b, - BiFunction<? super A, ? super B, ? extends C> zipper) { - Objects.requireNonNull(zipper); - @SuppressWarnings("unchecked") - Spliterator<A> as = (Spliterator<A>) Objects.requireNonNull(a).spliterator(); - @SuppressWarnings("unchecked") - Spliterator<B> bs = (Spliterator<B>) Objects.requireNonNull(b).spliterator(); - - // Combining loses DISTINCT and SORTED characteristics and for other - // characteristics the combined stream has a characteristic if both - // streams to combine have the characteristic - int characteristics = as.characteristics() & bs.characteristics() & - ~(Spliterator.DISTINCT | Spliterator.SORTED); - long size = Math.min(as.estimateSize(), bs.estimateSize()); - - Spliterator<C> cs = new ZipperSpliterator<>(as, bs, zipper, - size, characteristics); - return (a.isParallel() || b.isParallel()) - ? StreamSupport.parallelStream(cs) - : StreamSupport.stream(cs); - } - - private static final class ZipperSpliterator<A, B, C> extends Spliterators.AbstractSpliterator<C> - implements Consumer<Object> { - final Spliterator<A> as; - final Spliterator<B> bs; - final BiFunction<? super A, ? super B, ? extends C> zipper; - Object a; - Object b; - - ZipperSpliterator(Spliterator<A> as, Spliterator<B> bs, - BiFunction<? super A, ? super B, ? extends C> zipper, - long est, int additionalCharacteristics) { - super(est, additionalCharacteristics); - this.as = as; - this.bs = bs; - this.zipper = zipper; - this.a = Streams.NONE; - } - - @Override - public void accept(Object aOrB) { - if (a == Streams.NONE) { - a = aOrB; - } - else { - b = aOrB; - } - } - - @Override - @SuppressWarnings("unchecked") - public boolean tryAdvance(Consumer<? super C> action) { - if (as.tryAdvance(this) && bs.tryAdvance(this)) { - Object a = this.a; - this.a = Streams.NONE; - action.accept(zipper.apply((A) a, (B) b)); - return true; - } - return false; - } - } - - /** * An {@code int} range spliterator. */ static final class RangeIntSpliterator implements Spliterator.OfInt {
--- a/test/java/util/stream/test/org/openjdk/tests/java/util/stream/ZipOpTest.java Mon Jun 17 16:06:51 2013 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,88 +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. - * - * 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 org.openjdk.tests.java.util.stream; - -import java.util.List; -import java.util.function.Function; -import java.util.function.Supplier; -import java.util.stream.IntStream; -import java.util.stream.OpTestCase; -import java.util.stream.StreamTestDataProvider; -import org.testng.annotations.Test; - -import java.util.Arrays; -import java.util.Collections; -import java.util.stream.Stream; -import java.util.stream.Streams; -import java.util.stream.TestData; - -import static java.util.stream.LambdaTestHelpers.assertContents; -import static java.util.stream.LambdaTestHelpers.countTo; - -@Test -public class ZipOpTest extends OpTestCase { - - public void testZip() { - List<Integer> emptyIntList = Collections.emptyList(); - assertContents(Streams.zip(emptyIntList.stream(), emptyIntList.stream(), Integer::sum).iterator(), - emptyIntList.iterator()); - - assertContents(Streams.zip(emptyIntList.stream(), countTo(10).stream(), Integer::sum).iterator(), - emptyIntList.iterator()); - - assertContents(Streams.zip(countTo(10).stream(), emptyIntList.stream(), Integer::sum).iterator(), - emptyIntList.iterator()); - - Supplier<Stream<Integer>> s = () -> countTo(2).stream(); - Stream<Integer> x = s.get(); - for (int i = 0; i < 4; i++) { - x = Streams.zip(x, s.get(), Integer::sum); - } - assertContents(x.iterator(), Arrays.asList(5, 5 * 2).iterator()); - } - - @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class) - public void testOpsSequential(String name, TestData.OfRef<Integer> data) { - if (data.size() > 2) { - exerciseOps(data, - s -> Streams.zip(data.stream(), data.stream().limit(data.size() / 2), - (a, b) -> "" + a + b)); - exerciseOps(data, - s -> Streams.zip(data.stream().limit(data.size() / 2), data.stream(), - (a, b) -> "" + a + b)); - } - else { - exerciseOps(data, s -> Streams.zip(data.stream(), data.stream(), (a, b) -> "" + a + b)); - - } - } - - public void testWithGenerate() { - Supplier<Stream<String>> s = () -> Streams.zip(Stream.generate(() -> "i"), - IntStream.range(0, 100).boxed(), - (String a, Integer b) -> a + b); - withData(TestData.Factory.ofSupplier("", s)). - stream(Function.identity()). - exercise(); - } -}