Skip to content
Snippets Groups Projects
Commit 89d414ff authored by Alexander Diewald's avatar Alexander Diewald
Browse files

tooling.common: Extend LambdaUtils.

* Wrap the filterList method in filter for easier use.
* Add a forEach method.
refs 3203
parent a9188a3d
No related branches found
No related tags found
No related merge requests found
......@@ -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));
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment