Commit 82f524c0 authored by Alexander Diewald's avatar Alexander Diewald
Browse files

Add a utility method to get the directly connected elements of a given...

Add a utility method to get the directly connected elements of a given IHierarchicElement that have the given type.
parent c075ae6a
......@@ -17,6 +17,8 @@ $Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.base.utils;
import static org.fortiss.tooling.common.util.LambdaUtils.map;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
......@@ -39,7 +41,7 @@ import org.fortiss.tooling.base.model.element.IModelElementSpecification;
* @author hoelzl
* @author $Author: hoelzl $
* @version $Rev: 18709 $
* @ConQAT.Rating YELLOW Hash: 58521C469226B471D914409DD11925CD
* @ConQAT.Rating YELLOW Hash: E5C0A2F4390A13FD5ED05FDA44EB5944
*/
public class BaseModelElementUtils {
......@@ -150,4 +152,36 @@ public class BaseModelElementUtils {
}
return null;
}
/**
* Returns the <i>directly</i> connected {@link IHierarchicElement}s of the given
* {@code element} which have the given type.
*
* @param element
* {@link IHierarchicElement} for which to determine the connected elements.
* @param connectedElementType
* Type of the connected elements (filter).
* @return List of connected {@link IHierarchicElement}s of the given type.
*/
public static <T extends IHierarchicElement> List<T> getConnectedElements(
IHierarchicElement element, Class<T> connectedElementType) {
List<IConnector> elementConnectors = element.getConnectors();
List<IConnector> targetConnectors = new ArrayList<>();
for(IConnector elementConnector : elementConnectors) {
targetConnectors.addAll(map(elementConnector.getIncoming(), in -> in.getSource(),
ArrayList::new));
targetConnectors.addAll(map(elementConnector.getOutgoing(), out -> out.getTarget(),
ArrayList::new));
}
List<T> targetElements = new ArrayList<>();
for(IConnector targetConnector : targetConnectors) {
if(connectedElementType.isAssignableFrom(targetConnector.getOwner().getClass())) {
@SuppressWarnings("unchecked") T targetElement = (T)targetConnector.getOwner();
targetElements.add(targetElement);
}
}
return targetElements;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment