diff --git a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/utils/EcoreUtils.java b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/utils/EcoreUtils.java index c67f1677de2750ce34658c51dbb070edcb12e4e2..5e75b9dba7c4083a47caa55355a7bebbefaeba7b 100644 --- a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/utils/EcoreUtils.java +++ b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/utils/EcoreUtils.java @@ -72,4 +72,25 @@ public class EcoreUtils { result.add((T) sourceElement); return ECollections.unmodifiableEList(result); } + + /** + * From a given EList with source objects of type S pick the first object + * with type T, whereby T is a sub-type of S. + * + * @param targetClass + * - a class representing type T + * @param sourceList + * - an EList with objects of type S + * @return an object of type T or null if none exists + */ + @SuppressWarnings("unchecked") + public static <S, T extends S> T pickFirstInstanceOf(Class<T> targetClass, + EList<S> sourceList) { + if (sourceList == null) + return null; + for (S sourceElement : sourceList) + if (targetClass.isAssignableFrom(sourceElement.getClass())) + return (T) sourceElement; + return null; + } }