Skip to content
Snippets Groups Projects
Commit 0871526d authored by Daniel Ratiu's avatar Daniel Ratiu
Browse files

new utility method

refs 163
parent 8364d6bf
No related branches found
No related tags found
No related merge requests found
/*--------------------------------------------------------------------------+
$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);
}
}
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