10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package java.util.stream;
26
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.Comparator;
30 import java.util.Comparators;
31 import java.util.Objects;
32 import java.util.Spliterator;
33 import java.util.concurrent.ForkJoinTask;
34 import java.util.function.IntFunction;
35
36
37 /**
38 * Factory methods for transforming streams into sorted streams.
39 *
40 * @since 1.8
41 */
42 final class SortedOps {
43
44 private SortedOps() { }
45
46 /**
47 * Appends a "sorted" operation to the provided stream.
48 *
49 * @param <T> the type of both input and output elements
50 * @param upstream a reference stream with element type T
97
98 /**
99 * Specialized subtype for sorting reference streams
100 */
101 private static final class OfRef<T> extends ReferencePipeline.StatefulOp<T, T> {
102 /**
103 * Comparator used for sorting
104 */
105 private final boolean isNaturalSort;
106 private final Comparator<? super T> comparator;
107
108 /**
109 * Sort using natural order of {@literal <T>} which must be
110 * {@code Comparable}.
111 */
112 OfRef(AbstractPipeline<?, T, ?> upstream) {
113 super(upstream, StreamShape.REFERENCE,
114 StreamOpFlag.IS_ORDERED | StreamOpFlag.IS_SORTED);
115 this.isNaturalSort = true;
116 // Will throw CCE when we try to sort if T is not Comparable
117 this.comparator = (Comparator<? super T>) Comparators.naturalOrder();
118 }
119
120 /**
121 * Sort using the provided comparator.
122 *
123 * @param comparator The comparator to be used to evaluate ordering.
124 */
125 OfRef(AbstractPipeline<?, T, ?> upstream, Comparator<? super T> comparator) {
126 super(upstream, StreamShape.REFERENCE,
127 StreamOpFlag.IS_ORDERED | StreamOpFlag.NOT_SORTED);
128 this.isNaturalSort = false;
129 this.comparator = Objects.requireNonNull(comparator);
130 }
131
132 @Override
133 public Sink<T> opWrapSink(int flags, Sink sink) {
134 Objects.requireNonNull(sink);
135
136 // If the input is already naturally sorted and this operation
137 // also naturally sorted then this is a no-op
|
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package java.util.stream;
26
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.Comparator;
30 import java.util.Objects;
31 import java.util.Spliterator;
32 import java.util.concurrent.ForkJoinTask;
33 import java.util.function.IntFunction;
34
35
36 /**
37 * Factory methods for transforming streams into sorted streams.
38 *
39 * @since 1.8
40 */
41 final class SortedOps {
42
43 private SortedOps() { }
44
45 /**
46 * Appends a "sorted" operation to the provided stream.
47 *
48 * @param <T> the type of both input and output elements
49 * @param upstream a reference stream with element type T
96
97 /**
98 * Specialized subtype for sorting reference streams
99 */
100 private static final class OfRef<T> extends ReferencePipeline.StatefulOp<T, T> {
101 /**
102 * Comparator used for sorting
103 */
104 private final boolean isNaturalSort;
105 private final Comparator<? super T> comparator;
106
107 /**
108 * Sort using natural order of {@literal <T>} which must be
109 * {@code Comparable}.
110 */
111 OfRef(AbstractPipeline<?, T, ?> upstream) {
112 super(upstream, StreamShape.REFERENCE,
113 StreamOpFlag.IS_ORDERED | StreamOpFlag.IS_SORTED);
114 this.isNaturalSort = true;
115 // Will throw CCE when we try to sort if T is not Comparable
116 this.comparator = (Comparator<? super T>) Comparator.naturalOrder();
117 }
118
119 /**
120 * Sort using the provided comparator.
121 *
122 * @param comparator The comparator to be used to evaluate ordering.
123 */
124 OfRef(AbstractPipeline<?, T, ?> upstream, Comparator<? super T> comparator) {
125 super(upstream, StreamShape.REFERENCE,
126 StreamOpFlag.IS_ORDERED | StreamOpFlag.NOT_SORTED);
127 this.isNaturalSort = false;
128 this.comparator = Objects.requireNonNull(comparator);
129 }
130
131 @Override
132 public Sink<T> opWrapSink(int flags, Sink sink) {
133 Objects.requireNonNull(sink);
134
135 // If the input is already naturally sorted and this operation
136 // also naturally sorted then this is a no-op
|