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.Objects;
  30 import java.util.Optional;
  31 import java.util.Spliterator;
  32 import java.util.function.BiConsumer;
  33 import java.util.function.BiFunction;
  34 import java.util.function.BinaryOperator;
  35 import java.util.function.BooleanSupplier;
  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 delegate operations to another {@code
  47  * Stream}.
  48  *
  49  * @since 1.8
  50  */
  51 public class DelegatingStream<T> implements Stream<T> {
  52     final private Stream<T> delegate;
  53 
  54     /**
  55      * Construct a {@code Stream} that delegates operations to another {@code
  56      * Stream}.
  57      *
  58      * @param delegate The {@link Stream} that actually performs the operation
  59      * @throws NullPointerException if the delegate is null
  60      */
  61     public DelegatingStream(Stream<T> delegate) {
  62         this.delegate = Objects.requireNonNull(delegate);
  63     }
  64 
  65     // -- BaseStream methods --
  66 
  67     public Spliterator<T> spliterator() {
  68         return delegate.spliterator();
  69     }
  70 
  71     public boolean isParallel() {
  72         return delegate.isParallel();
  73     }
  74 
  75     public int getStreamFlags() {
  76         return delegate.getStreamFlags();
  77     }
  78 
  79     // -- Stream methods --
  80 
  81     public Stream<T> filter(Predicate<? super T> predicate) {
  82         return delegate.filter(predicate);
  83     }
  84 
  85     public <R> Stream<R> map(Function<? super T, ? extends R> mapper) {
  86         return delegate.map(mapper);
  87     }
  88 
  89     public IntStream map(ToIntFunction<? super T> mapper) {
  90         return delegate.map(mapper);
  91     }
  92 
  93     public LongStream map(ToLongFunction<? super T> mapper) {
  94         return delegate.map(mapper);
  95     }
  96 
  97     public DoubleStream map(ToDoubleFunction<? super T> mapper) {
  98         return delegate.map(mapper);
  99     }
 100 
 101     public <R> Stream<R> explode(BiConsumer<Stream.Downstream<R>, ? super T> exploder) {
 102         return delegate.explode(exploder);
 103     }
 104 
 105     public Stream<T> distinct() {
 106         return delegate.distinct();
 107     }
 108 
 109     public Stream<T> sorted() {
 110         return delegate.sorted();
 111     }
 112 
 113     public Stream<T> sorted(Comparator<? super T> comparator) {
 114         return delegate.sorted(comparator);
 115     }
 116 
 117     public void forEach(Consumer<? super T> consumer) {
 118         delegate.forEach(consumer);
 119     }
 120 
 121     public void forEachUntil(Consumer<? super T> consumer, BooleanSupplier until) {
 122         delegate.forEachUntil(consumer, until);
 123     }
 124 
 125     public Stream<T> peek(Consumer<? super T> consumer) {
 126         return delegate.peek(consumer);
 127     }
 128 
 129     public Stream<T> limit(long maxSize) {
 130         return delegate.limit(maxSize);
 131     }
 132 
 133     public Stream<T> substream(long startingOffset) {
 134         return delegate.substream(startingOffset);
 135     }
 136 
 137     public Stream<T> substream(long startingOffset, long endingOffset) {
 138         return delegate.substream(startingOffset, endingOffset);
 139     }
 140 
 141     public <A> A[] toArray(IntFunction<A[]> generator) {
 142         return delegate.toArray(generator);
 143     }
 144 
 145     public T reduce(T identity, BinaryOperator<T> reducer) {
 146         return delegate.reduce(identity, reducer);
 147     }
 148 
 149     public Optional<T> reduce(BinaryOperator<T> reducer) {
 150         return delegate.reduce(reducer);
 151     }
 152 
 153     public <U> U reduce(U identity, BiFunction<U, ? super T, U> accumulator,
 154                         BinaryOperator<U> reducer) {
 155         return delegate.reduce(identity, accumulator, reducer);
 156     }
 157 
 158     public <R> R collect(Supplier<R> resultFactory,
 159                          BiConsumer<R, ? super T> accumulator,
 160                          BiConsumer<R, R> reducer) {
 161         return delegate.collect(resultFactory, accumulator, reducer);
 162     }
 163 
 164     public <R> R collect(Collector<? super T, R> collector) {
 165         return delegate.collect(collector);
 166     }
 167 
 168     public <R> R collectUnordered(Collector<? super T, R> collector) {
 169         return delegate.collectUnordered(collector);
 170     }
 171 
 172     public boolean anyMatch(Predicate<? super T> predicate) {
 173         return delegate.anyMatch(predicate);
 174     }
 175 
 176     public boolean allMatch(Predicate<? super T> predicate) {
 177         return delegate.allMatch(predicate);
 178     }
 179 
 180     public boolean noneMatch(Predicate<? super T> predicate) {
 181         return delegate.noneMatch(predicate);
 182     }
 183 
 184     public Optional<T> findFirst() {
 185         return delegate.findFirst();
 186     }
 187 
 188     public Optional<T> findAny() {
 189         return delegate.findAny();
 190     }
 191 
 192     public Stream<T> sequential() {
 193         return delegate.sequential();
 194     }
 195 
 196     public Stream<T> parallel() {
 197         return delegate.parallel();
 198     }
 199 }