From faff24d3e890e2b02a19bf0ac6c9d151e36338aa Mon Sep 17 00:00:00 2001 From: Alexander Diewald <diewald@fortiss.org> Date: Fri, 26 Jan 2018 08:37:01 +0000 Subject: [PATCH] Common: LambdaUtils: Add firstOfType util method. The firstOfType method returns the first element from a given collection that is assignable from the given type. This method allows to reduce the LoCs since the described task is quite common. refs 3203 --- .../fortiss/tooling/common/util/LambdaUtils.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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 8c8006e9a..7c3e3be7d 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 @@ -237,6 +237,20 @@ public class LambdaUtils { return outCol; } + /** + * Returns the first element of the given {@code inCol} collection that is assignable from the + * given type. + * + * @param inCol + * Collection to be filtered. + * @param typeFilter + * Type for which the elements shall be filtered. + * @return Optional of the first found element of the given type. + */ + public static <S, T> Optional<T> firstOfType(Collection<S> inCol, Class<T> typeFilter) { + return getFirst(filterType(inCol, typeFilter)); + } + /** * Applies the given {@code action} to each element of the given {@code inputCollection}. * -- GitLab