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 a9ecd7cf80660c0e3626b85c16e82e2c504884ac..8fb5e7d08aa8a3c59421554a32729bb777462385 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 @@ -19,6 +19,7 @@ import static java.util.stream.Collectors.toList; import static java.util.stream.StreamSupport.stream; import static org.conqat.lib.commons.reflect.ReflectionUtils.isInstanceOfAll; +import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; @@ -128,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()); } @@ -143,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 @@ -154,9 +155,8 @@ 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()); } @@ -198,6 +198,24 @@ public class LambdaUtils { return outCol; } + /** + * Filters the elements of the given {@code inCol} collection based on their type by the given + * {@code typeFilter}. The filtered elements are returned in an ArrayList. + * + * @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 extends S, U extends T> Collection<T> filterByType(Collection<S> inCol, + Class<U> typeFilter) { + List<T> outCol = new ArrayList<>(); + filterStream(inCol, e -> typeFilter.isAssignableFrom(e.getClass())).map(typeFilter::cast) + .forEach(e -> outCol.add(e)); + return outCol; + } + /** * Applies the given {@code action} to each element of the given {@code inputCollection}. * @@ -300,8 +318,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)); }