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 076fdd34287ff748ba256de1d7a93d03e395293b..8e07c203c71b398c8a5e5f352f637d00f0d5b044 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)); }