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 |
|
26 package java.util.stream; |
|
27 |
|
28 import java.util.Comparator; |
|
29 import java.util.Iterator; |
|
30 import java.util.Objects; |
|
31 import java.util.Optional; |
|
32 import java.util.Spliterator; |
|
33 import java.util.function.BiConsumer; |
|
34 import java.util.function.BiFunction; |
|
35 import java.util.function.BinaryOperator; |
|
36 import java.util.function.Consumer; |
|
37 import java.util.function.Function; |
|
38 import java.util.function.IntFunction; |
|
39 import java.util.function.Predicate; |
|
40 import java.util.function.Supplier; |
|
41 import java.util.function.ToDoubleFunction; |
|
42 import java.util.function.ToIntFunction; |
|
43 import java.util.function.ToLongFunction; |
|
44 |
|
45 /** |
|
46 * A {@code Stream} implementation that delegates operations to another {@code |
|
47 * Stream}. |
|
48 * |
|
49 * @param <T> type of stream elements for this stream and underlying delegate |
|
50 * stream |
|
51 * |
|
52 * @since 1.8 |
|
53 */ |
|
54 public class DelegatingStream<T> implements Stream<T> { |
|
55 final private Stream<T> delegate; |
|
56 |
|
57 /** |
|
58 * Construct a {@code Stream} that delegates operations to another {@code |
|
59 * Stream}. |
|
60 * |
|
61 * @param delegate the underlying {@link Stream} to which we delegate all |
|
62 * {@code Stream} methods |
|
63 * @throws NullPointerException if the delegate is null |
|
64 */ |
|
65 public DelegatingStream(Stream<T> delegate) { |
|
66 this.delegate = Objects.requireNonNull(delegate); |
|
67 } |
|
68 |
|
69 // -- BaseStream methods -- |
|
70 |
|
71 @Override |
|
72 public Spliterator<T> spliterator() { |
|
73 return delegate.spliterator(); |
|
74 } |
|
75 |
|
76 @Override |
|
77 public boolean isParallel() { |
|
78 return delegate.isParallel(); |
|
79 } |
|
80 |
|
81 @Override |
|
82 public Iterator<T> iterator() { |
|
83 return delegate.iterator(); |
|
84 } |
|
85 |
|
86 // -- Stream methods -- |
|
87 |
|
88 @Override |
|
89 public Stream<T> filter(Predicate<? super T> predicate) { |
|
90 return delegate.filter(predicate); |
|
91 } |
|
92 |
|
93 @Override |
|
94 public <R> Stream<R> map(Function<? super T, ? extends R> mapper) { |
|
95 return delegate.map(mapper); |
|
96 } |
|
97 |
|
98 @Override |
|
99 public IntStream mapToInt(ToIntFunction<? super T> mapper) { |
|
100 return delegate.mapToInt(mapper); |
|
101 } |
|
102 |
|
103 @Override |
|
104 public LongStream mapToLong(ToLongFunction<? super T> mapper) { |
|
105 return delegate.mapToLong(mapper); |
|
106 } |
|
107 |
|
108 @Override |
|
109 public DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper) { |
|
110 return delegate.mapToDouble(mapper); |
|
111 } |
|
112 |
|
113 @Override |
|
114 public <R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper) { |
|
115 return delegate.flatMap(mapper); |
|
116 } |
|
117 |
|
118 @Override |
|
119 public IntStream flatMapToInt(Function<? super T, ? extends IntStream> mapper) { |
|
120 return delegate.flatMapToInt(mapper); |
|
121 } |
|
122 |
|
123 @Override |
|
124 public LongStream flatMapToLong(Function<? super T, ? extends LongStream> mapper) { |
|
125 return delegate.flatMapToLong(mapper); |
|
126 } |
|
127 |
|
128 @Override |
|
129 public DoubleStream flatMapToDouble(Function<? super T, ? extends DoubleStream> mapper) { |
|
130 return delegate.flatMapToDouble(mapper); |
|
131 } |
|
132 |
|
133 @Override |
|
134 public Stream<T> distinct() { |
|
135 return delegate.distinct(); |
|
136 } |
|
137 |
|
138 @Override |
|
139 public Stream<T> sorted() { |
|
140 return delegate.sorted(); |
|
141 } |
|
142 |
|
143 @Override |
|
144 public Stream<T> sorted(Comparator<? super T> comparator) { |
|
145 return delegate.sorted(comparator); |
|
146 } |
|
147 |
|
148 @Override |
|
149 public void forEach(Consumer<? super T> action) { |
|
150 delegate.forEach(action); |
|
151 } |
|
152 |
|
153 @Override |
|
154 public void forEachOrdered(Consumer<? super T> action) { |
|
155 delegate.forEachOrdered(action); |
|
156 } |
|
157 |
|
158 @Override |
|
159 public Stream<T> peek(Consumer<? super T> consumer) { |
|
160 return delegate.peek(consumer); |
|
161 } |
|
162 |
|
163 @Override |
|
164 public Stream<T> limit(long maxSize) { |
|
165 return delegate.limit(maxSize); |
|
166 } |
|
167 |
|
168 @Override |
|
169 public Stream<T> substream(long startingOffset) { |
|
170 return delegate.substream(startingOffset); |
|
171 } |
|
172 |
|
173 @Override |
|
174 public Stream<T> substream(long startingOffset, long endingOffset) { |
|
175 return delegate.substream(startingOffset, endingOffset); |
|
176 } |
|
177 |
|
178 @Override |
|
179 public <A> A[] toArray(IntFunction<A[]> generator) { |
|
180 return delegate.toArray(generator); |
|
181 } |
|
182 |
|
183 @Override |
|
184 public Object[] toArray() { |
|
185 return delegate.toArray(); |
|
186 } |
|
187 |
|
188 @Override |
|
189 public T reduce(T identity, BinaryOperator<T> accumulator) { |
|
190 return delegate.reduce(identity, accumulator); |
|
191 } |
|
192 |
|
193 @Override |
|
194 public Optional<T> reduce(BinaryOperator<T> accumulator) { |
|
195 return delegate.reduce(accumulator); |
|
196 } |
|
197 |
|
198 @Override |
|
199 public <U> U reduce(U identity, BiFunction<U, ? super T, U> accumulator, |
|
200 BinaryOperator<U> combiner) { |
|
201 return delegate.reduce(identity, accumulator, combiner); |
|
202 } |
|
203 |
|
204 @Override |
|
205 public <R> R collect(Supplier<R> resultFactory, |
|
206 BiConsumer<R, ? super T> accumulator, |
|
207 BiConsumer<R, R> combiner) { |
|
208 return delegate.collect(resultFactory, accumulator, combiner); |
|
209 } |
|
210 |
|
211 @Override |
|
212 public <R, A> R collect(Collector<? super T, A, ? extends R> collector) { |
|
213 return delegate.collect(collector); |
|
214 } |
|
215 |
|
216 @Override |
|
217 public Optional<T> max(Comparator<? super T> comparator) { |
|
218 return delegate.max(comparator); |
|
219 } |
|
220 |
|
221 @Override |
|
222 public Optional<T> min(Comparator<? super T> comparator) { |
|
223 return delegate.min(comparator); |
|
224 } |
|
225 |
|
226 @Override |
|
227 public long count() { |
|
228 return delegate.count(); |
|
229 } |
|
230 |
|
231 @Override |
|
232 public boolean anyMatch(Predicate<? super T> predicate) { |
|
233 return delegate.anyMatch(predicate); |
|
234 } |
|
235 |
|
236 @Override |
|
237 public boolean allMatch(Predicate<? super T> predicate) { |
|
238 return delegate.allMatch(predicate); |
|
239 } |
|
240 |
|
241 @Override |
|
242 public boolean noneMatch(Predicate<? super T> predicate) { |
|
243 return delegate.noneMatch(predicate); |
|
244 } |
|
245 |
|
246 @Override |
|
247 public Optional<T> findFirst() { |
|
248 return delegate.findFirst(); |
|
249 } |
|
250 |
|
251 @Override |
|
252 public Optional<T> findAny() { |
|
253 return delegate.findAny(); |
|
254 } |
|
255 |
|
256 @Override |
|
257 public Stream<T> unordered() { |
|
258 return delegate.unordered(); |
|
259 } |
|
260 |
|
261 @Override |
|
262 public Stream<T> sequential() { |
|
263 return delegate.sequential(); |
|
264 } |
|
265 |
|
266 @Override |
|
267 public Stream<T> parallel() { |
|
268 return delegate.parallel(); |
|
269 } |
|
270 } |
|