Class MCRStreamUtils

java.lang.Object
org.mycore.common.MCRStreamUtils

public class MCRStreamUtils extends Object
Helper methods to handle common Stream use cases.
Author:
Thomas Scheffler (yagee)
  • Constructor Details

    • MCRStreamUtils

      public MCRStreamUtils()
  • Method Details

    • flatten

      public static <T> Stream<T> flatten(T node, Function<T,Collection<T>> subNodeSupplier, Function<Collection<T>,Stream<T>> streamProvider)
      Short circuit for calling flatten(node, subNodeSupplier, subNodeSupplier, t -> true)
      Parameters:
      node - node that holds kind-of subtree.
      subNodeSupplier - a function that delivers subtree items of next level
      streamProvider - a function that makes a Stream of a Collection<T>, usually Collection::stream or Collection::parallelStream
      Since:
      2016.04
      See Also:
    • flatten

      public static <T> Stream<T> flatten(T node, Function<T,Collection<T>> subNodeSupplier, Function<Collection<T>,Stream<T>> streamProvider, Predicate<T> filter)
      Example:
         MCRCategory foo = MCRCategoryDAOFactory.getInstance().getCategory(MCRCategoryID.rootID("foo"), -1);
         Stream<MCRCategory> parentCategories = flatten(foo, MCRCategory::getChildren, true, MCRCategory::hasChildren);
       
      Parameters:
      node - first node the stream is made of
      subNodeSupplier - a function that delivers subtree items of next level
      streamProvider - a function that makes a Stream of a Collection<T>, usually Collection::stream or Collection::parallelStream
      filter - a predicate that filters the element of the next level
      Since:
      2016.04
    • asStream

      public static <T> Stream<T> asStream(Enumeration<T> e)
      Transforms an Enumeration in a Stream.
      Parameters:
      e - the enumeration to transform
      Returns:
      a sequential, ordered Stream of unknown size
    • concat

      @SafeVarargs public static <T> Stream<T> concat(Stream<T>... streams)
      Concats any number of Streams not just 2 as in Stream.concat(Stream, Stream).
      Since:
      2016.04
    • distinctByKey

      public static <T> Predicate<T> distinctByKey(Function<? super T,?> keyExtractor)
      Stream distinct by filter function.

      persons.stream().filter(MCRStreamUtils.distinctByKey(p -> p.getName());

      It should be noted that for ordered parallel stream this solution does not guarantee which object will be extracted (unlike normal distinct()).
      Parameters:
      keyExtractor - a compare function
      Returns:
      a predicate
      See Also:
    • not

      public static <T> Predicate<T> not(Predicate<T> predicate)
      Negates a predicate.
      Parameters:
      predicate - the predicate to negate
      Returns:
      the negated predicate
      See Also: