| 1 | /** |
| 2 | * |
| 3 | */ |
| 4 | package de.fzi.se.quality.util; |
| 5 | |
| 6 | import java.util.ArrayList; |
| 7 | import java.util.Collections; |
| 8 | import java.util.Iterator; |
| 9 | import java.util.List; |
| 10 | |
| 11 | /**Class with helper function for EMF processing. |
| 12 | * |
| 13 | * @author groenda |
| 14 | */ |
| 15 | public class EMFHelper { |
| 16 | |
| 17 | /**Gets the first object of a given type from a list. |
| 18 | * @param <T> Type of the Object. |
| 19 | * @param collection List of objects. |
| 20 | * @param c Class of the requested object. |
| 21 | * @return The first object in the list of the given type. |
| 22 | */ |
| 23 | @SuppressWarnings("unchecked") |
| 24 | public static <T> T getFirstObjectByType(List<?> collection, Class<T> c) { |
| 25 | Iterator<?> i = collection.iterator(); |
| 26 | Object o = null; |
| 27 | while ((o = i.next()) != null) { |
| 28 | if (c.isAssignableFrom(o.getClass())) |
| 29 | break; |
| 30 | } |
| 31 | return (T) o; |
| 32 | } |
| 33 | |
| 34 | /**Gets all objects of a given type from a list. |
| 35 | * @param <T> Type of the Object. |
| 36 | * @param collection List of objects. |
| 37 | * @param c Class of the requested object. |
| 38 | * @return The first object in the list of the given type. |
| 39 | */ |
| 40 | @SuppressWarnings("unchecked") |
| 41 | public static <T> List<T> getObjectsByType(List<?> collection, Class<T> c) { |
| 42 | List<T> list = new ArrayList<T>(); |
| 43 | Iterator<?> i = collection.iterator(); |
| 44 | Object o = null; |
| 45 | while (i.hasNext()) { |
| 46 | o = i.next(); |
| 47 | if (c.isAssignableFrom(o.getClass())) { |
| 48 | list.add((T) o); |
| 49 | } |
| 50 | } |
| 51 | return Collections.unmodifiableList(list); |
| 52 | } |
| 53 | |
| 54 | } |