Refactored new views to use methods from core packages

This commit introduces refactorings of the sunburst and the matrix view
that make use of the new methods introduced in TraceHelper. It also adds
shared functionality from those views to EMFHelper and TraceHelper to
make it available to other bundles. In particular, a simple comparison
of EObjects based on their identifier was added to EMFHelper and a
method to get a list of the elements that are traced by a list of
connections.

Change-Id: I948d7a1cd29ec3379bd00ea65684c09723c42ac5
diff --git a/bundles/org.eclipse.capra.core/src/org/eclipse/capra/core/helpers/EMFHelper.java b/bundles/org.eclipse.capra.core/src/org/eclipse/capra/core/helpers/EMFHelper.java
index 858c083..78c70d9 100644
--- a/bundles/org.eclipse.capra.core/src/org/eclipse/capra/core/helpers/EMFHelper.java
+++ b/bundles/org.eclipse.capra.core/src/org/eclipse/capra/core/helpers/EMFHelper.java
@@ -24,8 +24,8 @@
  * handling EMF models.
  */
 public class EMFHelper {
-	
-	private EMFHelper () {
+
+	private EMFHelper() {
 		super();
 	}
 
@@ -33,17 +33,16 @@
 	 * Builds an identifier String for the given EObject. This identifier starts
 	 * with
 	 * <ul>
-	 * <li>the attribute of the EObject as a String, if the EObject does only
-	 * have one attribute.</li>
+	 * <li>the attribute of the EObject as a String, if the EObject does only have
+	 * one attribute.</li>
 	 * <li>the attribute called 'name' of the EObject, if it has such an
 	 * attribute</li>
-	 * <li>any attribute of the EObject, but String attributes are preferred
-	 * </li>
+	 * <li>any attribute of the EObject, but String attributes are preferred</li>
 	 * </ul>
 	 * The identifier ends with " : " followed by the type of the EObject. <br>
 	 * Example: A Node with the name "foo" will result in "foo : Node" <br>
-	 * If the EObject does not have any attributes or all attributes have the
-	 * value null, this function will only return the type of the EObject.
+	 * If the EObject does not have any attributes or all attributes have the value
+	 * null, this function will only return the type of the EObject.
 	 */
 	public static String getIdentifier(final EObject eObject) {
 		if (eObject == null) {
@@ -78,10 +77,9 @@
 	}
 
 	/**
-	 * @param name
-	 *            Use an empty StringBuilder as input. If this function returns
-	 *            true, this parameter has been filled, if it returns false,
-	 *            nothing happened.
+	 * @param name Use an empty StringBuilder as input. If this function returns
+	 *             true, this parameter has been filled, if it returns false,
+	 *             nothing happened.
 	 * @return Indicates the success of this function and if the last parameter
 	 *         contains output.
 	 */
@@ -99,10 +97,9 @@
 	}
 
 	/**
-	 * @param name
-	 *            Use an empty StringBuilder as input. If this function returns
-	 *            true, this parameter has been filled, if it returns false,
-	 *            nothing happened.
+	 * @param name Use an empty StringBuilder as input. If this function returns
+	 *             true, this parameter has been filled, if it returns false,
+	 *             nothing happened.
 	 * @return Indicates the success of this function and if the last parameter
 	 *         contains output.
 	 */
@@ -123,10 +120,9 @@
 	}
 
 	/**
-	 * @param name
-	 *            Use an empty StringBuilder as input. If this function returns
-	 *            true, this parameter has been filled, if it returns false,
-	 *            nothing happened.
+	 * @param name Use an empty StringBuilder as input. If this function returns
+	 *             true, this parameter has been filled, if it returns false,
+	 *             nothing happened.
 	 * @return Indicates the success of this function and if the last parameter
 	 *         contains output.
 	 */
@@ -157,15 +153,13 @@
 	}
 
 	/**
-	 * Linearizes a tree to a list. The function checks if the provided
-	 * parameter is of type {@link EObject} and linearizes only if that is the
-	 * case.
+	 * Linearizes a tree to a list. The function checks if the provided parameter is
+	 * of type {@link EObject} and linearizes only if that is the case.
 	 * 
-	 * @param object
-	 *            the object to linearize
-	 * @return a list of {@link EObject}s originally contained in the tree
-	 *         structure of the parameter or an empty list if the paramter was
-	 *         not an {@link EObject}
+	 * @param object the object to linearize
+	 * @return a list of {@link EObject}s originally contained in the tree structure
+	 *         of the parameter or an empty list if the paramter was not an
+	 *         {@link EObject}
 	 */
 	public static List<EObject> linearize(Object object) {
 		ArrayList<EObject> elementList = new ArrayList<EObject>();
@@ -177,11 +171,8 @@
 		return elementList;
 	}
 
