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

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

There is a newer version: 0.0.34
Show newest version
package io.github.oliviercailloux.jaris.exceptions;

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

import com.google.common.collect.ImmutableList;
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 {
  @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(Throwing.Supplier 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(Throwing.Predicate predicate) {
    final Predicate wrapped = UNCHECKER.wrapPredicate(predicate);
    return new CheckedStreamImpl<>(delegate.dropWhile(wrapped));
  }

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

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

  @Override
  public  CheckedStreamImpl
      flatMap(Throwing.Function, ? 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(Throwing.Function 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(Throwing.Comparator comparator) {
    final Comparator wrapped = UNCHECKER.wrapComparator(comparator);
    return new CheckedStreamImpl<>(delegate.sorted(wrapped));
  }

  @Override
  public T reduce(T identity, Throwing.BinaryOperator 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(Throwing.BinaryOperator 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, Throwing.BiFunction accumulator,
      Throwing.BinaryOperator 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(Throwing.Supplier supplier,
      Throwing.BiConsumer accumulator,
      Throwing.BiConsumer 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(Throwing.Predicate 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(Throwing.Predicate 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(Throwing.Predicate 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(Throwing.Consumer 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(Throwing.Consumer 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(Throwing.Consumer 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(Throwing.Comparator 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(Throwing.Comparator 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 - 2024 Weber Informatics LLC | Privacy Policy