Class CheckedStream

Object
CheckedStream

public class CheckedStream extends Object
Map operations for streams that can throw checked-exceptions.
  • Method Details

    • forEach

      public static <T, E extends Exception> void forEach(Stream<T> stream, Class<? extends Exception> throwableClass, CheckedConsumer<T,E> consumer) throws E
      Performs a Stream.forEach(java.util.function.Consumer<? super T>) but accepts a consumer that can throw a checked-exception.

      This uses some internal reflection trickery to suppress the checked exception, and then rethrow it.

      As a side-effect, any runtime exceptions that are thrown during the function, will be rethrown wrapped inside a CheckedStream.ConvertedToRuntimeException.

      Type Parameters:
      T - type to consume.
      E - exception that can be thrown by mapFunction.
      Parameters:
      stream - the stream to apply the map on.
      throwableClass - the class of E.
      consumer - the function to call for each object in the stream.
      Throws:
      E - if the exception is thrown during mapping.
    • filter

      public static <T, E extends Exception> Stream<T> filter(Stream<T> stream, Class<? extends Exception> throwableClass, CheckedPredicate<T,E> predicate) throws E
      Performs a Stream.filter(java.util.function.Predicate<? super T>) but accepts a predicate that can throw a checked-exception.

      This uses some internal reflection trickery to suppress the checked exception, and then rethrow it.

      As a side-effect, any runtime exceptions that are thrown during the function, will be rethrown wrapped inside a CheckedStream.ConvertedToRuntimeException.

      Type Parameters:
      T - type to consume
      E - exception that can be thrown by mapFunction
      Parameters:
      stream - the stream to apply the map on.
      throwableClass - the class of E.
      predicate - the predicate to call for each object in the stream.
      Returns:
      elements from stream that match the predicate.
      Throws:
      E - if the exception is thrown during filtering.
    • map

      public static <S, T, E extends Exception> Stream<T> map(Stream<S> stream, Class<? extends Exception> throwableClass, CheckedFunction<S,T,E> mapFunction) throws E
      Performs a Stream.map(java.util.function.Function<? super T, ? extends R>) but accepts a function that can throw a checked-exception.

      This uses some internal reflection trickery to suppress the checked exception, and then rethrow it.

      As a side-effect, any runtime exceptions that are thrown during the function, will be rethrown wrapped inside a CheckedStream.ConvertedToRuntimeException.

      Type Parameters:
      S - input-type to map
      T - output-type of map
      E - exception that can be thrown by mapFunction
      Parameters:
      stream - the stream to apply the map on.
      throwableClass - the class of E.
      mapFunction - the function to use for mapping.
      Returns:
      the output of the flatMap.
      Throws:
      E - if the exception is thrown during mapping.
    • mapToInt

      public static <S, E extends Exception> IntStream mapToInt(Stream<S> stream, Class<? extends Exception> throwableClass, CheckedToIntFunction<S,E> mapFunction) throws E
      Performs a Stream.mapToInt(java.util.function.ToIntFunction<? super T>) but accepts a function that can throw a checked-exception.

      This uses some internal reflection trickery to suppress the checked exception, and then rethrow it.

      As a side-effect, any runtime exceptions that are thrown during the function, will be rethrown wrapped inside a CheckedStream.ConvertedToRuntimeException.

      Type Parameters:
      S - input-type to map
      E - exception that can be thrown by mapFunction
      Parameters:
      stream - the stream to apply the map on.
      throwableClass - the class of E.
      mapFunction - the function to use for mapping.
      Returns:
      the output of the flatMap.
      Throws:
      E - if the exception is thrown during mapping.
    • mapIntStream

      public static <T, E extends Exception> Stream<T> mapIntStream(IntStream stream, Class<? extends Exception> throwableClass, CheckedIntFunction<T,E> mapFunc) throws E
      Creates a new feature-list by mapping integers (from a range) each to an optional feature accepting a checked-exception

      This uses some internal reflection trickery to suppress the checked exception, and then rethrow it.

      Type Parameters:
      T - end-type for mapping
      E - an exception that be thrown during mapping
      Parameters:
      stream - stream of ints.
      throwableClass - the class of E.
      mapFunc - function for mapping.
      Returns:
      the stream after the mapping.
      Throws:
      E - if mapFunc throws it.
    • mapToObj

      public static <T, E extends Exception> Stream<T> mapToObj(IntStream stream, Class<? extends Exception> throwableClass, CheckedIntFunction<T,E> mapFunction) throws E
      Performs a IntStream.mapToObj(java.util.function.IntFunction<? extends U>) but accepts a function that can throw a checked-exception.

      This uses some internal reflection trickery to suppress the checked exception, and then rethrow it.

      As a side-effect, any runtime exceptions that are thrown during the function, will be rethrown wrapped inside a CheckedStream.ConvertedToRuntimeException.

      Type Parameters:
      T - object-type to map-to.
      E - exception that can be thrown by mapFunction.
      Parameters:
      stream - the stream to apply the map on.
      throwableClass - the class of E.
      mapFunction - the function to use for mapping.
      Returns:
      the output of the flatMap.
      Throws:
      E - if the exception is thrown during mapping.
    • flatMap

      public static <S, T, E extends Exception> Stream<T> flatMap(Stream<S> stream, Class<? extends Exception> throwableClass, CheckedFunction<S,Stream<? extends T>,E> flatMapFunction) throws E
      Performs a Stream.flatMap(java.util.function.Function<? super T, ? extends java.util.stream.Stream<? extends R>>) but accepts a function that can throw a checked-exception

      This uses some internal reflection trickery to suppress the checked exception, and then rethrow it.

      As a side-effect, any runtime exceptions that are thrown during the function, will be rethrown wrapped inside a CheckedStream.ConvertedToRuntimeException.

      Type Parameters:
      S - input-type to flatMap
      T - output-type of flatMap
      E - exception that can be thrown by flatMapFunction.
      Parameters:
      stream - the stream to apply the flatMap on.
      throwableClass - the class of E.
      flatMapFunction - the function to use for flatMapping.
      Returns:
      the output of the flatMap.
      Throws:
      E - if the exception.