From fe574773ff28aec5b13e688fedc7ee20e26d766d Mon Sep 17 00:00:00 2001 From: Daniel Ratiu <ratiu@fortiss.org> Date: Sat, 22 Oct 2011 11:00:05 +0000 Subject: [PATCH] fixing the Simple Traffic Lights controller test + beautifying the code refs 222 --- .../tooling/kernel/utils/EcoreUtils.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) 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 c67f1677d..5e75b9dba 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; + } } -- GitLab