From 89d414ff115c73106235cbcae3fcf96d2ae2cf9c Mon Sep 17 00:00:00 2001 From: Alexander Diewald <diewald@fortiss.org> Date: Wed, 29 Nov 2017 10:00:53 +0000 Subject: [PATCH] tooling.common: Extend LambdaUtils. * Wrap the filterList method in filter for easier use. * Add a forEach method. refs 3203 --- .../tooling/common/util/LambdaUtils.java | 53 +++++++++++++++++-- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/org.fortiss.tooling.common/trunk/src/org/fortiss/tooling/common/util/LambdaUtils.java b/org.fortiss.tooling.common/trunk/src/org/fortiss/tooling/common/util/LambdaUtils.java index 076fdd342..8e07c203c 100644 --- a/org.fortiss.tooling.common/trunk/src/org/fortiss/tooling/common/util/LambdaUtils.java +++ b/org.fortiss.tooling.common/trunk/src/org/fortiss/tooling/common/util/LambdaUtils.java @@ -22,6 +22,7 @@ import java.util.Iterator; import java.util.List; import java.util.Optional; import java.util.Set; +import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; import java.util.function.Supplier; @@ -53,10 +54,25 @@ public class LambdaUtils { * @return Filtered collection as a Set. */ public static <T, S extends T> Collection<T> filter(Collection<S> collection, - Predicate<S> filter, Supplier<? extends Collection<T>> sup) { + Predicate<S> filter, Supplier<Collection<T>> sup) { return filterStream(collection, filter).collect(Collectors.toCollection(sup)); } + /** + * Filters the given collection by the given filter and returns the remaining elements as a + * Collection of the type List. This method wraps {@link #filterList(Collection, Predicate)}. + * + * @param collection + * Collection to be filtered. + * @param filter + * Filter to apply. + * @return Filtered collection as a Set. + */ + public static <T, S extends T> Collection<T> filter(Collection<S> collection, + Predicate<S> filter) { + return filterList(collection, filter); + } + /** * Filters the given collection by the given filter and returns the remaining elements as a * Stream. @@ -68,6 +84,21 @@ public class LambdaUtils { * @return Filtered collection as a Stream. */ public static <S> Stream<S> filterStream(Collection<S> collection, Predicate<S> filter) { + return collection.stream().filter(filter); + } + + /** + * Filters the given collection by the given filter and returns the remaining elements as a + * Stream. + * <b>NOTE:</b> The filter operation is applied in parallel to each of the elements. + * + * @param collection + * Collection to be filtered. + * @param filter + * Filter to apply. + * @return Filtered collection as a Stream. + */ + public static <S> Stream<S> filterStreamPar(Collection<S> collection, Predicate<S> filter) { return collection.parallelStream().filter(filter); } @@ -94,8 +125,8 @@ public class LambdaUtils { * Filter to apply. * @return Filtered collection as a List. */ - public static <T, S extends T> List<T> - filterList(Collection<S> collection, Predicate<S> filter) { + public static <T, S extends T> List<T> filterList(Collection<S> collection, + Predicate<S> filter) { return filterStream(collection, filter).collect(Collectors.toList()); } @@ -119,6 +150,18 @@ public class LambdaUtils { return outCol; } + /** + * Applies the given {@code action} to each element of the given {@code inputCollection}. + * + * @param inputCollection + * Collection to traverse. + * @param action + * Action to be applied to each element. + */ + public static void forEach(Collection<?> inputCollection, Consumer<? super Object> action) { + inputCollection.stream().forEach(action); + } + /** * Filters the given collection by the given filter and returns the first of the remaining * elements . @@ -209,8 +252,8 @@ public class LambdaUtils { * Supplier for the output {@link Collection}, e.g. TreeSet::new. * @return Collection of mapping results. */ - public static <S, T, R extends Stream<S>, U extends Collection<S>> Collection<S> flatMapInOut( - Collection<T> inColl, Function<T, R> mapper, Supplier<U> sup) { + public static <S, T, R extends Stream<S>, U extends Collection<S>> Collection<S> + flatMapInOut(Collection<T> inColl, Function<T, R> mapper, Supplier<U> sup) { return inColl.parallelStream().flatMap(mapper).collect(Collectors.toCollection(sup)); } -- GitLab