author | psandoz |
Thu, 16 Jan 2014 18:20:31 +0100 | |
changeset 22289 | bb9c71b84919 |
parent 21339 | 20e8b81964d5 |
permissions | -rw-r--r-- |
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
1 |
/* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
2 |
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
4 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
10 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
15 |
* accompanied this code). |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
16 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
20 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
23 |
* questions. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
24 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
25 |
package java.util.stream; |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
26 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
27 |
import java.util.Spliterator; |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
28 |
import java.util.function.Consumer; |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
29 |
import java.util.function.DoubleConsumer; |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
30 |
import java.util.function.IntConsumer; |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
31 |
import java.util.function.IntFunction; |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
32 |
import java.util.function.LongConsumer; |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
33 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
34 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
35 |
* An immutable container for describing an ordered sequence of elements of some |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
36 |
* type {@code T}. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
37 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
38 |
* <p>A {@code Node} contains a fixed number of elements, which can be accessed |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
39 |
* via the {@link #count}, {@link #spliterator}, {@link #forEach}, |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
40 |
* {@link #asArray}, or {@link #copyInto} methods. A {@code Node} may have zero |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
41 |
* or more child {@code Node}s; if it has no children (accessed via |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
42 |
* {@link #getChildCount} and {@link #getChild(int)}, it is considered <em>flat |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
43 |
* </em> or a <em>leaf</em>; if it has children, it is considered an |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
44 |
* <em>internal</em> node. The size of an internal node is the sum of sizes of |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
45 |
* its children. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
46 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
47 |
* @apiNote |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
48 |
* <p>A {@code Node} typically does not store the elements directly, but instead |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
49 |
* mediates access to one or more existing (effectively immutable) data |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
50 |
* structures such as a {@code Collection}, array, or a set of other |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
51 |
* {@code Node}s. Commonly {@code Node}s are formed into a tree whose shape |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
52 |
* corresponds to the computation tree that produced the elements that are |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
53 |
* contained in the leaf nodes. The use of {@code Node} within the stream |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
54 |
* framework is largely to avoid copying data unnecessarily during parallel |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
55 |
* operations. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
56 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
57 |
* @param <T> the type of elements. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
58 |
* @since 1.8 |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
59 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
60 |
interface Node<T> { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
61 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
62 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
63 |
* Returns a {@link Spliterator} describing the elements contained in this |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
64 |
* {@code Node}. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
65 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
66 |
* @return a {@code Spliterator} describing the elements contained in this |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
67 |
* {@code Node} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
68 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
69 |
Spliterator<T> spliterator(); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
70 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
71 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
72 |
* Traverses the elements of this node, and invoke the provided |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
73 |
* {@code Consumer} with each element. Elements are provided in encounter |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
74 |
* order if the source for the {@code Node} has a defined encounter order. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
75 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
76 |
* @param consumer a {@code Consumer} that is to be invoked with each |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
77 |
* element in this {@code Node} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
78 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
79 |
void forEach(Consumer<? super T> consumer); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
80 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
81 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
82 |
* Returns the number of child nodes of this node. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
83 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
84 |
* @implSpec The default implementation returns zero. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
85 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
86 |
* @return the number of child nodes |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
87 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
88 |
default int getChildCount() { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
89 |
return 0; |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
90 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
91 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
92 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
93 |
* Retrieves the child {@code Node} at a given index. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
94 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
95 |
* @implSpec The default implementation always throws |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
96 |
* {@code IndexOutOfBoundsException}. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
97 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
98 |
* @param i the index to the child node |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
99 |
* @return the child node |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
100 |
* @throws IndexOutOfBoundsException if the index is less than 0 or greater |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
101 |
* than or equal to the number of child nodes |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
102 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
103 |
default Node<T> getChild(int i) { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
104 |
throw new IndexOutOfBoundsException(); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
105 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
106 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
107 |
/** |
18527 | 108 |
* Return a node describing a subsequence of the elements of this node, |
109 |
* starting at the given inclusive start offset and ending at the given |
|
110 |
* exclusive end offset. |
|
111 |
* |
|
112 |
* @param from The (inclusive) starting offset of elements to include, must |
|
113 |
* be in range 0..count(). |
|
114 |
* @param to The (exclusive) end offset of elements to include, must be |
|
115 |
* in range 0..count(). |
|
116 |
* @param generator A function to be used to create a new array, if needed, |
|
117 |
* for reference nodes. |
|
118 |
* @return the truncated node |
|
119 |
*/ |
|
120 |
default Node<T> truncate(long from, long to, IntFunction<T[]> generator) { |
|
121 |
if (from == 0 && to == count()) |
|
122 |
return this; |
|
123 |
Spliterator<T> spliterator = spliterator(); |
|
124 |
long size = to - from; |
|
125 |
Node.Builder<T> nodeBuilder = Nodes.builder(size, generator); |
|
126 |
nodeBuilder.begin(size); |
|
127 |
for (int i = 0; i < from && spliterator.tryAdvance(e -> { }); i++) { } |
|
128 |
for (int i = 0; (i < size) && spliterator.tryAdvance(nodeBuilder); i++) { } |
|
129 |
nodeBuilder.end(); |
|
130 |
return nodeBuilder.build(); |
|
131 |
} |
|
132 |
||
133 |
/** |
|
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
134 |
* Provides an array view of the contents of this node. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
135 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
136 |
* <p>Depending on the underlying implementation, this may return a |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
137 |
* reference to an internal array rather than a copy. Since the returned |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
138 |
* array may be shared, the returned array should not be modified. The |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
139 |
* {@code generator} function may be consulted to create the array if a new |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
140 |
* array needs to be created. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
141 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
142 |
* @param generator a factory function which takes an integer parameter and |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
143 |
* returns a new, empty array of that size and of the appropriate |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
144 |
* array type |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
145 |
* @return an array containing the contents of this {@code Node} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
146 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
147 |
T[] asArray(IntFunction<T[]> generator); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
148 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
149 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
150 |
* Copies the content of this {@code Node} into an array, starting at a |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
151 |
* given offset into the array. It is the caller's responsibility to ensure |
20507
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
18527
diff
changeset
|
152 |
* there is sufficient room in the array, otherwise unspecified behaviour |
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
18527
diff
changeset
|
153 |
* will occur if the array length is less than the number of elements |
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
18527
diff
changeset
|
154 |
* contained in this node. |
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
155 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
156 |
* @param array the array into which to copy the contents of this |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
157 |
* {@code Node} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
158 |
* @param offset the starting offset within the array |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
159 |
* @throws IndexOutOfBoundsException if copying would cause access of data |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
160 |
* outside array bounds |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
161 |
* @throws NullPointerException if {@code array} is {@code null} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
162 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
163 |
void copyInto(T[] array, int offset); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
164 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
165 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
166 |
* Gets the {@code StreamShape} associated with this {@code Node}. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
167 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
168 |
* @implSpec The default in {@code Node} returns |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
169 |
* {@code StreamShape.REFERENCE} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
170 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
171 |
* @return the stream shape associated with this node |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
172 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
173 |
default StreamShape getShape() { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
174 |
return StreamShape.REFERENCE; |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
175 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
176 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
177 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
178 |
* Returns the number of elements contained in this node. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
179 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
180 |
* @return the number of elements contained in this node |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
181 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
182 |
long count(); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
183 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
184 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
185 |
* A mutable builder for a {@code Node} that implements {@link Sink}, which |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
186 |
* builds a flat node containing the elements that have been pushed to it. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
187 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
188 |
interface Builder<T> extends Sink<T> { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
189 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
190 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
191 |
* Builds the node. Should be called after all elements have been |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
192 |
* pushed and signalled with an invocation of {@link Sink#end()}. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
193 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
194 |
* @return the resulting {@code Node} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
195 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
196 |
Node<T> build(); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
197 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
198 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
199 |
* Specialized @{code Node.Builder} for int elements |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
200 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
201 |
interface OfInt extends Node.Builder<Integer>, Sink.OfInt { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
202 |
@Override |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
203 |
Node.OfInt build(); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
204 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
205 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
206 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
207 |
* Specialized @{code Node.Builder} for long elements |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
208 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
209 |
interface OfLong extends Node.Builder<Long>, Sink.OfLong { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
210 |
@Override |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
211 |
Node.OfLong build(); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
212 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
213 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
214 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
215 |
* Specialized @{code Node.Builder} for double elements |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
216 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
217 |
interface OfDouble extends Node.Builder<Double>, Sink.OfDouble { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
218 |
@Override |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
219 |
Node.OfDouble build(); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
220 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
221 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
222 |
|
18527 | 223 |
public interface OfPrimitive<T, T_CONS, T_ARR, |
224 |
T_SPLITR extends Spliterator.OfPrimitive<T, T_CONS, T_SPLITR>, |
|
225 |
T_NODE extends OfPrimitive<T, T_CONS, T_ARR, T_SPLITR, T_NODE>> |
|
226 |
extends Node<T> { |
|
227 |
||
228 |
/** |
|
229 |
* {@inheritDoc} |
|
230 |
* |
|
231 |
* @return a {@link Spliterator.OfPrimitive} describing the elements of |
|
232 |
* this node |
|
233 |
*/ |
|
234 |
@Override |
|
235 |
T_SPLITR spliterator(); |
|
236 |
||
237 |
/** |
|
238 |
* Traverses the elements of this node, and invoke the provided |
|
239 |
* {@code action} with each element. |
|
240 |
* |
|
241 |
* @param action a consumer that is to be invoked with each |
|
242 |
* element in this {@code Node.OfPrimitive} |
|
243 |
*/ |
|
21339 | 244 |
@SuppressWarnings("overloads") |
18527 | 245 |
void forEach(T_CONS action); |
246 |
||
247 |
@Override |
|
248 |
default T_NODE getChild(int i) { |
|
249 |
throw new IndexOutOfBoundsException(); |
|
250 |
} |
|
251 |
||
252 |
T_NODE truncate(long from, long to, IntFunction<T[]> generator); |
|
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
253 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
254 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
255 |
* {@inheritDoc} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
256 |
* |
18527 | 257 |
* @implSpec the default implementation invokes the generator to create |
258 |
* an instance of a boxed primitive array with a length of |
|
259 |
* {@link #count()} and then invokes {@link #copyInto(T[], int)} with |
|
260 |
* that array at an offset of 0. |
|
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
261 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
262 |
@Override |
18527 | 263 |
default T[] asArray(IntFunction<T[]> generator) { |
20507
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
18527
diff
changeset
|
264 |
if (java.util.stream.Tripwire.ENABLED) |
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
18527
diff
changeset
|
265 |
java.util.stream.Tripwire.trip(getClass(), "{0} calling Node.OfPrimitive.asArray"); |
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
18527
diff
changeset
|
266 |
|
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
18527
diff
changeset
|
267 |
long size = count(); |
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
18527
diff
changeset
|
268 |
if (size >= Nodes.MAX_ARRAY_SIZE) |
8498104f92c3
8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents:
18527
diff
changeset
|
269 |
throw new IllegalArgumentException(Nodes.BAD_SIZE); |
18527 | 270 |
T[] boxed = generator.apply((int) count()); |
271 |
copyInto(boxed, 0); |
|
272 |
return boxed; |
|
273 |
} |
|
274 |
||
275 |
/** |
|
276 |
* Views this node as a primitive array. |
|
277 |
* |
|
278 |
* <p>Depending on the underlying implementation this may return a |
|
279 |
* reference to an internal array rather than a copy. It is the callers |
|
280 |
* responsibility to decide if either this node or the array is utilized |
|
281 |
* as the primary reference for the data.</p> |
|
282 |
* |
|
283 |
* @return an array containing the contents of this {@code Node} |
|
284 |
*/ |
|
285 |
T_ARR asPrimitiveArray(); |
|
286 |
||
287 |
/** |
|
288 |
* Creates a new primitive array. |
|
289 |
* |
|
290 |
* @param count the length of the primitive array. |
|
291 |
* @return the new primitive array. |
|
292 |
*/ |
|
293 |
T_ARR newArray(int count); |
|
294 |
||
295 |
/** |
|
296 |
* Copies the content of this {@code Node} into a primitive array, |
|
297 |
* starting at a given offset into the array. It is the caller's |
|
298 |
* responsibility to ensure there is sufficient room in the array. |
|
299 |
* |
|
300 |
* @param array the array into which to copy the contents of this |
|
301 |
* {@code Node} |
|
302 |
* @param offset the starting offset within the array |
|
303 |
* @throws IndexOutOfBoundsException if copying would cause access of |
|
304 |
* data outside array bounds |
|
305 |
* @throws NullPointerException if {@code array} is {@code null} |
|
306 |
*/ |
|
307 |
void copyInto(T_ARR array, int offset); |
|
308 |
} |
|
309 |
||
310 |
/** |
|
311 |
* Specialized {@code Node} for int elements |
|
312 |
*/ |
|
313 |
interface OfInt extends OfPrimitive<Integer, IntConsumer, int[], Spliterator.OfInt, OfInt> { |
|
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
314 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
315 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
316 |
* {@inheritDoc} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
317 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
318 |
* @param consumer a {@code Consumer} that is to be invoked with each |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
319 |
* element in this {@code Node}. If this is an |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
320 |
* {@code IntConsumer}, it is cast to {@code IntConsumer} so the |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
321 |
* elements may be processed without boxing. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
322 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
323 |
@Override |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
324 |
default void forEach(Consumer<? super Integer> consumer) { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
325 |
if (consumer instanceof IntConsumer) { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
326 |
forEach((IntConsumer) consumer); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
327 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
328 |
else { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
329 |
if (Tripwire.ENABLED) |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
330 |
Tripwire.trip(getClass(), "{0} calling Node.OfInt.forEachRemaining(Consumer)"); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
331 |
spliterator().forEachRemaining(consumer); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
332 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
333 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
334 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
335 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
336 |
* {@inheritDoc} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
337 |
* |
18171 | 338 |
* @implSpec the default implementation invokes {@link #asPrimitiveArray()} to |
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
339 |
* obtain an int[] array then and copies the elements from that int[] |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
340 |
* array into the boxed Integer[] array. This is not efficient and it |
18527 | 341 |
* is recommended to invoke {@link #copyInto(Object, int)}. |
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
342 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
343 |
@Override |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
344 |
default void copyInto(Integer[] boxed, int offset) { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
345 |
if (Tripwire.ENABLED) |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
346 |
Tripwire.trip(getClass(), "{0} calling Node.OfInt.copyInto(Integer[], int)"); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
347 |
|
18171 | 348 |
int[] array = asPrimitiveArray(); |
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
349 |
for (int i = 0; i < array.length; i++) { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
350 |
boxed[offset + i] = array[i]; |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
351 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
352 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
353 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
354 |
@Override |
18527 | 355 |
default Node.OfInt truncate(long from, long to, IntFunction<Integer[]> generator) { |
356 |
if (from == 0 && to == count()) |
|
357 |
return this; |
|
358 |
long size = to - from; |
|
359 |
Spliterator.OfInt spliterator = spliterator(); |
|
360 |
Node.Builder.OfInt nodeBuilder = Nodes.intBuilder(size); |
|
361 |
nodeBuilder.begin(size); |
|
362 |
for (int i = 0; i < from && spliterator.tryAdvance((IntConsumer) e -> { }); i++) { } |
|
363 |
for (int i = 0; (i < size) && spliterator.tryAdvance((IntConsumer) nodeBuilder); i++) { } |
|
364 |
nodeBuilder.end(); |
|
365 |
return nodeBuilder.build(); |
|
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
366 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
367 |
|
18527 | 368 |
@Override |
369 |
default int[] newArray(int count) { |
|
370 |
return new int[count]; |
|
371 |
} |
|
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
372 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
373 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
374 |
* {@inheritDoc} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
375 |
* @implSpec The default in {@code Node.OfInt} returns |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
376 |
* {@code StreamShape.INT_VALUE} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
377 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
378 |
default StreamShape getShape() { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
379 |
return StreamShape.INT_VALUE; |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
380 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
381 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
382 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
383 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
384 |
* Specialized {@code Node} for long elements |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
385 |
*/ |
18527 | 386 |
interface OfLong extends OfPrimitive<Long, LongConsumer, long[], Spliterator.OfLong, OfLong> { |
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
387 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
388 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
389 |
* {@inheritDoc} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
390 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
391 |
* @param consumer A {@code Consumer} that is to be invoked with each |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
392 |
* element in this {@code Node}. If this is an |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
393 |
* {@code LongConsumer}, it is cast to {@code LongConsumer} so |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
394 |
* the elements may be processed without boxing. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
395 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
396 |
@Override |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
397 |
default void forEach(Consumer<? super Long> consumer) { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
398 |
if (consumer instanceof LongConsumer) { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
399 |
forEach((LongConsumer) consumer); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
400 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
401 |
else { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
402 |
if (Tripwire.ENABLED) |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
403 |
Tripwire.trip(getClass(), "{0} calling Node.OfLong.forEachRemaining(Consumer)"); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
404 |
spliterator().forEachRemaining(consumer); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
405 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
406 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
407 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
408 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
409 |
* {@inheritDoc} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
410 |
* |
18171 | 411 |
* @implSpec the default implementation invokes {@link #asPrimitiveArray()} |
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
412 |
* to obtain a long[] array then and copies the elements from that |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
413 |
* long[] array into the boxed Long[] array. This is not efficient and |
18527 | 414 |
* it is recommended to invoke {@link #copyInto(Object, int)}. |
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
415 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
416 |
@Override |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
417 |
default void copyInto(Long[] boxed, int offset) { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
418 |
if (Tripwire.ENABLED) |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
419 |
Tripwire.trip(getClass(), "{0} calling Node.OfInt.copyInto(Long[], int)"); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
420 |
|
18171 | 421 |
long[] array = asPrimitiveArray(); |
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
422 |
for (int i = 0; i < array.length; i++) { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
423 |
boxed[offset + i] = array[i]; |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
424 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
425 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
426 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
427 |
@Override |
18527 | 428 |
default Node.OfLong truncate(long from, long to, IntFunction<Long[]> generator) { |
429 |
if (from == 0 && to == count()) |
|
430 |
return this; |
|
431 |
long size = to - from; |
|
432 |
Spliterator.OfLong spliterator = spliterator(); |
|
433 |
Node.Builder.OfLong nodeBuilder = Nodes.longBuilder(size); |
|
434 |
nodeBuilder.begin(size); |
|
435 |
for (int i = 0; i < from && spliterator.tryAdvance((LongConsumer) e -> { }); i++) { } |
|
436 |
for (int i = 0; (i < size) && spliterator.tryAdvance((LongConsumer) nodeBuilder); i++) { } |
|
437 |
nodeBuilder.end(); |
|
438 |
return nodeBuilder.build(); |
|
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
439 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
440 |
|
18527 | 441 |
@Override |
442 |
default long[] newArray(int count) { |
|
443 |
return new long[count]; |
|
444 |
} |
|
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
445 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
446 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
447 |
* {@inheritDoc} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
448 |
* @implSpec The default in {@code Node.OfLong} returns |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
449 |
* {@code StreamShape.LONG_VALUE} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
450 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
451 |
default StreamShape getShape() { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
452 |
return StreamShape.LONG_VALUE; |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
453 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
454 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
455 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
456 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
457 |
* Specialized {@code Node} for double elements |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
458 |
*/ |
18527 | 459 |
interface OfDouble extends OfPrimitive<Double, DoubleConsumer, double[], Spliterator.OfDouble, OfDouble> { |
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
460 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
461 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
462 |
* {@inheritDoc} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
463 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
464 |
* @param consumer A {@code Consumer} that is to be invoked with each |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
465 |
* element in this {@code Node}. If this is an |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
466 |
* {@code DoubleConsumer}, it is cast to {@code DoubleConsumer} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
467 |
* so the elements may be processed without boxing. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
468 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
469 |
@Override |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
470 |
default void forEach(Consumer<? super Double> consumer) { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
471 |
if (consumer instanceof DoubleConsumer) { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
472 |
forEach((DoubleConsumer) consumer); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
473 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
474 |
else { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
475 |
if (Tripwire.ENABLED) |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
476 |
Tripwire.trip(getClass(), "{0} calling Node.OfLong.forEachRemaining(Consumer)"); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
477 |
spliterator().forEachRemaining(consumer); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
478 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
479 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
480 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
481 |
// |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
482 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
483 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
484 |
* {@inheritDoc} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
485 |
* |
18171 | 486 |
* @implSpec the default implementation invokes {@link #asPrimitiveArray()} |
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
487 |
* to obtain a double[] array then and copies the elements from that |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
488 |
* double[] array into the boxed Double[] array. This is not efficient |
18527 | 489 |
* and it is recommended to invoke {@link #copyInto(Object, int)}. |
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
490 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
491 |
@Override |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
492 |
default void copyInto(Double[] boxed, int offset) { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
493 |
if (Tripwire.ENABLED) |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
494 |
Tripwire.trip(getClass(), "{0} calling Node.OfDouble.copyInto(Double[], int)"); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
495 |
|
18171 | 496 |
double[] array = asPrimitiveArray(); |
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
497 |
for (int i = 0; i < array.length; i++) { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
498 |
boxed[offset + i] = array[i]; |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
499 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
500 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
501 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
502 |
@Override |
18527 | 503 |
default Node.OfDouble truncate(long from, long to, IntFunction<Double[]> generator) { |
504 |
if (from == 0 && to == count()) |
|
505 |
return this; |
|
506 |
long size = to - from; |
|
507 |
Spliterator.OfDouble spliterator = spliterator(); |
|
508 |
Node.Builder.OfDouble nodeBuilder = Nodes.doubleBuilder(size); |
|
509 |
nodeBuilder.begin(size); |
|
510 |
for (int i = 0; i < from && spliterator.tryAdvance((DoubleConsumer) e -> { }); i++) { } |
|
511 |
for (int i = 0; (i < size) && spliterator.tryAdvance((DoubleConsumer) nodeBuilder); i++) { } |
|
512 |
nodeBuilder.end(); |
|
513 |
return nodeBuilder.build(); |
|
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
514 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
515 |
|
18527 | 516 |
@Override |
517 |
default double[] newArray(int count) { |
|
518 |
return new double[count]; |
|
519 |
} |
|
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
520 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
521 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
522 |
* {@inheritDoc} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
523 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
524 |
* @implSpec The default in {@code Node.OfDouble} returns |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
525 |
* {@code StreamShape.DOUBLE_VALUE} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
526 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
527 |
default StreamShape getShape() { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
528 |
return StreamShape.DOUBLE_VALUE; |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
529 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
530 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
531 |
} |