-	
-
 	/**
-	 * Public API access for other classes to get the name attribute of an
-	 * EObject
+	 * Public API access for other classes to get the name attribute of an EObject
 	 * 
 	 * @param eObject
 	 * @return String
@@ -198,4 +189,35 @@
 		}
 		return name;
 	}
+
+	/**
+	 * Compares to {@link EObject} instances based on their identifier using
+	 * {@link EMFHelper#getIdentifier(EObject)}.
+	 * 
+	 * @param first  the first {@code EObject} to compare
+	 * @param second the second {@code EObject} to compare
+	 * @return {@code true} if the identifiers of the two instances are equal,
+	 *         {@code false} otherwise
+	 */
+	public static boolean hasSameIdentifier(EObject first, EObject second) {
+		return EMFHelper.getIdentifier(first).equals(EMFHelper.getIdentifier(second));
+	}
+
+	/**
+	 * Checks if the given {@link EObject} is in the list of {@code EObject}s using
+	 * {@link #hasSameIdentifier(EObject, EObject)}.
+	 * 
+	 * @param list the list to check
+	 * @param obj  the object to check for
+	 * @return {@code true} if {@code obj} is in {@code list}, {@code false}
+	 *         otherwise
+	 */
+	public static boolean isElementInList(List<EObject> list, EObject obj) {
+		for (EObject next : list) {
+			if (EMFHelper.hasSameIdentifier(obj, next)) {
+				return true;
+			}
+		}
+		return false;
+	}
 }
diff --git a/bundles/org.eclipse.capra.core/src/org/eclipse/capra/core/helpers/TraceHelper.java b/bundles/org.eclipse.capra.core/src/org/eclipse/capra/core/helpers/TraceHelper.java
index a0e2644..f9c7d7a 100644
--- a/bundles/org.eclipse.capra.core/src/org/eclipse/capra/core/helpers/TraceHelper.java
+++ b/bundles/org.eclipse.capra.core/src/org/eclipse/capra/core/helpers/TraceHelper.java
@@ -14,6 +14,7 @@
 package org.eclipse.capra.core.helpers;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -219,4 +220,26 @@
 		return relevantConnections;
 	}
 
+	/**
+	 * Retrieves all unique artifacts connected by the provided collection of
+	 * {@code traces}.
+	 * <p>
+	 * The method will always return a valid set which can be empty.
+	 * 
+	 * @param traces the collection of traces whose artifacts should be retrieved
+	 * @return a set of unique artifacts connected by the provided list of
+	 *         {@code traces}
+	 */
+	public static Set<EObject> getTracedElements(Collection<Connection> traces) {
+		Set<EObject> inserted = new HashSet<>();
+		for (Connection trace : traces) {
+			inserted.add(trace.getOrigin());
+			List<EObject> targets = trace.getTargets();
+			for (EObject target : targets) {
+				inserted.add(target);
+			}
+		}
+		return inserted;
+	}
+
 }
diff --git a/bundles/org.eclipse.capra.ui.matrix/src/org/eclipse/capra/ui/matrix/TraceabilityMatrixDataProvider.java b/bundles/org.eclipse.capra.ui.matrix/src/org/eclipse/capra/ui/matrix/TraceabilityMatrixDataProvider.java
index e723f68..e8de81e 100644
--- a/bundles/org.eclipse.capra.ui.matrix/src/org/eclipse/capra/ui/matrix/TraceabilityMatrixDataProvider.java
+++ b/bundles/org.eclipse.capra.ui.matrix/src/org/eclipse/capra/ui/matrix/TraceabilityMatrixDataProvider.java
@@ -21,6 +21,7 @@
 import org.eclipse.capra.core.adapters.Connection;
 import org.eclipse.capra.core.adapters.TraceMetaModelAdapter;
 import org.eclipse.capra.core.helpers.EMFHelper;
+import org.eclipse.capra.core.helpers.TraceHelper;
 import org.eclipse.emf.ecore.EClass;
 import org.eclipse.emf.ecore.EObject;
 import org.eclipse.nebula.widgets.nattable.data.IDataProvider;
@@ -57,7 +58,7 @@
 	 */
 	public TraceabilityMatrixDataProvider(List<Connection> connections, EObject traceModel,
 			TraceMetaModelAdapter traceAdapter) {
-		for (EObject element : getAllUniqueElements(connections)) {
+		for (EObject element : TraceHelper.getTracedElements(connections)) {
 			EntryData colEntry = new EntryData(element);
 			colEntry.connections = traceAdapter.getConnectedElements(element, traceModel);
 			this.columns.add(colEntry);
@@ -77,7 +78,8 @@
 		EntryData rowEntry = columns.get(rowIndex);
 		for (Connection connection : colEntry.connections) {
 			for (EObject target : connection.getTargets()) {
-				if (!sameElement(colEntry.artifact, target) && sameElement(rowEntry.artifact, target)) {
+				if (!EMFHelper.hasSameIdentifier(colEntry.artifact, target)
+						&& EMFHelper.hasSameIdentifier(rowEntry.artifact, target)) {
 					EObject eClass = connection.getTlink().eClass();
 					return (eClass == null ? "" : ((EClass) eClass).getName());
 				}
@@ -147,7 +149,8 @@
 		EntryData rowEntry = columns.get(row);
 		for (Connection connection : colEntry.connections) {
 			for (EObject target : connection.getTargets()) {
-				if (!sameElement(colEntry.artifact, target) && sameElement(rowEntry.artifact, target)) {
+				if (!EMFHelper.hasSameIdentifier(colEntry.artifact, target)
+						&& EMFHelper.hasSameIdentifier(rowEntry.artifact, target)) {
 					return connection;
 				}
 			}
@@ -155,37 +158,4 @@
 		return null;
 	}
 
-	private List<EObject> getAllUniqueElements(List<Connection> traces) {
-		List<EObject> inserted = new ArrayList<>();
-		for (Connection trace : traces) {
-			EObject element = trace.getOrigin();
-			if (inserted.isEmpty()) {
-				inserted.add(element);
-			} else {
-				if (!isElementInList(inserted, element)) {
-					inserted.add(element);
-				}
-			}
-			List<EObject> targets = trace.getTargets();
-			for (EObject target : targets) {
-				if (!isElementInList(inserted, target)) {
-					inserted.add(target);
-				}
-			}
-		}
-		return inserted;
-	}
-
-	private boolean isElementInList(List<EObject> list, EObject obj) {
-		for (EObject next : list) {
-			if (sameElement(obj, next)) {
-				return true;
-			}
-		}
-		return false;
-	}
-
-	private boolean sameElement(EObject first, EObject second) {
-		return EMFHelper.getIdentifier(first).equals(EMFHelper.getIdentifier(second));
-	}
 }
diff --git a/bundles/org.eclipse.capra.ui.matrix/src/org/eclipse/capra/ui/matrix/views/TraceabilityMatrixView.java b/bundles/org.eclipse.capra.ui.matrix/src/org/eclipse/capra/ui/matrix/views/TraceabilityMatrixView.java
index 17f193b..4c5b7f1 100644
--- a/bundles/org.eclipse.capra.ui.matrix/src/org/eclipse/capra/ui/matrix/views/TraceabilityMatrixView.java
+++ b/bundles/org.eclipse.capra.ui.matrix/src/org/eclipse/capra/ui/matrix/views/TraceabilityMatrixView.java
@@ -28,6 +28,7 @@
 import org.eclipse.capra.core.helpers.ArtifactHelper;
 import org.eclipse.capra.core.helpers.EMFHelper;
 import org.eclipse.capra.core.helpers.ExtensionPointHelper;
+import org.eclipse.capra.core.helpers.TraceHelper;
 import org.eclipse.capra.ui.helpers.SelectionSupportHelper;
 import org.eclipse.capra.ui.matrix.TraceabilityMatrixBodyToolTip;
 import org.eclipse.capra.ui.matrix.TraceabilityMatrixColumnHeaderDataProvider;
@@ -133,6 +134,7 @@
 	private EObject traceModel = null;
 	private EObject artifactModel = null;
 	private ArtifactHelper artifactHelper;
+	private TraceHelper traceHelper;
 
 	private TraceabilityMatrixDataProvider bodyDataProvider;
 	private BodyLayerStack bodyLayer;
@@ -273,6 +275,7 @@
 		this.artifactModel = persistenceAdapter.getArtifactWrappers(resourceSet);
 		this.artifactHelper = new ArtifactHelper(this.artifactModel);
 		this.traceModel = persistenceAdapter.getTraceModel(resourceSet);
+		this.traceHelper = new TraceHelper(traceModel);
 
 		if (!selectedModels.isEmpty()) {
 			// Show the matrix for the selected objects
@@ -287,7 +290,7 @@
 						unpackedElement = model;
 					}
 					EObject wrappedElement = handler.createWrapper(unpackedElement, this.artifactModel);
-					if (isInTraceModel(wrappedElement)) {
+					if (traceHelper.isArtifactInTraceModel(wrappedElement)) {
 						selectedObject = wrappedElement;
 						traces.addAll(this.traceAdapter.getConnectedElements(selectedObject, this.traceModel));
 					}
@@ -341,7 +344,8 @@
 			traceMatrixTable.configure();
 
 			// Attach the selection provider
-			getSite().setSelectionProvider(new TraceabilityMatrixSelectionProvider(bodyLayer.getSelectionLayer(), bodyDataProvider));
+			getSite().setSelectionProvider(
+					new TraceabilityMatrixSelectionProvider(bodyLayer.getSelectionLayer(), bodyDataProvider));
 
 			// Adding the tool tips
 			attachToolTip();
@@ -466,53 +470,6 @@
 		return EMFHelper.getIdentifier(first).equals(EMFHelper.getIdentifier(second));
 	}
 
-	private boolean isElementInList(List<EObject> list, EObject obj) {
-		for (EObject next : list) {
-			if (sameElement(obj, next)) {
-				return true;
-			}
-		}
-		return false;
-	}
-
-	private List<EObject> getAllUniqueElements(List<Connection> traces) {
-		List<EObject> inserted = new ArrayList<>();
-		for (Connection trace : traces) {
-			EObject element = trace.getOrigin();
-			if (inserted.isEmpty()) {
-				inserted.add(element);
-			} else {
-				if (!isElementInList(inserted, element)) {
-					inserted.add(element);
-				}
-			}
-			List<EObject> targets = trace.getTargets();
-			for (EObject target : targets) {
-				if (!isElementInList(inserted, target)) {
-					inserted.add(target);
-				}
-			}
-		}
-		return inserted;
-	}
-
-	/**
-	 * Checks if the given {@code artifact} is part of the trace model.
-	 * 
-	 * @param artifact the artifact whose presence is checked
-	 * @return {@code true} if the artifact is contained in the trace model,
-	 *         {@code false} otherwise
-	 */
-	private boolean isInTraceModel(EObject artifact) {
-		List<EObject> artifacts = getAllUniqueElements(metamodelAdapter.getAllTraceLinks(traceModel));
-
-		if (isElementInList(artifacts, artifact)) {
-			return true;
-		} else {
-			return false;
-		}
-	}
-
 	/**
 	 * Class for the column stack
 	 */
diff --git a/bundles/org.eclipse.capra.ui.sunburst/src/org/eclipse/capra/ui/sunburst/view/SunburstView.java b/bundles/org.eclipse.capra.ui.sunburst/src/org/eclipse/capra/ui/sunburst/view/SunburstView.java
index 24a3b26..e2965c9 100644
--- a/bundles/org.eclipse.capra.ui.sunburst/src/org/eclipse/capra/ui/sunburst/view/SunburstView.java
+++ b/bundles/org.eclipse.capra.ui.sunburst/src/org/eclipse/capra/ui/sunburst/view/SunburstView.java
@@ -30,6 +30,7 @@
 import org.eclipse.capra.core.helpers.ArtifactHelper;
 import org.eclipse.capra.core.helpers.EMFHelper;
 import org.eclipse.capra.core.helpers.ExtensionPointHelper;
+import org.eclipse.capra.core.helpers.TraceHelper;
 import org.eclipse.capra.ui.helpers.SelectionSupportHelper;
 import org.eclipse.capra.ui.sunburst.SunburstPreferences;
 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
@@ -97,6 +98,7 @@
 	private EObject artifactModel = persistenceAdapter.getArtifactWrappers(resourceSet);
 
 	private ArtifactHelper artifactHelper = new ArtifactHelper(artifactModel);
+	private TraceHelper traceHelper = new TraceHelper(traceModel);
 
 	private List<Object> selectedModels = new ArrayList<>();
 
@@ -247,7 +249,7 @@
 						unpackedElement = selection;
 					}
 					EObject wrappedElement = handler.createWrapper(unpackedElement, artifactModel);
-					if (isInTraceModel(wrappedElement)) {
+					if (traceHelper.isArtifactInTraceModel(wrappedElement)) {
 						selectedObject = wrappedElement;
 						if (selectedModels.size() == 1) {
 							secondLevelNodes = getUniqueChildren(null, selectedObject);
@@ -346,13 +348,13 @@
 	 */
 	public List<EObject> getUniqueChildren(List<EObject> prev, EObject artifact) {
 		List<Connection> conNow = traceAdapter.getConnectedElements(artifact, traceModel);
-		List<EObject> allConnected = getAllUniqueElements(conNow);
-		if (isElementInList(allConnected, artifact)) {
+		List<EObject> allConnected = new ArrayList<>(TraceHelper.getTracedElements(conNow));
+		if (EMFHelper.isElementInList(allConnected, artifact)) {
 			allConnected.remove(artifact);
 		}
 		if (prev != null) {
 			for (EObject x : prev) {
-				if (isElementInList(allConnected, x)) {
+				if (EMFHelper.isElementInList(allConnected, x)) {
 					allConnected.remove(x);
 				}
 			}
@@ -361,81 +363,6 @@
 	}
 
 	/**
-	 * Compares to {@link EObject} instances based on their identifier using
-	 * {@link EMFHelper#getIdentifier(EObject)}.
-	 * 
-	 * @param first  the first {@code EObject} to compare
-	 * @param second the second {@code EObject} to compare
-	 * @return {@code true} if the identifiers of the two instances are equal,
-	 *         {@code false} otherwise
-	 */
-	private boolean sameElement(EObject first, EObject second) {
-		return EMFHelper.getIdentifier(first).equals(EMFHelper.getIdentifier(second));
-	}
-
-	/**
-	 * Checks if the given {@link EObject} is in the list of {@code EObject}s using
-	 * {@link #sameElement(EObject, EObject)}.
-	 * 
-	 * @param list the list to check
-	 * @param obj  the object to check for
-	 * @return {@code true} if {@code obj} is in {@code list}, {@code false}
-	 *         otherwise
-	 */
-	private boolean isElementInList(List<EObject> list, EObject obj) {
-		for (EObject next : list) {
-			if (sameElement(obj, next)) {
-				return true;
-			}
-		}
-		return false;
-	}
-
-	/**
-	 * Retrieves a list of all unique artifacts connected by the provided list of
-	 * {@code traces}.
-	 * <p>
-	 * The method will always return a valid list which can be empty.
-	 * 
-	 * @param traces the list of traces whose artifacts
-	 * @return a list of unique artifacts connected by the provided list of
-	 *         {@code traces}
-	 */
-	private List<EObject> getAllUniqueElements(List<Connection> traces) {
-		List<EObject> inserted = new ArrayList<>();
-		for (Connection trace : traces) {
-			EObject element = trace.getOrigin();
-			if (!isElementInList(inserted, element)) {
-				inserted.add(element);
-			}
-
-			for (EObject target : trace.getTargets()) {
-				if (!isElementInList(inserted, target)) {
-					inserted.add(target);
-				}
-			}
-		}
-		return inserted;
-	}
-
-	/**
-	 * Checks if the given {@code artifact} is part of the trace model.
-	 * 
-	 * @param artifact the artifact whose presence is checked
-	 * @return {@code true} if the artifact is contained in the trace model,
-	 *         {@code false} otherwise
-	 */
-	private boolean isInTraceModel(EObject artifact) {
-		List<EObject> artifacts = getAllUniqueElements(metamodelAdapter.getAllTraceLinks(traceModel));
-
-		if (isElementInList(artifacts, artifact)) {
-			return true;
-		} else {
-			return false;
-		}
-	}
-
-	/**
 	 * Class to allow selecting the maximum traversal depth of trace model. Stores
 	 * the selection in the Eclipse Capra preferences.
 	 */