Class IterableHelpers

    • Method Detail

      • flatMap

        public static <T,​R> Iterable<R> flatMap​(@Nonnull
                                                      Iterable<T> source,
                                                      @Nonnull
                                                      Function<? super T,​? extends Iterable<R>> mapper)
        Flat-maps given function to the given iterable and returns a new iterable. This is akin to compositions based on Stream, however, allows for restarting the iteration as per general contract in Iterable.
        Type Parameters:
        T - type of source iterable
        R - type the given mapper function returns iterables of
        Parameters:
        source - source iterable
        mapper - mapper function to map from T to Iterable of R
        Returns:
        an iterable of type R that is the conceptual flat map of source using mapper
      • map

        public static <T,​R> Iterable<R> map​(@Nonnull
                                                  Iterable<T> source,
                                                  @Nonnull
                                                  Function<? super T,​R> mapper)
        Maps given function to the given iterable and returns a new iterable. This is akin to compositions based on Stream, however, allows for restarting the iteration as per general contract in Iterable.
        Type Parameters:
        T - type of source iterable
        R - type the given mapper function returns
        Parameters:
        source - source iterable
        mapper - mapper function to map from T to Iterable of R
        Returns:
        an iterable of type R that is the conceptual map of source using mapper
      • orElseOf

        public static <T> Iterable<T> orElseOf​(@Nonnull
                                               Iterable<T> source,
                                               @Nonnull
                                               T value)
        Returns an alternative singleton iterable if the given source iterable is empty.
        Type Parameters:
        T - type parameter of the given iterable
        Parameters:
        source - source
        value - value to use if source is empty
        Returns:
        an iterable of type T that returns the original iterable if it is not empty, or a singleton iterable composed of just value.
      • orElse

        public static <T> Iterable<T> orElse​(@Nonnull
                                             Iterable<T> source,
                                             @Nonnull
                                             Iterable<T> otherIterable)
        Returns an alternative iterable if the given source iterable is empty.
        Type Parameters:
        T - type parameter of the given iterable
        Parameters:
        source - source
        otherIterable - iterable to return if source is empty
        Returns:
        an iterable of type T that returns the original iterable if it is not empty, or otherIterable