All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.zeebe.test.util.stream.StreamWrapper Maven / Gradle / Ivy

There is a newer version: 0.16.4
Show newest version
/*
 * Copyright © 2017 camunda services GmbH ([email protected])
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.zeebe.test.util.stream;

import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Spliterator;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;
import one.util.streamex.StreamEx;

public abstract class StreamWrapper> implements Stream {
  private final Stream wrappedStream;

  public StreamWrapper(Stream wrappedStream) {
    this.wrappedStream = wrappedStream;
  }

  protected abstract S supply(Stream wrappedStream);

  /**
   * Skips elements until the predicate is matched. Retains the first element that matches the
   * predicate.
   */
  public S skipUntil(Predicate predicate) {
    return supply(StreamEx.of(this).dropWhile(predicate.negate()));
  }

  /**
   * short-circuiting operation; limits the stream to the first element that fulfills the predicate
   */
  public S limit(Predicate predicate) {
    // #takeWhile comes with Java >= 9
    return supply(StreamEx.of(this).takeWhileInclusive(predicate.negate()));
  }

  // Helper to extract values

  public boolean exists() {
    return wrappedStream.findFirst().isPresent();
  }

  public T getFirst() {
    return wrappedStream.findFirst().orElseThrow(StreamWrapperException::new);
  }

  public T getLast() {
    final List list = asList();

    if (list.isEmpty()) {
      throw new StreamWrapperException();
    }

    return list.get(list.size() - 1);
  }

  public List asList() {
    return wrappedStream.collect(Collectors.toList());
  }

  // Custom delegate methods to fit generics

  @Override
  public S filter(final Predicate predicate) {
    return supply(wrappedStream.filter(predicate));
  }

  @Override
  public S distinct() {
    return supply(wrappedStream.distinct());
  }

  @Override
  public S sorted() {
    return supply(wrappedStream.sorted());
  }

  @Override
  public S sorted(final Comparator comparator) {
    return supply(wrappedStream.sorted(comparator));
  }

  @Override
  public S peek(final Consumer consumer) {
    return supply(wrappedStream.peek(consumer));
  }

  @Override
  public S limit(final long l) {
    return supply(wrappedStream.limit(l));
  }

  @Override
  public S skip(final long l) {
    return supply(wrappedStream.skip(l));
  }

  @Override
  public S sequential() {
    return supply(wrappedStream.sequential());
  }

  @Override
  public S parallel() {
    return supply(wrappedStream.parallel());
  }

  @Override
  public S unordered() {
    return supply(wrappedStream.unordered());
  }

  @Override
  public S onClose(final Runnable runnable) {
    return supply(wrappedStream.onClose(runnable));
  }

  // Generate delegate methods

  public Iterator iterator() {
    return wrappedStream.iterator();
  }

  public Spliterator spliterator() {
    return wrappedStream.spliterator();
  }

  public boolean isParallel() {
    return wrappedStream.isParallel();
  }

  public void close() {
    wrappedStream.close();
  }

  public  Stream map(Function mapper) {
    return wrappedStream.map(mapper);
  }

  public IntStream mapToInt(ToIntFunction mapper) {
    return wrappedStream.mapToInt(mapper);
  }

  public LongStream mapToLong(ToLongFunction mapper) {
    return wrappedStream.mapToLong(mapper);
  }

  public DoubleStream mapToDouble(ToDoubleFunction mapper) {
    return wrappedStream.mapToDouble(mapper);
  }

  public  Stream flatMap(Function> mapper) {
    return wrappedStream.flatMap(mapper);
  }

  public IntStream flatMapToInt(Function mapper) {
    return wrappedStream.flatMapToInt(mapper);
  }

  public LongStream flatMapToLong(Function mapper) {
    return wrappedStream.flatMapToLong(mapper);
  }

  public DoubleStream flatMapToDouble(Function mapper) {
    return wrappedStream.flatMapToDouble(mapper);
  }

  public void forEach(Consumer action) {
    wrappedStream.forEach(action);
  }

  public void forEachOrdered(Consumer action) {
    wrappedStream.forEachOrdered(action);
  }

  public Object[] toArray() {
    return wrappedStream.toArray();
  }

  public  A[] toArray(IntFunction generator) {
    return wrappedStream.toArray(generator);
  }

  public T reduce(T identity, BinaryOperator accumulator) {
    return wrappedStream.reduce(identity, accumulator);
  }

  public Optional reduce(BinaryOperator accumulator) {
    return wrappedStream.reduce(accumulator);
  }

  public  U reduce(
      U identity, BiFunction accumulator, BinaryOperator combiner) {
    return wrappedStream.reduce(identity, accumulator, combiner);
  }

  public  R collect(
      Supplier supplier, BiConsumer accumulator, BiConsumer combiner) {
    return wrappedStream.collect(supplier, accumulator, combiner);
  }

  public  R collect(Collector collector) {
    return wrappedStream.collect(collector);
  }

  public Optional min(Comparator comparator) {
    return wrappedStream.min(comparator);
  }

  public Optional max(Comparator comparator) {
    return wrappedStream.max(comparator);
  }

  public long count() {
    return wrappedStream.count();
  }

  public boolean anyMatch(Predicate predicate) {
    return wrappedStream.anyMatch(predicate);
  }

  public boolean allMatch(Predicate predicate) {
    return wrappedStream.allMatch(predicate);
  }

  public boolean noneMatch(Predicate predicate) {
    return wrappedStream.noneMatch(predicate);
  }

  public Optional findFirst() {
    return wrappedStream.findFirst();
  }

  public Optional findAny() {
    return wrappedStream.findAny();
  }
}