src/java.base/share/classes/java/util/stream/SortedOps.java
author psandoz
Thu, 21 Dec 2017 13:52:20 -0800
changeset 48593 fca88bbbafb9
parent 47216 71c04702a3d5
permissions -rw-r--r--
8075939: Stream.flatMap() causes breaking of short-circuiting of terminal operations Reviewed-by: forax, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     1
/*
48593
fca88bbbafb9 8075939: Stream.flatMap() causes breaking of short-circuiting of terminal operations
psandoz
parents: 47216
diff changeset
     2
 * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     4
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    10
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    15
 * accompanied this code).
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    16
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    20
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    23
 * questions.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    24
 */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    25
package java.util.stream;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    26
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    27
import java.util.ArrayList;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    28
import java.util.Arrays;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    29
import java.util.Comparator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    30
import java.util.Objects;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    31
import java.util.Spliterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    32
import java.util.function.IntFunction;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    33
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    34
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    35
/**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    36
 * Factory methods for transforming streams into sorted streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    37
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    38
 * @since 1.8
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    39
 */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    40
final class SortedOps {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    41
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    42
    private SortedOps() { }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    43
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    44
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    45
     * Appends a "sorted" operation to the provided stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    46
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    47
     * @param <T> the type of both input and output elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    48
     * @param upstream a reference stream with element type T
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    49
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    50
    static <T> Stream<T> makeRef(AbstractPipeline<?, T, ?> upstream) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    51
        return new OfRef<>(upstream);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    52
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    53
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    54
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    55
     * Appends a "sorted" operation to the provided stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    56
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    57
     * @param <T> the type of both input and output elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    58
     * @param upstream a reference stream with element type T
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    59
     * @param comparator the comparator to order elements by
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    60
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    61
    static <T> Stream<T> makeRef(AbstractPipeline<?, T, ?> upstream,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    62
                                Comparator<? super T> comparator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    63
        return new OfRef<>(upstream, comparator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    64
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    65
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    66
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    67
     * Appends a "sorted" operation to the provided stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    68
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    69
     * @param <T> the type of both input and output elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    70
     * @param upstream a reference stream with element type T
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    71
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    72
    static <T> IntStream makeInt(AbstractPipeline<?, Integer, ?> upstream) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    73
        return new OfInt(upstream);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    74
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    75
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    76
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    77
     * Appends a "sorted" operation to the provided stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    78
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    79
     * @param <T> the type of both input and output elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    80
     * @param upstream a reference stream with element type T
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    81
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    82
    static <T> LongStream makeLong(AbstractPipeline<?, Long, ?> upstream) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    83
        return new OfLong(upstream);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    84
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    85
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    86
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    87
     * Appends a "sorted" operation to the provided stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    88
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    89
     * @param <T> the type of both input and output elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    90
     * @param upstream a reference stream with element type T
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    91
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    92
    static <T> DoubleStream makeDouble(AbstractPipeline<?, Double, ?> upstream) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    93
        return new OfDouble(upstream);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    94
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    95
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    96
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    97
     * Specialized subtype for sorting reference streams
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    98
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    99
    private static final class OfRef<T> extends ReferencePipeline.StatefulOp<T, T> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   100
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   101
         * Comparator used for sorting
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   102
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   103
        private final boolean isNaturalSort;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   104
        private final Comparator<? super T> comparator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   105
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   106
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   107
         * Sort using natural order of {@literal <T>} which must be
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   108
         * {@code Comparable}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   109
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   110
        OfRef(AbstractPipeline<?, T, ?> upstream) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   111
            super(upstream, StreamShape.REFERENCE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   112
                  StreamOpFlag.IS_ORDERED | StreamOpFlag.IS_SORTED);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   113
            this.isNaturalSort = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   114
            // Will throw CCE when we try to sort if T is not Comparable
22297
1c62c67d9dd2 8031373: Lint warnings in java.util.stream
briangoetz
parents: 22078
diff changeset
   115
            @SuppressWarnings("unchecked")
1c62c67d9dd2 8031373: Lint warnings in java.util.stream
briangoetz
parents: 22078
diff changeset
   116
            Comparator<? super T> comp = (Comparator<? super T>) Comparator.naturalOrder();
1c62c67d9dd2 8031373: Lint warnings in java.util.stream
briangoetz
parents: 22078
diff changeset
   117
            this.comparator = comp;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   118
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   119
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   120
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   121
         * Sort using the provided comparator.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   122
         *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   123
         * @param comparator The comparator to be used to evaluate ordering.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   124
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   125
        OfRef(AbstractPipeline<?, T, ?> upstream, Comparator<? super T> comparator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   126
            super(upstream, StreamShape.REFERENCE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   127
                  StreamOpFlag.IS_ORDERED | StreamOpFlag.NOT_SORTED);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   128
            this.isNaturalSort = false;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   129
            this.comparator = Objects.requireNonNull(comparator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   130
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   131
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   132
        @Override
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   133
        public Sink<T> opWrapSink(int flags, Sink<T> sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   134
            Objects.requireNonNull(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   135
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   136
            // If the input is already naturally sorted and this operation
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   137
            // also naturally sorted then this is a no-op
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   138
            if (StreamOpFlag.SORTED.isKnown(flags) && isNaturalSort)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   139
                return sink;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   140
            else if (StreamOpFlag.SIZED.isKnown(flags))
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   141
                return new SizedRefSortingSink<>(sink, comparator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   142
            else
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   143
                return new RefSortingSink<>(sink, comparator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   144
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   145
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   146
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   147
        public <P_IN> Node<T> opEvaluateParallel(PipelineHelper<T> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   148
                                                 Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   149
                                                 IntFunction<T[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   150
            // If the input is already naturally sorted and this operation
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   151
            // naturally sorts then collect the output
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   152
            if (StreamOpFlag.SORTED.isKnown(helper.getStreamAndOpFlags()) && isNaturalSort) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   153
                return helper.evaluate(spliterator, false, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   154
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   155
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   156
                // @@@ Weak two-pass parallel implementation; parallel collect, parallel sort
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   157
                T[] flattenedData = helper.evaluate(spliterator, true, generator).asArray(generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   158
                Arrays.parallelSort(flattenedData, comparator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   159
                return Nodes.node(flattenedData);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   160
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   161
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   162
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   163
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   164
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   165
     * Specialized subtype for sorting int streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   166
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   167
    private static final class OfInt extends IntPipeline.StatefulOp<Integer> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   168
        OfInt(AbstractPipeline<?, Integer, ?> upstream) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   169
            super(upstream, StreamShape.INT_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   170
                  StreamOpFlag.IS_ORDERED | StreamOpFlag.IS_SORTED);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   171
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   172
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   173
        @Override
22297
1c62c67d9dd2 8031373: Lint warnings in java.util.stream
briangoetz
parents: 22078
diff changeset
   174
        public Sink<Integer> opWrapSink(int flags, Sink<Integer> sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   175
            Objects.requireNonNull(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   176
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   177
            if (StreamOpFlag.SORTED.isKnown(flags))
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   178
                return sink;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   179
            else if (StreamOpFlag.SIZED.isKnown(flags))
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   180
                return new SizedIntSortingSink(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   181
            else
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   182
                return new IntSortingSink(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   183
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   184
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   185
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   186
        public <P_IN> Node<Integer> opEvaluateParallel(PipelineHelper<Integer> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   187
                                                       Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   188
                                                       IntFunction<Integer[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   189
            if (StreamOpFlag.SORTED.isKnown(helper.getStreamAndOpFlags())) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   190
                return helper.evaluate(spliterator, false, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   191
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   192
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   193
                Node.OfInt n = (Node.OfInt) helper.evaluate(spliterator, true, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   194
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 17182
diff changeset
   195
                int[] content = n.asPrimitiveArray();
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   196
                Arrays.parallelSort(content);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   197
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   198
                return Nodes.node(content);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   199
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   200
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   201
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   202
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   203
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   204
     * Specialized subtype for sorting long streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   205
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   206
    private static final class OfLong extends LongPipeline.StatefulOp<Long> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   207
        OfLong(AbstractPipeline<?, Long, ?> upstream) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   208
            super(upstream, StreamShape.LONG_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   209
                  StreamOpFlag.IS_ORDERED | StreamOpFlag.IS_SORTED);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   210
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   211
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   212
        @Override
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18571
diff changeset
   213
        public Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   214
            Objects.requireNonNull(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   215
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   216
            if (StreamOpFlag.SORTED.isKnown(flags))
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   217
                return sink;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   218
            else if (StreamOpFlag.SIZED.isKnown(flags))
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   219
                return new SizedLongSortingSink(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   220
            else
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   221
                return new LongSortingSink(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   222
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   223
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   224
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   225
        public <P_IN> Node<Long> opEvaluateParallel(PipelineHelper<Long> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   226
                                                    Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   227
                                                    IntFunction<Long[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   228
            if (StreamOpFlag.SORTED.isKnown(helper.getStreamAndOpFlags())) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   229
                return helper.evaluate(spliterator, false, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   230
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   231
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   232
                Node.OfLong n = (Node.OfLong) helper.evaluate(spliterator, true, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   233
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 17182
diff changeset
   234
                long[] content = n.asPrimitiveArray();
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   235
                Arrays.parallelSort(content);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   236
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   237
                return Nodes.node(content);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   238
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   239
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   240
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   241
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   242
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   243
     * Specialized subtype for sorting double streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   244
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   245
    private static final class OfDouble extends DoublePipeline.StatefulOp<Double> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   246
        OfDouble(AbstractPipeline<?, Double, ?> upstream) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   247
            super(upstream, StreamShape.DOUBLE_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   248
                  StreamOpFlag.IS_ORDERED | StreamOpFlag.IS_SORTED);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   249
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   250
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   251
        @Override
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18571
diff changeset
   252
        public Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   253
            Objects.requireNonNull(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   254
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   255
            if (StreamOpFlag.SORTED.isKnown(flags))
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   256
                return sink;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   257
            else if (StreamOpFlag.SIZED.isKnown(flags))
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   258
                return new SizedDoubleSortingSink(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   259
            else
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   260
                return new DoubleSortingSink(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   261
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   262
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   263
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   264
        public <P_IN> Node<Double> opEvaluateParallel(PipelineHelper<Double> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   265
                                                      Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   266
                                                      IntFunction<Double[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   267
            if (StreamOpFlag.SORTED.isKnown(helper.getStreamAndOpFlags())) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   268
                return helper.evaluate(spliterator, false, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   269
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   270
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   271
                Node.OfDouble n = (Node.OfDouble) helper.evaluate(spliterator, true, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   272
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 17182
diff changeset
   273
                double[] content = n.asPrimitiveArray();
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   274
                Arrays.parallelSort(content);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   275
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   276
                return Nodes.node(content);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   277
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   278
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   279
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   280
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   281
    /**
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   282
     * Abstract {@link Sink} for implementing sort on reference streams.
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   283
     *
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   284
     * <p>
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   285
     * Note: documentation below applies to reference and all primitive sinks.
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   286
     * <p>
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   287
     * Sorting sinks first accept all elements, buffering then into an array
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   288
     * or a re-sizable data structure, if the size of the pipeline is known or
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   289
     * unknown respectively.  At the end of the sink protocol those elements are
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   290
     * sorted and then pushed downstream.
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   291
     * This class records if {@link #cancellationRequested} is called.  If so it
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   292
     * can be inferred that the source pushing source elements into the pipeline
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   293
     * knows that the pipeline is short-circuiting.  In such cases sub-classes
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   294
     * pushing elements downstream will preserve the short-circuiting protocol
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   295
     * by calling {@code downstream.cancellationRequested()} and checking the
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   296
     * result is {@code false} before an element is pushed.
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   297
     * <p>
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   298
     * Note that the above behaviour is an optimization for sorting with
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   299
     * sequential streams.  It is not an error that more elements, than strictly
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   300
     * required to produce a result, may flow through the pipeline.  This can
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   301
     * occur, in general (not restricted to just sorting), for short-circuiting
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   302
     * parallel pipelines.
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   303
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   304
    private abstract static class AbstractRefSortingSink<T> extends Sink.ChainedReference<T, T> {
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   305
        protected final Comparator<? super T> comparator;
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   306
        // @@@ could be a lazy final value, if/when support is added
48593
fca88bbbafb9 8075939: Stream.flatMap() causes breaking of short-circuiting of terminal operations
psandoz
parents: 47216
diff changeset
   307
        // true if cancellationRequested() has been called
fca88bbbafb9 8075939: Stream.flatMap() causes breaking of short-circuiting of terminal operations
psandoz
parents: 47216
diff changeset
   308
        protected boolean cancellationRequestedCalled;
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   309
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   310
        AbstractRefSortingSink(Sink<? super T> downstream, Comparator<? super T> comparator) {
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   311
            super(downstream);
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   312
            this.comparator = comparator;
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   313
        }
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   314
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   315
        /**
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   316
         * Records is cancellation is requested so short-circuiting behaviour
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   317
         * can be preserved when the sorted elements are pushed downstream.
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   318
         *
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   319
         * @return false, as this sink never short-circuits.
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   320
         */
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   321
        @Override
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   322
        public final boolean cancellationRequested() {
48593
fca88bbbafb9 8075939: Stream.flatMap() causes breaking of short-circuiting of terminal operations
psandoz
parents: 47216
diff changeset
   323
            // If this method is called then an operation within the stream
fca88bbbafb9 8075939: Stream.flatMap() causes breaking of short-circuiting of terminal operations
psandoz
parents: 47216
diff changeset
   324
            // pipeline is short-circuiting (see AbstractPipeline.copyInto).
fca88bbbafb9 8075939: Stream.flatMap() causes breaking of short-circuiting of terminal operations
psandoz
parents: 47216
diff changeset
   325
            // Note that we cannot differentiate between an upstream or
fca88bbbafb9 8075939: Stream.flatMap() causes breaking of short-circuiting of terminal operations
psandoz
parents: 47216
diff changeset
   326
            // downstream operation
fca88bbbafb9 8075939: Stream.flatMap() causes breaking of short-circuiting of terminal operations
psandoz
parents: 47216
diff changeset
   327
            cancellationRequestedCalled = true;
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   328
            return false;
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   329
        }
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   330
    }
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   331
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   332
    /**
20503
074dd13d9cdf 8025535: Unsafe typecast in java.util.stream.SortedOps
psandoz
parents: 19593
diff changeset
   333
     * {@link Sink} for implementing sort on SIZED reference streams.
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   334
     */
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   335
    private static final class SizedRefSortingSink<T> extends AbstractRefSortingSink<T> {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   336
        private T[] array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   337
        private int offset;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   338
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   339
        SizedRefSortingSink(Sink<? super T> sink, Comparator<? super T> comparator) {
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   340
            super(sink, comparator);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   341
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   342
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   343
        @Override
22297
1c62c67d9dd2 8031373: Lint warnings in java.util.stream
briangoetz
parents: 22078
diff changeset
   344
        @SuppressWarnings("unchecked")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   345
        public void begin(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   346
            if (size >= Nodes.MAX_ARRAY_SIZE)
20507
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 20503
diff changeset
   347
                throw new IllegalArgumentException(Nodes.BAD_SIZE);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   348
            array = (T[]) new Object[(int) size];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   349
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   350
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   351
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   352
        public void end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   353
            Arrays.sort(array, 0, offset, comparator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   354
            downstream.begin(offset);
48593
fca88bbbafb9 8075939: Stream.flatMap() causes breaking of short-circuiting of terminal operations
psandoz
parents: 47216
diff changeset
   355
            if (!cancellationRequestedCalled) {
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   356
                for (int i = 0; i < offset; i++)
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   357
                    downstream.accept(array[i]);
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   358
            }
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   359
            else {
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   360
                for (int i = 0; i < offset && !downstream.cancellationRequested(); i++)
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   361
                    downstream.accept(array[i]);
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   362
            }
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   363
            downstream.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   364
            array = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   365
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   366
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   367
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   368
        public void accept(T t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   369
            array[offset++] = t;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   370
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   371
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   372
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   373
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   374
     * {@link Sink} for implementing sort on reference streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   375
     */
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   376
    private static final class RefSortingSink<T> extends AbstractRefSortingSink<T> {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   377
        private ArrayList<T> list;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   378
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   379
        RefSortingSink(Sink<? super T> sink, Comparator<? super T> comparator) {
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   380
            super(sink, comparator);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   381
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   382
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   383
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   384
        public void begin(long size) {
20503
074dd13d9cdf 8025535: Unsafe typecast in java.util.stream.SortedOps
psandoz
parents: 19593
diff changeset
   385
            if (size >= Nodes.MAX_ARRAY_SIZE)
20507
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 20503
diff changeset
   386
                throw new IllegalArgumentException(Nodes.BAD_SIZE);
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 20507
diff changeset
   387
            list = (size >= 0) ? new ArrayList<>((int) size) : new ArrayList<>();
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   388
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   389
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   390
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   391
        public void end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   392
            list.sort(comparator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   393
            downstream.begin(list.size());
48593
fca88bbbafb9 8075939: Stream.flatMap() causes breaking of short-circuiting of terminal operations
psandoz
parents: 47216
diff changeset
   394
            if (!cancellationRequestedCalled) {
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   395
                list.forEach(downstream::accept);
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   396
            }
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   397
            else {
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   398
                for (T t : list) {
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   399
                    if (downstream.cancellationRequested()) break;
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   400
                    downstream.accept(t);
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   401
                }
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   402
            }
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   403
            downstream.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   404
            list = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   405
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   406
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   407
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   408
        public void accept(T t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   409
            list.add(t);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   410
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   411
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   412
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   413
    /**
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   414
     * Abstract {@link Sink} for implementing sort on int streams.
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   415
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   416
    private abstract static class AbstractIntSortingSink extends Sink.ChainedInt<Integer> {
48593
fca88bbbafb9 8075939: Stream.flatMap() causes breaking of short-circuiting of terminal operations
psandoz
parents: 47216
diff changeset
   417
        // true if cancellationRequested() has been called
fca88bbbafb9 8075939: Stream.flatMap() causes breaking of short-circuiting of terminal operations
psandoz
parents: 47216
diff changeset
   418
        protected boolean cancellationRequestedCalled;
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   419
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   420
        AbstractIntSortingSink(Sink<? super Integer> downstream) {
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   421
            super(downstream);
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   422
        }
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   423
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   424
        @Override
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   425
        public final boolean cancellationRequested() {
48593
fca88bbbafb9 8075939: Stream.flatMap() causes breaking of short-circuiting of terminal operations
psandoz
parents: 47216
diff changeset
   426
            cancellationRequestedCalled = true;
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   427
            return false;
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   428
        }
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   429
    }
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   430
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   431
    /**
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   432
     * {@link Sink} for implementing sort on SIZED int streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   433
     */
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   434
    private static final class SizedIntSortingSink extends AbstractIntSortingSink {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   435
        private int[] array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   436
        private int offset;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   437
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   438
        SizedIntSortingSink(Sink<? super Integer> downstream) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   439
            super(downstream);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   440
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   441
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   442
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   443
        public void begin(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   444
            if (size >= Nodes.MAX_ARRAY_SIZE)
20507
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 20503
diff changeset
   445
                throw new IllegalArgumentException(Nodes.BAD_SIZE);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   446
            array = new int[(int) size];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   447
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   448
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   449
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   450
        public void end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   451
            Arrays.sort(array, 0, offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   452
            downstream.begin(offset);
48593
fca88bbbafb9 8075939: Stream.flatMap() causes breaking of short-circuiting of terminal operations
psandoz
parents: 47216
diff changeset
   453
            if (!cancellationRequestedCalled) {
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   454
                for (int i = 0; i < offset; i++)
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   455
                    downstream.accept(array[i]);
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   456
            }
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   457
            else {
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   458
                for (int i = 0; i < offset && !downstream.cancellationRequested(); i++)
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   459
                    downstream.accept(array[i]);
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   460
            }
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   461
            downstream.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   462
            array = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   463
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   464
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   465
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   466
        public void accept(int t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   467
            array[offset++] = t;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   468
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   469
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   470
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   471
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   472
     * {@link Sink} for implementing sort on int streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   473
     */
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   474
    private static final class IntSortingSink extends AbstractIntSortingSink {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   475
        private SpinedBuffer.OfInt b;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   476
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   477
        IntSortingSink(Sink<? super Integer> sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   478
            super(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   479
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   480
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   481
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   482
        public void begin(long size) {
20503
074dd13d9cdf 8025535: Unsafe typecast in java.util.stream.SortedOps
psandoz
parents: 19593
diff changeset
   483
            if (size >= Nodes.MAX_ARRAY_SIZE)
20507
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 20503
diff changeset
   484
                throw new IllegalArgumentException(Nodes.BAD_SIZE);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   485
            b = (size > 0) ? new SpinedBuffer.OfInt((int) size) : new SpinedBuffer.OfInt();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   486
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   487
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   488
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   489
        public void end() {
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 17182
diff changeset
   490
            int[] ints = b.asPrimitiveArray();
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   491
            Arrays.sort(ints);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   492
            downstream.begin(ints.length);
48593
fca88bbbafb9 8075939: Stream.flatMap() causes breaking of short-circuiting of terminal operations
psandoz
parents: 47216
diff changeset
   493
            if (!cancellationRequestedCalled) {
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   494
                for (int anInt : ints)
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   495
                    downstream.accept(anInt);
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   496
            }
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   497
            else {
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   498
                for (int anInt : ints) {
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   499
                    if (downstream.cancellationRequested()) break;
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   500
                    downstream.accept(anInt);
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   501
                }
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   502
            }
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   503
            downstream.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   504
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   505
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   506
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   507
        public void accept(int t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   508
            b.accept(t);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   509
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   510
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   511
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   512
    /**
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   513
     * Abstract {@link Sink} for implementing sort on long streams.
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   514
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   515
    private abstract static class AbstractLongSortingSink extends Sink.ChainedLong<Long> {
48593
fca88bbbafb9 8075939: Stream.flatMap() causes breaking of short-circuiting of terminal operations
psandoz
parents: 47216
diff changeset
   516
        // true if cancellationRequested() has been called
fca88bbbafb9 8075939: Stream.flatMap() causes breaking of short-circuiting of terminal operations
psandoz
parents: 47216
diff changeset
   517
        protected boolean cancellationRequestedCalled;
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   518
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   519
        AbstractLongSortingSink(Sink<? super Long> downstream) {
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   520
            super(downstream);
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   521
        }
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   522
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   523
        @Override
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   524
        public final boolean cancellationRequested() {
48593
fca88bbbafb9 8075939: Stream.flatMap() causes breaking of short-circuiting of terminal operations
psandoz
parents: 47216
diff changeset
   525
            cancellationRequestedCalled = true;
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   526
            return false;
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   527
        }
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   528
    }
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   529
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   530
    /**
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   531
     * {@link Sink} for implementing sort on SIZED long streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   532
     */
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   533
    private static final class SizedLongSortingSink extends AbstractLongSortingSink {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   534
        private long[] array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   535
        private int offset;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   536
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   537
        SizedLongSortingSink(Sink<? super Long> downstream) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   538
            super(downstream);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   539
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   540
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   541
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   542
        public void begin(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   543
            if (size >= Nodes.MAX_ARRAY_SIZE)
20507
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 20503
diff changeset
   544
                throw new IllegalArgumentException(Nodes.BAD_SIZE);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   545
            array = new long[(int) size];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   546
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   547
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   548
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   549
        public void end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   550
            Arrays.sort(array, 0, offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   551
            downstream.begin(offset);
48593
fca88bbbafb9 8075939: Stream.flatMap() causes breaking of short-circuiting of terminal operations
psandoz
parents: 47216
diff changeset
   552
            if (!cancellationRequestedCalled) {
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   553
                for (int i = 0; i < offset; i++)
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   554
                    downstream.accept(array[i]);
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   555
            }
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   556
            else {
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   557
                for (int i = 0; i < offset && !downstream.cancellationRequested(); i++)
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   558
                    downstream.accept(array[i]);
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   559
            }
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   560
            downstream.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   561
            array = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   562
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   563
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   564
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   565
        public void accept(long t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   566
            array[offset++] = t;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   567
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   568
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   569
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   570
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   571
     * {@link Sink} for implementing sort on long streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   572
     */
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   573
    private static final class LongSortingSink extends AbstractLongSortingSink {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   574
        private SpinedBuffer.OfLong b;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   575
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   576
        LongSortingSink(Sink<? super Long> sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   577
            super(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   578
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   579
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   580
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   581
        public void begin(long size) {
20503
074dd13d9cdf 8025535: Unsafe typecast in java.util.stream.SortedOps
psandoz
parents: 19593
diff changeset
   582
            if (size >= Nodes.MAX_ARRAY_SIZE)
20507
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 20503
diff changeset
   583
                throw new IllegalArgumentException(Nodes.BAD_SIZE);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   584
            b = (size > 0) ? new SpinedBuffer.OfLong((int) size) : new SpinedBuffer.OfLong();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   585
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   586
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   587
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   588
        public void end() {
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 17182
diff changeset
   589
            long[] longs = b.asPrimitiveArray();
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   590
            Arrays.sort(longs);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   591
            downstream.begin(longs.length);
48593
fca88bbbafb9 8075939: Stream.flatMap() causes breaking of short-circuiting of terminal operations
psandoz
parents: 47216
diff changeset
   592
            if (!cancellationRequestedCalled) {
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   593
                for (long aLong : longs)
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   594
                    downstream.accept(aLong);
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   595
            }
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   596
            else {
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   597
                for (long aLong : longs) {
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   598
                    if (downstream.cancellationRequested()) break;
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   599
                    downstream.accept(aLong);
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   600
                }
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   601
            }
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   602
            downstream.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   603
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   604
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   605
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   606
        public void accept(long t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   607
            b.accept(t);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   608
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   609
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   610
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   611
    /**
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   612
     * Abstract {@link Sink} for implementing sort on long streams.
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   613
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   614
    private abstract static class AbstractDoubleSortingSink extends Sink.ChainedDouble<Double> {
48593
fca88bbbafb9 8075939: Stream.flatMap() causes breaking of short-circuiting of terminal operations
psandoz
parents: 47216
diff changeset
   615
        // true if cancellationRequested() has been called
fca88bbbafb9 8075939: Stream.flatMap() causes breaking of short-circuiting of terminal operations
psandoz
parents: 47216
diff changeset
   616
        protected boolean cancellationRequestedCalled;
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   617
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   618
        AbstractDoubleSortingSink(Sink<? super Double> downstream) {
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   619
            super(downstream);
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   620
        }
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   621
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   622
        @Override
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   623
        public final boolean cancellationRequested() {
48593
fca88bbbafb9 8075939: Stream.flatMap() causes breaking of short-circuiting of terminal operations
psandoz
parents: 47216
diff changeset
   624
            cancellationRequestedCalled = true;
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   625
            return false;
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   626
        }
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   627
    }
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   628
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   629
    /**
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   630
     * {@link Sink} for implementing sort on SIZED double streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   631
     */
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   632
    private static final class SizedDoubleSortingSink extends AbstractDoubleSortingSink {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   633
        private double[] array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   634
        private int offset;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   635
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   636
        SizedDoubleSortingSink(Sink<? super Double> downstream) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   637
            super(downstream);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   638
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   639
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   640
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   641
        public void begin(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   642
            if (size >= Nodes.MAX_ARRAY_SIZE)
20507
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 20503
diff changeset
   643
                throw new IllegalArgumentException(Nodes.BAD_SIZE);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   644
            array = new double[(int) size];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   645
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   646
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   647
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   648
        public void end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   649
            Arrays.sort(array, 0, offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   650
            downstream.begin(offset);
48593
fca88bbbafb9 8075939: Stream.flatMap() causes breaking of short-circuiting of terminal operations
psandoz
parents: 47216
diff changeset
   651
            if (!cancellationRequestedCalled) {
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   652
                for (int i = 0; i < offset; i++)
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   653
                    downstream.accept(array[i]);
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   654
            }
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   655
            else {
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   656
                for (int i = 0; i < offset && !downstream.cancellationRequested(); i++)
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   657
                    downstream.accept(array[i]);
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   658
            }
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   659
            downstream.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   660
            array = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   661
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   662
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   663
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   664
        public void accept(double t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   665
            array[offset++] = t;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   666
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   667
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   668
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   669
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   670
     * {@link Sink} for implementing sort on double streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   671
     */
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   672
    private static final class DoubleSortingSink extends AbstractDoubleSortingSink {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   673
        private SpinedBuffer.OfDouble b;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   674
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   675
        DoubleSortingSink(Sink<? super Double> sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   676
            super(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   677
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   678
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   679
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   680
        public void begin(long size) {
20503
074dd13d9cdf 8025535: Unsafe typecast in java.util.stream.SortedOps
psandoz
parents: 19593
diff changeset
   681
            if (size >= Nodes.MAX_ARRAY_SIZE)
20507
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 20503
diff changeset
   682
                throw new IllegalArgumentException(Nodes.BAD_SIZE);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   683
            b = (size > 0) ? new SpinedBuffer.OfDouble((int) size) : new SpinedBuffer.OfDouble();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   684
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   685
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   686
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   687
        public void end() {
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 17182
diff changeset
   688
            double[] doubles = b.asPrimitiveArray();
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   689
            Arrays.sort(doubles);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   690
            downstream.begin(doubles.length);
48593
fca88bbbafb9 8075939: Stream.flatMap() causes breaking of short-circuiting of terminal operations
psandoz
parents: 47216
diff changeset
   691
            if (!cancellationRequestedCalled) {
24258
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   692
                for (double aDouble : doubles)
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   693
                    downstream.accept(aDouble);
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   694
            }
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   695
            else {
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   696
                for (double aDouble : doubles) {
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   697
                    if (downstream.cancellationRequested()) break;
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   698
                    downstream.accept(aDouble);
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   699
                }
0e9ab834f44a 8042355: stream with sorted() causes downstream ops not to be lazy
psandoz
parents: 22297
diff changeset
   700
            }
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   701
            downstream.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   702
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   703
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   704
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   705
        public void accept(double t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   706
            b.accept(t);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   707
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   708
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   709
}