| author | Knut Wannheden | 2012-11-26 17:46:55 (EST) |
|---|---|---|
| committer | Knut Wannheden | 2012-11-26 17:46:55 (EST) |
| commit | 5b5eeedcd55f9fdb78c9eef19f3e0c77a35af52e (patch) (side-by-side diff) | |
| tree | 4d3b693b53dc8ed4decf3d56785e16d727bee68e | |
| parent | 0419c82c27fe9a29946f4901ba90da624af26228 (diff) | |
| download | org.eclipse.xtext-5b5eeedcd55f9fdb78c9eef19f3e0c77a35af52e.zip org.eclipse.xtext-5b5eeedcd55f9fdb78c9eef19f3e0c77a35af52e.tar.gz org.eclipse.xtext-5b5eeedcd55f9fdb78c9eef19f3e0c77a35af52e.tar.bz2 | |
[util] simplify EcoreUtil2#getContainerOfType()
As noted in message
http://www.eclipse.org/forums/index.php/mv/msg/432854/983757/#msg_983757
the performance of EcoreUtil2#getContainerOfType() can be improved by
simplifying the implementation and removing a redundant eContainer()
call.
| -rw-r--r-- | plugins/org.eclipse.xtext/src/org/eclipse/xtext/EcoreUtil2.java | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/plugins/org.eclipse.xtext/src/org/eclipse/xtext/EcoreUtil2.java b/plugins/org.eclipse.xtext/src/org/eclipse/xtext/EcoreUtil2.java index 41fbf50..b674e77 100644 --- a/plugins/org.eclipse.xtext/src/org/eclipse/xtext/EcoreUtil2.java +++ b/plugins/org.eclipse.xtext/src/org/eclipse/xtext/EcoreUtil2.java @@ -94,13 +94,17 @@ public class EcoreUtil2 extends EcoreUtil { return previous; } - + /** + * Returns the closest {@link EObject#eContainer() container object} of the requested type. If the given object is + * an instance of the requested type, then the object itself will be returned. If no container object is of the + * requested type, then {@code null} will be returned. + */ @SuppressWarnings("unchecked") public static <T extends EObject> T getContainerOfType(EObject ele, Class<T> type) { - if (type.isAssignableFrom(ele.getClass())) - return (T) ele; - if (ele.eContainer() != null) - return getContainerOfType(ele.eContainer(), type); + for (EObject e = ele; e != null; e = e.eContainer()) + if (type.isInstance(e)) + return (T) e; + return null; } |

