Class Recursions
java.lang.Object
ru.progrm_jarvis.javacommons.recursion.Recursions
Utilities for performing common recursive operations.
- Author:
- xdark, progrm_jarvis
-
Method Summary
Modifier and TypeMethodDescriptionstatic <S> Stream<S>recurse(@NonNull Stream<? extends S> sources, @NonNull Function<? super S, @NotNull Stream<? extends S>> digger) Recursively traverses the sources evaluating to the recursive hierarchy.static <S> Stream<S>Recursively traverses the source evaluating to the recursive hierarchy.static <S,E> Stream<E> recurseFully(@NonNull Stream<? extends S> sources, @NonNull Function<? super S, @NotNull Stream<? extends S>> digger, @NonNull Function<? super S, @NotNull Stream<? extends E>> elementGetter) Recursively traverses the sources evaluating to thestreamof hierarchy members' components.static <S,E> Stream<E> recurseFully(S source, @NonNull Function<? super S, @NotNull Stream<? extends S>> digger, @NonNull Function<? super S, @NotNull Stream<? extends E>> elementGetter) Recursively traverses the source evaluating to thestreamhierarchy members' components.
-
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 providedStreamwill attempt to be as lazy as possible.- Type Parameters:
S- type of source elements- Parameters:
sources- stream of sources which should be traversed recursivelydigger- function used to generate the stream of child elements from the base one- Returns:
- stream of recursive hierarchy
- Throws:
NullPointerException- ifsourcesisnullNullPointerException- ifdiggerisnull- 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
Streamwill attempt to be as lazy as possible.An example providing the stream of class hierarchy of
Stringclass: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 useddigger- function used to generate the stream of child elements from the base one- Returns:
- stream of recursive hierarchy
- Throws:
NullPointerException- ifdiggerisnull- 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 thestreamof hierarchy members' components.- Type Parameters:
S- type of source elementsE- type of resulting elements- Parameters:
sources- stream of sources which should be traversed recursivelydigger- function used to generate the stream of child elements from the base oneelementGetter- function used to get the elements from a source- Returns:
- stream of elements got from recursive hierarchy
- Throws:
NullPointerException- ifsourcesisnullNullPointerException- ifdiggerisnullNullPointerException- ifelementGetterisnull- 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
streamhierarchy members' components.An example providing the stream of all declared method in
Stringclass 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 elementsE- type of resulting elements- Parameters:
source- source element useddigger- function used to generate the stream of child elements from the base oneelementGetter- function used to get the elements from a source- Returns:
- stream of elements got from recursive hierarchy
- Throws:
NullPointerException- ifdiggerisnullNullPointerException- ifelementGetterisnull- See Also:
-