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

LambdaUtils: Simplified filterByType method

parent b09ab4f9
No related branches found
No related tags found
1 merge request!183203 extend the lambda utils
LambdaUtils.java 7c3e3be7d04d62a8829b67bba78c10503bb4d2be GREEN
LambdaUtils.java 2b2767757ebc00e13bf73c55cbaeecc067bc3440 YELLOW
......@@ -129,8 +129,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());
}
......@@ -144,8 +144,8 @@ public class LambdaUtils {
* @return Sub-{@link Collection} of {@code types} that is derived from all {@code classifiers}.
*/
@SafeVarargs
public static <T, C> Collection<Class<? extends T>>
filterTypes(Collection<Class<? extends T>> types, Class<? extends C>... classifiers) {
public static <T, C> Collection<Class<? extends T>> filterTypes(
Collection<Class<? extends T>> types, Class<? extends C>... classifiers) {
// Checks if a single type is of the given single classifier type and returns the
// result as a Boolean. Cannot be implemented as a BiPredicate since "currying" / partial
......@@ -155,8 +155,9 @@ public class LambdaUtils {
// Applies isOfType for all provided "classifiers" to a single type. The "orElse"
// handles the case that no "classifiers" have been provided.
Predicate<Class<? extends T>> typeClassifierFilter = t -> Stream.of(classifiers)
.map(c -> isOfClassifier.apply(t, c)).reduce((b1, b2) -> b1 && b2).orElse(true);
Predicate<Class<? extends T>> typeClassifierFilter =
t -> Stream.of(classifiers).map(c -> isOfClassifier.apply(t, c))
.reduce((b1, b2) -> b1 && b2).orElse(true);
return types.stream().filter(typeClassifierFilter).collect(toList());
}
......@@ -178,6 +179,24 @@ public class LambdaUtils {
return elements.stream().filter(e -> isInstanceOfAll(e, classifiers)).collect(toList());
}
/**
* Filters the elements of the given {@code inCol} collection based on their type by the given
* {@code typeFilter}.
*
* @param inCol
* Collection to be filtered.
* @param typeFilter
* Type for which the elements shall be filtered.
* @return Collection of elements of the given {@code filterType} (same as {@code outCol}).
*/
public static <S, T, U extends T> Collection<T> filterByType(Collection<S> inCol,
Class<T> typeFilter) {
Collection<T> outCol = new ArrayList<>();
filterStream(inCol, e -> typeFilter.isAssignableFrom(e.getClass())).map(typeFilter::cast)
.forEach(e -> outCol.add(e));
return outCol;
}
/**
* Filters the elements of the given {@code inCol} collection based on their type by the given
* {@code typeFilter}. The matching elements are added to the given {@code outCol} collection
......@@ -192,7 +211,7 @@ public class LambdaUtils {
* @return Collection of elements of the given {@code filterType} (same as {@code outCol}).
*/
public static <S, T extends S, U extends T> Collection<T> filterByType(Collection<S> inCol,
Collection<T> outCol, Class<U> typeFilter) {
Collection<T> outCol, Class<T> typeFilter) {
filterStream(inCol, e -> typeFilter.isAssignableFrom(e.getClass())).map(typeFilter::cast)
.forEach(e -> outCol.add(e));
return outCol;
......@@ -369,8 +388,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