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

io.github.oliviercailloux.jaris.exceptions.CheckedStreamImpl Maven / Gradle / Ivy

package io.github.oliviercailloux.jaris.exceptions;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.collect.ImmutableList;
import io.github.oliviercailloux.jaris.throwing.TBiConsumer;
import io.github.oliviercailloux.jaris.throwing.TBiFunction;
import io.github.oliviercailloux.jaris.throwing.TBinaryOperator;
import io.github.oliviercailloux.jaris.throwing.TComparator;
import io.github.oliviercailloux.jaris.throwing.TConsumer;
import io.github.oliviercailloux.jaris.throwing.TFunction;
import io.github.oliviercailloux.jaris.throwing.TPredicate;
import io.github.oliviercailloux.jaris.throwing.TSupplier;
import java.util.Comparator;
import java.util.Optional;
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.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Stream;

class CheckedStreamImpl implements CheckedStream {
  /**
   * This can’t be a generic class as it extends
   * Throwable.
   */
  @SuppressWarnings("serial")
  private static class InternalException extends RuntimeException {
    public InternalException(Exception e) {
      super(e);
    }

    /**
     * Guaranteed to be an X, if only X’s are given to the constructor.
     */
    @Override
    public synchronized Exception getCause() {
      return (Exception) super.getCause();
    }
  }

  /**
   * Wraps any checked exceptions into an InternalException with the checked exception as its cause.
   */
  private static final Unchecker UNCHECKER =
      Unchecker.wrappingWith(InternalException::new);

  public static  CheckedStreamImpl wrapping(Stream delegate) {
    return new CheckedStreamImpl<>(delegate);
  }

  public static  CheckedStreamImpl
      generate(TSupplier generator) {
    final Supplier wrapped = UNCHECKER.wrapSupplier(generator);
    return new CheckedStreamImpl<>(Stream.generate(wrapped));
  }

  private final Stream delegate;

  private CheckedStreamImpl(Stream delegate) {
    this.delegate = checkNotNull(delegate);
  }

  @Override
  public CheckedStreamImpl distinct() {
    return new CheckedStreamImpl<>(delegate.distinct());
  }

  @Override
  public CheckedStreamImpl dropWhile(TPredicate predicate) {
    final Predicate wrapped = UNCHECKER.wrapPredicate(predicate);
    return new CheckedStreamImpl<>(delegate.dropWhile(wrapped));
  }

  @Override
  public CheckedStreamImpl takeWhile(TPredicate predicate) {
    final Predicate wrapped = UNCHECKER.wrapPredicate(predicate);
    return new CheckedStreamImpl<>(delegate.takeWhile(wrapped));
  }

  @Override
  public CheckedStreamImpl filter(TPredicate predicate) {
    final Predicate wrapped = UNCHECKER.wrapPredicate(predicate);
    return new CheckedStreamImpl<>(delegate.filter(wrapped));
  }

  @Override
  public  CheckedStreamImpl
      flatMap(TFunction, ? extends X> mapper) {
    final Function> wrapped =
        UNCHECKER.wrapFunction(mapper);
    return new CheckedStreamImpl<>(delegate.flatMap(wrapped));
  }

  @Override
  public CheckedStreamImpl limit(long maxSize) {
    return new CheckedStreamImpl<>(delegate.limit(maxSize));
  }

  @Override
  public  CheckedStreamImpl map(TFunction mapper) {
    final Function wrapped = UNCHECKER.wrapFunction(mapper);
    return new CheckedStreamImpl<>(delegate.map(wrapped));
  }

  @Override
  public CheckedStreamImpl skip(long n) {
    return new CheckedStreamImpl<>(delegate.skip(n));
  }

  @Override
  public CheckedStreamImpl sorted() {
    return new CheckedStreamImpl<>(delegate.sorted());
  }

  @Override
  public CheckedStreamImpl sorted(TComparator comparator) {
    final Comparator wrapped = UNCHECKER.wrapComparator(comparator);
    return new CheckedStreamImpl<>(delegate.sorted(wrapped));
  }

  @Override
  public T reduce(T identity, TBinaryOperator accumulator) throws X {
    final BinaryOperator wrapped = UNCHECKER.wrapBinaryOperator(accumulator);
    try {
      return delegate.reduce(identity, wrapped);
    } catch (InternalException e) {
      final Exception cause = e.getCause();
      @SuppressWarnings("unchecked")
      final X castedCause = (X) cause;
      throw castedCause;
    }
  }

  @Override
  public Optional reduce(TBinaryOperator accumulator) throws X {
    final BinaryOperator wrapped = UNCHECKER.wrapBinaryOperator(accumulator);
    try {
      return delegate.reduce(wrapped);
    } catch (InternalException e) {
      final Exception cause = e.getCause();
      @SuppressWarnings("unchecked")
      final X castedCause = (X) cause;
      throw castedCause;
    }
  }

  @Override
  public  U reduce(U identity, TBiFunction accumulator,
      TBinaryOperator combiner) throws X {
    final BiFunction wrappedAccumulator = UNCHECKER.wrapBiFunction(accumulator);
    final BinaryOperator wrappedCombiner = UNCHECKER.wrapBinaryOperator(combiner);
    try {
      return delegate.reduce(identity, wrappedAccumulator, wrappedCombiner);
    } catch (InternalException e) {
      final Exception cause = e.getCause();
      @SuppressWarnings("unchecked")
      final X castedCause = (X) cause;
      throw castedCause;
    }
  }

  @Override
  public  R collect(TSupplier supplier,
      TBiConsumer accumulator, TBiConsumer combiner)
      throws X {
    final Supplier wrappedSupplier = UNCHECKER.wrapSupplier(supplier);
    final BiConsumer wrappedAccumulator = UNCHECKER.wrapBiConsumer(accumulator);
    final BiConsumer wrappedCombiner = UNCHECKER.wrapBiConsumer(combiner);
    try {
      return delegate.collect(wrappedSupplier, wrappedAccumulator, wrappedCombiner);
    } catch (InternalException e) {
      final Exception cause = e.getCause();
      @SuppressWarnings("unchecked")
      final X castedCause = (X) cause;
      throw castedCause;
    }
  }

  @Override
  public  R collect(Collector collector) throws X {
    try {
      return delegate.collect(collector);
    } catch (InternalException e) {
      final Exception cause = e.getCause();
      @SuppressWarnings("unchecked")
      final X castedCause = (X) cause;
      throw castedCause;
    }
  }

  /**
   * Returns whether all elements of this stream match the provided predicate. May not evaluate the
   * predicate on all elements if not necessary for determining the result. If the stream is empty
   * then {@code true} is returned and the predicate is not evaluated.
   *
   * 

* This is a short-circuiting terminal operation. * * @apiNote This method evaluates the universal quantification of the predicate over the * elements of the stream (for all x P(x)). If the stream is empty, the quantification is * said to be vacuously satisfied and is always {@code true} (regardless of * P(x)). * * @param predicate a non-interfering, * stateless predicate to apply to * elements of this stream * @return {@code true} if either all elements of the stream match the provided predicate or the * stream is empty, otherwise {@code false} * @throws X if any functional interface operating on this stream throws a checked exception * @see Stream#allMatch(Predicate) */ @Override public boolean allMatch(TPredicate predicate) throws X { /* * Any checked exception thrown by predicate is supposed to extend X, by its header. Only such * exceptions are wrapped into an InternalException instance by the UNCHECKER. Thus, any * InternalException thrown by the wrapped predicate has a Y as its cause. */ final Predicate wrapped = UNCHECKER.wrapPredicate(predicate); try { return delegate.allMatch(wrapped); } catch (InternalException e) { final Exception cause = e.getCause(); @SuppressWarnings("unchecked") final X castedCause = (X) cause; throw castedCause; } } @Override public boolean anyMatch(TPredicate predicate) throws X { final Predicate wrapped = UNCHECKER.wrapPredicate(predicate); try { return delegate.anyMatch(wrapped); } catch (InternalException e) { final Exception cause = e.getCause(); @SuppressWarnings("unchecked") final X castedCause = (X) cause; throw castedCause; } } @Override public boolean noneMatch(TPredicate predicate) throws X { final Predicate wrapped = UNCHECKER.wrapPredicate(predicate); try { return delegate.noneMatch(wrapped); } catch (InternalException e) { final Exception cause = e.getCause(); @SuppressWarnings("unchecked") final X castedCause = (X) cause; throw castedCause; } } @Override public CheckedStreamImpl peek(TConsumer action) throws X { final Consumer wrapped = UNCHECKER.wrapConsumer(action); try { return new CheckedStreamImpl<>(delegate.peek(wrapped)); } catch (InternalException e) { final Exception cause = e.getCause(); @SuppressWarnings("unchecked") final X castedCause = (X) cause; throw castedCause; } } @Override public long count() throws X { try { return delegate.count(); } catch (InternalException e) { final Exception cause = e.getCause(); @SuppressWarnings("unchecked") final X castedCause = (X) cause; throw castedCause; } } @Override public Optional findAny() throws X { try { return delegate.findAny(); } catch (InternalException e) { final Exception cause = e.getCause(); @SuppressWarnings("unchecked") final X castedCause = (X) cause; throw castedCause; } } @Override public Optional findFirst() throws X { try { return delegate.findFirst(); } catch (InternalException e) { final Exception cause = e.getCause(); @SuppressWarnings("unchecked") final X castedCause = (X) cause; throw castedCause; } } @Override public void forEach(TConsumer action) throws X { final Consumer wrapped = UNCHECKER.wrapConsumer(action); try { delegate.forEach(wrapped); } catch (InternalException e) { final Exception cause = e.getCause(); @SuppressWarnings("unchecked") final X castedCause = (X) cause; throw castedCause; } } @Override public void forEachOrdered(TConsumer action) throws X { final Consumer wrapped = UNCHECKER.wrapConsumer(action); try { delegate.forEachOrdered(wrapped); } catch (InternalException e) { final Exception cause = e.getCause(); @SuppressWarnings("unchecked") final X castedCause = (X) cause; throw castedCause; } } @Override public Optional max(TComparator comparator) throws X { final Comparator wrapped = UNCHECKER.wrapComparator(comparator); try { return delegate.max(wrapped); } catch (InternalException e) { final Exception cause = e.getCause(); @SuppressWarnings("unchecked") final X castedCause = (X) cause; throw castedCause; } } @Override public Optional min(TComparator comparator) throws X { final Comparator wrapped = UNCHECKER.wrapComparator(comparator); try { return delegate.min(wrapped); } catch (InternalException e) { final Exception cause = e.getCause(); @SuppressWarnings("unchecked") final X castedCause = (X) cause; throw castedCause; } } @Override public ImmutableList toList() throws X { try { return delegate.collect(ImmutableList.toImmutableList()); } catch (InternalException e) { final Exception cause = e.getCause(); @SuppressWarnings("unchecked") final X castedCause = (X) cause; throw castedCause; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy