From 4e0e9ebdd1669eb8df8bfaf46f0d11874a24a5e2 Mon Sep 17 00:00:00 2001 From: Alexander Diewald <diewald@fortiss.org> Date: Fri, 26 Jan 2018 08:28:33 +0000 Subject: [PATCH] Common: LambdaUtils: Add a more general filter by type method. * The current filterByType methods has fairly strict expectations towards inheritance which restricts the use of the method. * Add a more generic filterType method that has less restrictions w.r.t. the generics. * Rename the existing method to filterTypeSafe. refs 3203 --- .../org/fortiss/tooling/common/util/.ratings | 2 +- .../tooling/common/util/LambdaUtils.java | 23 ++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/org.fortiss.tooling.common/trunk/src/org/fortiss/tooling/common/util/.ratings b/org.fortiss.tooling.common/trunk/src/org/fortiss/tooling/common/util/.ratings index f3011fbb8..25a0808bf 100644 --- a/org.fortiss.tooling.common/trunk/src/org/fortiss/tooling/common/util/.ratings +++ b/org.fortiss.tooling.common/trunk/src/org/fortiss/tooling/common/util/.ratings @@ -1 +1 @@ -LambdaUtils.java 4bd5016212e8d0b596a13ac7262cef1548e9b077 YELLOW +LambdaUtils.java 1f86b97c677b5870bd02d3237f7af85f438dcf84 YELLOW 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 00d10f1bb..8c8006e9a 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 @@ -208,7 +208,28 @@ public class LambdaUtils { * 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, + public static <S, T> Collection<T> filterType(Collection<S> inCol, Class<T> typeFilter) { + List<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 filtered elements are returned in an ArrayList. + * <p> + * NOTE: this version of the type filer expects the filtered typed to be directly assignable + * from the type of the collection. + * </p> + * + * @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> filterTypeSafe(Collection<S> inCol, Class<U> typeFilter) { List<T> outCol = new ArrayList<>(); filterStream(inCol, e -> typeFilter.isAssignableFrom(e.getClass())).map(typeFilter::cast) -- GitLab