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

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
parent 8f92ade8
No related branches found
No related tags found
No related merge requests found
LambdaUtils.java 4bd5016212e8d0b596a13ac7262cef1548e9b077 YELLOW
LambdaUtils.java 1f86b97c677b5870bd02d3237f7af85f438dcf84 YELLOW
......@@ -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)
......
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