Class Recursions

java.lang.Object
ru.progrm_jarvis.javacommons.recursion.Recursions

public final class Recursions extends Object
Utilities for performing common recursive operations.
Author:
xdark, progrm_jarvis
  • Method Details

    • recurse

      public static <S> Stream<S> recurse(@NonNull @NonNull Stream<? extends S> sources, @NonNull @NonNull Function<? super S,@NotNull Stream<? extends S>> digger)
      Recursively traverses the sources evaluating to the recursive hierarchy. The provided Stream will attempt to be as lazy as possible.
      Type Parameters:
      S - type of source elements
      Parameters:
      sources - stream of sources which should be traversed recursively
      digger - function used to generate the stream of child elements from the base one
      Returns:
      stream of recursive hierarchy
      Throws:
      NullPointerException - if sources is null
      NullPointerException - if digger is null
      See Also:
    • recurse

      public static <S> Stream<S> recurse(S source, @NonNull @NonNull Function<? super S,@NotNull Stream<? extends S>> digger)

      Recursively traverses the source evaluating to the recursive hierarchy. The provided Stream will attempt to be as lazy as possible.

      An example providing the stream of class hierarchy of String class:

      
       Recursions.<Class<?>, Method>recurse(
               String.class,
               clazz -> {
                   final Class<?> superClass;
                   return Stream.concat(
                           (superClass = clazz.getSuperclass()) == null
                                   ? Stream.empty() : Stream.of(superClass),
                           Arrays.stream(clazz.getInterfaces())
                   );
               }
       )
       
      Type Parameters:
      S - type of source elements
      Parameters:
      source - source element used
      digger - function used to generate the stream of child elements from the base one
      Returns:
      stream of recursive hierarchy
      Throws:
      NullPointerException - if digger is null
      See Also:
    • recurseFully

      public static <S, E> Stream<E> recurseFully(@NonNull @NonNull Stream<? extends S> sources, @NonNull @NonNull Function<? super S,@NotNull Stream<? extends S>> digger, @NonNull @NonNull Function<? super S,@NotNull Stream<? extends E>> elementGetter)
      Recursively traverses the sources evaluating to the stream of hierarchy members' components.
      Type Parameters:
      S - type of source elements
      E - type of resulting elements
      Parameters:
      sources - stream of sources which should be traversed recursively
      digger - function used to generate the stream of child elements from the base one
      elementGetter - function used to get the elements from a source
      Returns:
      stream of elements got from recursive hierarchy
      Throws:
      NullPointerException - if sources is null
      NullPointerException - if digger is null
      NullPointerException - if elementGetter is null
      See Also:
    • recurseFully

      public static <S, E> Stream<E> recurseFully(S source, @NonNull @NonNull Function<? super S,@NotNull Stream<? extends S>> digger, @NonNull @NonNull Function<? super S,@NotNull Stream<? extends E>> elementGetter)

      Recursively traverses the source evaluating to the stream hierarchy members' components.

      An example providing the stream of all declared method in String class hierarchy:

      
       Recursions.<Class<?>, Method>recurseFully(
               String.class,
               clazz -> {
                   final Class<?> superClass;
                   return Stream.concat(
                           (superClass = clazz.getSuperclass()) == null
                                   ? Stream.empty() : Stream.of(superClass),
                           Arrays.stream(clazz.getInterfaces())
                   );
               },
               clazz -> Arrays.stream(clazz.getDeclaredMethods())
       )
       
      Type Parameters:
      S - type of source elements
      E - type of resulting elements
      Parameters:
      source - source element used
      digger - function used to generate the stream of child elements from the base one
      elementGetter - function used to get the elements from a source
      Returns:
      stream of elements got from recursive hierarchy
      Throws:
      NullPointerException - if digger is null
      NullPointerException - if elementGetter is null
      See Also: