From 0871526d83896d74260d5cc63e7c5b4770c274c2 Mon Sep 17 00:00:00 2001 From: Daniel Ratiu <ratiu@fortiss.org> Date: Tue, 13 Sep 2011 09:24:49 +0000 Subject: [PATCH] new utility method refs 163 --- .../tooling/kernel/utils/EcoreUtils.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/utils/EcoreUtils.java 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 new file mode 100644 index 000000000..e949cb199 --- /dev/null +++ b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/utils/EcoreUtils.java @@ -0,0 +1,75 @@ +/*--------------------------------------------------------------------------+ +$Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $ +| | +| Copyright 2011 ForTISS GmbH | +| | +| Licensed under the Apache License, Version 2.0 (the "License"); | +| you may not use this file except in compliance with the License. | +| You may obtain a copy of the License at | +| | +| http://www.apache.org/licenses/LICENSE-2.0 | +| | +| Unless required by applicable law or agreed to in writing, software | +| distributed under the License is distributed on an "AS IS" BASIS, | +| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | +| See the License for the specific language governing permissions and | +| limitations under the License. | ++--------------------------------------------------------------------------*/ +package org.fortiss.tooling.kernel.utils; + +import org.eclipse.emf.common.util.BasicEList; +import org.eclipse.emf.common.util.ECollections; +import org.eclipse.emf.common.util.EList; + +/** + * Utility methods for dealing with .ecore models. These methods should be used + * to define EMF operations that make the models easier to use. + * + * @author ratiu + * @author $Author: hoelzl $ + * @version $Rev: 18709 $ + * @ConQAT.Rating RED Hash: + */ +public class EcoreUtils { + + /** + * Converts an EList of a given type into an EList of one of its subtypes. + * Utility method to avoid unnecessary casts. + * + * @param targetClass + * - the class representing the subtype + * @param sourceList + * - the source list containing objects of type S, a supertype of + * T + * @return a target EList containing elements of type targetClazz + */ + @SuppressWarnings("unchecked") + public static <S, T extends S> EList<T> convertList(Class<T> targetClass, + EList<S> sourceList) { + return (EList<T>) (EList<?>) sourceList; + } + + /** + * From a given EList with source objects of type S create another + * unmodifiable EList with objects of type T, whereby T is a sub-type of S. + * The resulting EList is unmodifiable thereby it represents only a view + * over the source list. + * + * @param targetClass + * - a class representing type T + * @param sourceList + * - an EList with objects of type S + * @return an unmodifiable EList of objects of type T + */ + @SuppressWarnings("unchecked") + public static <S, T extends S> EList<T> pickInstanceOf( + Class<T> targetClass, EList<S> sourceList) { + if (sourceList == null) + return null; + EList<T> result = new BasicEList<T>(); + for (S sourceElement : sourceList) + if (targetClass.isAssignableFrom(sourceElement.getClass())) + result.add((T) sourceElement); + return ECollections.unmodifiableEList(result); + } +} -- GitLab