1 /* 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package java.util.stream; 24 25 import org.testng.Assert; 26 27 import java.util.Spliterator; 28 import java.util.function.IntFunction; 29 30 /** Test helper class for java.util.stream test framework */ 31 public final class CollectorOps { CollectorOps()32 private CollectorOps() { } 33 collector()34 public static <E_IN> StatefulTestOp<E_IN> collector() { 35 return new StatefulCollector<>(0, StreamShape.REFERENCE); 36 } 37 38 /* Utility classes for collecting output of intermediate pipeline stages */ 39 public static class StatefulCollector<E_IN> implements StatefulTestOp<E_IN> { 40 private final int opFlags; 41 private final StreamShape inputShape; 42 StatefulCollector(int opFlags, StreamShape inputShape)43 public StatefulCollector(int opFlags, StreamShape inputShape) { 44 this.opFlags = opFlags; 45 this.inputShape = inputShape; 46 } 47 48 @Override inputShape()49 public StreamShape inputShape() { 50 return inputShape; 51 } 52 53 @Override outputShape()54 public StreamShape outputShape() { 55 return inputShape; 56 } 57 58 @Override opGetFlags()59 public int opGetFlags() { 60 return opFlags; 61 } 62 63 @Override opWrapSink(int flags, boolean parallel, Sink<E_IN> sink)64 public Sink<E_IN> opWrapSink(int flags, boolean parallel, Sink<E_IN> sink) { 65 return sink; 66 } 67 68 @Override opEvaluateParallel(PipelineHelper<E_IN> helper, Spliterator<P_IN> spliterator, IntFunction<E_IN[]> generator)69 public <P_IN> Node<E_IN> opEvaluateParallel(PipelineHelper<E_IN> helper, 70 Spliterator<P_IN> spliterator, 71 IntFunction<E_IN[]> generator) { 72 return helper.evaluate(spliterator, false, generator); 73 } 74 } 75 76 public static class TestParallelSizedOp<T> extends StatefulCollector<T> { TestParallelSizedOp()77 public TestParallelSizedOp() { 78 this(StreamShape.REFERENCE); 79 } 80 TestParallelSizedOp(StreamShape shape)81 protected TestParallelSizedOp(StreamShape shape) { 82 super(0, shape); 83 } 84 85 @Override opEvaluateParallel(PipelineHelper<T> helper, Spliterator<P_IN> spliterator, IntFunction<T[]> generator)86 public <P_IN> Node<T> opEvaluateParallel(PipelineHelper<T> helper, 87 Spliterator<P_IN> spliterator, 88 IntFunction<T[]> generator) { 89 int flags = helper.getStreamAndOpFlags(); 90 91 Assert.assertTrue(StreamOpFlag.SIZED.isKnown(flags)); 92 return super.opEvaluateParallel(helper, spliterator, generator); 93 } 94 95 public static class OfInt extends TestParallelSizedOp<Integer> { OfInt()96 public OfInt() { 97 super(StreamShape.INT_VALUE); 98 } 99 } 100 101 public static class OfLong extends TestParallelSizedOp<Long> { OfLong()102 public OfLong() { 103 super(StreamShape.LONG_VALUE); 104 } 105 } 106 107 public static class OfDouble extends TestParallelSizedOp<Double> { OfDouble()108 public OfDouble() { 109 super(StreamShape.DOUBLE_VALUE); 110 } 111 } 112 } 113 } 114