author | sundar |
Thu, 25 Jul 2013 20:10:48 +0530 | |
changeset 19101 | 3e6fe94f02a8 |
parent 17913 | 0652101517a3 |
permissions | -rw-r--r-- |
17195 | 1 |
/* |
2 |
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
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.function.Consumer; |
|
28 |
import java.util.function.DoubleConsumer; |
|
29 |
import java.util.function.IntConsumer; |
|
30 |
import java.util.function.LongConsumer; |
|
31 |
||
32 |
/** |
|
33 |
* A mutable builder for a {@code Stream}. This allows the creation of a |
|
34 |
* {@code Stream} by generating elements individually and adding them to the |
|
35 |
* {@code StreamBuilder} (without the copying overhead that comes from using |
|
36 |
* an {@code ArrayList} as a temporary buffer.) |
|
37 |
* |
|
38 |
* <p>A {@code StreamBuilder} has a lifecycle, where it starts in a building |
|
39 |
* phase, during which elements can be added, and then transitions to a built |
|
40 |
* phase, after which elements may not be added. The built phase begins |
|
17913
0652101517a3
8014409: Spec typo: extra } in the spec for j.u.s.StreamBuilder
psandoz
parents:
17195
diff
changeset
|
41 |
* when the {@link #build()} method is called, which creates an ordered |
17195 | 42 |
* {@code Stream} whose elements are the elements that were added to the stream |
43 |
* builder, in the order they were added. |
|
44 |
* |
|
45 |
* <p>Primitive specializations of {@code StreamBuilder} are provided |
|
46 |
* for {@link OfInt int}, {@link OfLong long}, and {@link OfDouble double} |
|
47 |
* values. |
|
48 |
* |
|
49 |
* @param <T> the type of stream elements |
|
50 |
* @see Stream#builder() |
|
51 |
* @since 1.8 |
|
52 |
*/ |
|
53 |
public interface StreamBuilder<T> extends Consumer<T> { |
|
54 |
||
55 |
/** |
|
56 |
* Adds an element to the stream being built. |
|
57 |
* |
|
58 |
* @throws IllegalStateException if the builder has already transitioned to |
|
59 |
* the built state |
|
60 |
*/ |
|
61 |
@Override |
|
62 |
void accept(T t); |
|
63 |
||
64 |
/** |
|
65 |
* Adds an element to the stream being built. |
|
66 |
* |
|
67 |
* @implSpec |
|
68 |
* The default implementation behaves as if: |
|
69 |
* <pre>{@code |
|
70 |
* accept(t) |
|
71 |
* return this; |
|
72 |
* }</pre> |
|
73 |
* |
|
74 |
* @param t the element to add |
|
75 |
* @return {@code this} builder |
|
76 |
* @throws IllegalStateException if the builder has already transitioned to |
|
77 |
* the built state |
|
78 |
*/ |
|
79 |
default StreamBuilder<T> add(T t) { |
|
80 |
accept(t); |
|
81 |
return this; |
|
82 |
} |
|
83 |
||
84 |
/** |
|
85 |
* Builds the stream, transitioning this builder to the built state. |
|
86 |
* An {@code IllegalStateException} is thrown if there are further attempts |
|
87 |
* to operate on the builder after it has entered the built state. |
|
88 |
* |
|
89 |
* @return the built stream |
|
90 |
* @throws IllegalStateException if the builder has already transitioned to |
|
91 |
* the built state |
|
92 |
*/ |
|
93 |
Stream<T> build(); |
|
94 |
||
95 |
/** |
|
96 |
* A mutable builder for an {@code IntStream}. |
|
97 |
* |
|
98 |
* <p>A stream builder has a lifecycle, where it starts in a building |
|
99 |
* phase, during which elements can be added, and then transitions to a |
|
100 |
* built phase, after which elements may not be added. The built phase |
|
17913
0652101517a3
8014409: Spec typo: extra } in the spec for j.u.s.StreamBuilder
psandoz
parents:
17195
diff
changeset
|
101 |
* begins when the {@link #build()} method is called, which creates an |
17195 | 102 |
* ordered stream whose elements are the elements that were added to the |
103 |
* stream builder, in the order they were added. |
|
104 |
* |
|
105 |
* @see IntStream#builder() |
|
106 |
* @since 1.8 |
|
107 |
*/ |
|
108 |
interface OfInt extends IntConsumer { |
|
109 |
||
110 |
/** |
|
111 |
* Adds an element to the stream being built. |
|
112 |
* |
|
113 |
* @throws IllegalStateException if the builder has already transitioned |
|
114 |
* to the built state |
|
115 |
*/ |
|
116 |
@Override |
|
117 |
void accept(int t); |
|
118 |
||
119 |
/** |
|
120 |
* Adds an element to the stream being built. |
|
121 |
* |
|
122 |
* @implSpec |
|
123 |
* The default implementation behaves as if: |
|
124 |
* <pre>{@code |
|
125 |
* accept(t) |
|
126 |
* return this; |
|
127 |
* }</pre> |
|
128 |
* |
|
129 |
* @param t the element to add |
|
130 |
* @return {@code this} builder |
|
131 |
* @throws IllegalStateException if the builder has already transitioned |
|
132 |
* to the built state |
|
133 |
*/ |
|
134 |
default StreamBuilder.OfInt add(int t) { |
|
135 |
accept(t); |
|
136 |
return this; |
|
137 |
} |
|
138 |
||
139 |
/** |
|
140 |
* Builds the stream, transitioning this builder to the built state. |
|
141 |
* An {@code IllegalStateException} is thrown if there are further |
|
142 |
* attempts to operate on the builder after it has entered the built |
|
143 |
* state. |
|
144 |
* |
|
145 |
* @return the built stream |
|
146 |
* @throws IllegalStateException if the builder has already transitioned to |
|
147 |
* the built state |
|
148 |
*/ |
|
149 |
IntStream build(); |
|
150 |
} |
|
151 |
||
152 |
/** |
|
153 |
* A mutable builder for a {@code LongStream}. |
|
154 |
* |
|
155 |
* <p>A stream builder has a lifecycle, where it starts in a building |
|
156 |
* phase, during which elements can be added, and then transitions to a |
|
157 |
* built phase, after which elements may not be added. The built phase |
|
17913
0652101517a3
8014409: Spec typo: extra } in the spec for j.u.s.StreamBuilder
psandoz
parents:
17195
diff
changeset
|
158 |
* begins when the {@link #build()} method is called, which creates an |
17195 | 159 |
* ordered stream whose elements are the elements that were added to the |
160 |
* stream builder, in the order they were added. |
|
161 |
* |
|
162 |
* @see LongStream#builder() |
|
163 |
* @since 1.8 |
|
164 |
*/ |
|
165 |
interface OfLong extends LongConsumer { |
|
166 |
||
167 |
/** |
|
168 |
* Adds an element to the stream being built. |
|
169 |
* |
|
170 |
* @throws IllegalStateException if the builder has already transitioned |
|
171 |
* to the built state |
|
172 |
*/ |
|
173 |
@Override |
|
174 |
void accept(long t); |
|
175 |
||
176 |
/** |
|
177 |
* Adds an element to the stream being built. |
|
178 |
* |
|
179 |
* @implSpec |
|
180 |
* The default implementation behaves as if: |
|
181 |
* <pre>{@code |
|
182 |
* accept(t) |
|
183 |
* return this; |
|
184 |
* }</pre> |
|
185 |
* |
|
186 |
* @param t the element to add |
|
187 |
* @return {@code this} builder |
|
188 |
* @throws IllegalStateException if the builder has already transitioned |
|
189 |
* to the built state |
|
190 |
*/ |
|
191 |
default StreamBuilder.OfLong add(long t) { |
|
192 |
accept(t); |
|
193 |
return this; |
|
194 |
} |
|
195 |
||
196 |
/** |
|
197 |
* Builds the stream, transitioning this builder to the built state. |
|
198 |
* An {@code IllegalStateException} is thrown if there are further |
|
199 |
* attempts to operate on the builder after it has entered the built |
|
200 |
* state. |
|
201 |
* |
|
202 |
* @return the built stream |
|
203 |
* @throws IllegalStateException if the builder has already transitioned |
|
204 |
* to the built state |
|
205 |
*/ |
|
206 |
LongStream build(); |
|
207 |
} |
|
208 |
||
209 |
/** |
|
210 |
* A mutable builder for a {@code DoubleStream}. |
|
211 |
* |
|
17913
0652101517a3
8014409: Spec typo: extra } in the spec for j.u.s.StreamBuilder
psandoz
parents:
17195
diff
changeset
|
212 |
* <p>A stream builder has a lifecycle, where it starts in a building |
0652101517a3
8014409: Spec typo: extra } in the spec for j.u.s.StreamBuilder
psandoz
parents:
17195
diff
changeset
|
213 |
* phase, during which elements can be added, and then transitions to a |
0652101517a3
8014409: Spec typo: extra } in the spec for j.u.s.StreamBuilder
psandoz
parents:
17195
diff
changeset
|
214 |
* built phase, after which elements may not be added. The built phase |
0652101517a3
8014409: Spec typo: extra } in the spec for j.u.s.StreamBuilder
psandoz
parents:
17195
diff
changeset
|
215 |
* begins when the {@link #build()} method is called, which creates an |
0652101517a3
8014409: Spec typo: extra } in the spec for j.u.s.StreamBuilder
psandoz
parents:
17195
diff
changeset
|
216 |
* ordered stream whose elements are the elements that were added to the |
0652101517a3
8014409: Spec typo: extra } in the spec for j.u.s.StreamBuilder
psandoz
parents:
17195
diff
changeset
|
217 |
* stream builder, in the order they were added. |
0652101517a3
8014409: Spec typo: extra } in the spec for j.u.s.StreamBuilder
psandoz
parents:
17195
diff
changeset
|
218 |
* |
17195 | 219 |
* @see LongStream#builder() |
220 |
* @since 1.8 |
|
221 |
*/ |
|
222 |
interface OfDouble extends DoubleConsumer { |
|
223 |
||
224 |
/** |
|
225 |
* Adds an element to the stream being built. |
|
226 |
* |
|
227 |
* @throws IllegalStateException if the builder has already transitioned |
|
228 |
* to the built state |
|
229 |
*/ |
|
230 |
@Override |
|
231 |
void accept(double t); |
|
232 |
||
233 |
/** |
|
234 |
* Adds an element to the stream being built. |
|
235 |
* |
|
236 |
* @implSpec |
|
237 |
* The default implementation behaves as if: |
|
238 |
* <pre>{@code |
|
239 |
* accept(t) |
|
240 |
* return this; |
|
241 |
* }</pre> |
|
242 |
* |
|
243 |
* @param t the element to add |
|
244 |
* @return {@code this} builder |
|
245 |
* @throws IllegalStateException if the builder has already transitioned |
|
246 |
* to the built state |
|
247 |
*/ |
|
248 |
default StreamBuilder.OfDouble add(double t) { |
|
249 |
accept(t); |
|
250 |
return this; |
|
251 |
} |
|
252 |
||
253 |
/** |
|
254 |
* Builds the stream, transitioning this builder to the built state. |
|
255 |
* An {@code IllegalStateException} is thrown if there are further |
|
256 |
* attempts to operate on the builder after it has entered the built |
|
257 |
* state. |
|
258 |
* |
|
259 |
* @return the built stream |
|
260 |
* @throws IllegalStateException if the builder has already transitioned |
|
261 |
* to the built state |
|
262 |
*/ |
|
263 |
DoubleStream build(); |
|
264 |
} |
|
265 |
} |