Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilliam Chen2012-05-03 09:42:35 +0000
committerWilliam Chen2012-05-03 09:42:35 +0000
commit3c010ba2d91f0576a7459a761c15687f9a23b1ab (patch)
tree3141bf64300a716c21a0bbddbf34d1b461d016fc /target_explorer/plugins/org.eclipse.tcf.te.core
parent8a95f2529a739c273c58fe83f155775dbdd6b861 (diff)
downloadorg.eclipse.tcf-3c010ba2d91f0576a7459a761c15687f9a23b1ab.tar.gz
org.eclipse.tcf-3c010ba2d91f0576a7459a761c15687f9a23b1ab.tar.xz
org.eclipse.tcf-3c010ba2d91f0576a7459a761c15687f9a23b1ab.zip
Target Explorer: Remove the redundant classes.
Diffstat (limited to 'target_explorer/plugins/org.eclipse.tcf.te.core')
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.core/src/org/eclipse/tcf/te/core/utils/Ancestor.java114
1 files changed, 114 insertions, 0 deletions
diff --git a/target_explorer/plugins/org.eclipse.tcf.te.core/src/org/eclipse/tcf/te/core/utils/Ancestor.java b/target_explorer/plugins/org.eclipse.tcf.te.core/src/org/eclipse/tcf/te/core/utils/Ancestor.java
new file mode 100644
index 000000000..698b81bf4
--- /dev/null
+++ b/target_explorer/plugins/org.eclipse.tcf.te.core/src/org/eclipse/tcf/te/core/utils/Ancestor.java
@@ -0,0 +1,114 @@
+package org.eclipse.tcf.te.core.utils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.core.runtime.Assert;
+
+public abstract class Ancestor<T> {
+
+ /**
+ * Get an element which is the common ancestor of all the specified elements.
+ *
+ * @param elements The element list.
+ * @return The ancestor element.
+ */
+ protected T getAncestor(List<T> elements) {
+ if (elements.isEmpty()) return null;
+ if (elements.size() == 1) return elements.get(0);
+ T element1 = elements.get(0);
+ for (int i = 1; i < elements.size(); i++) {
+ T element2 = elements.get(i);
+ element1 = getCommonAncestor(element1, element2);
+ if (element1 == null) return null;
+ }
+ return element1;
+ }
+
+ /**
+ * Get the top most elements of the specified list.
+ *
+ * @param elements The original list.
+ * @return The top most elements.
+ */
+ protected List<T> getAncestors(List<T> elements) {
+ List<T> result = new ArrayList<T>();
+ for (T element : elements) {
+ if (!hasAncestor(element, elements)) {
+ result.add(element);
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Get the common ancestor of the specified two elements.
+ *
+ * @param element1 The first element.
+ * @param element2 The second element.
+ * @return The common ancestor.
+ */
+ private T getCommonAncestor(T element1, T element2) {
+ Assert.isNotNull(element1);
+ Assert.isNotNull(element2);
+ if (isAncestorOf(element1, element2)) {
+ return element1;
+ }
+ if (isAncestorOf(element2, element1)) {
+ return element2;
+ }
+ T ancestor = null;
+ T parent1 = getParent(element1);
+ if(parent1 != null) {
+ ancestor = getCommonAncestor(parent1, element2);
+ }
+ if(ancestor != null) return ancestor;
+ T parent2 = getParent(element2);
+ if(parent2 != null) {
+ ancestor = getCommonAncestor(element1, parent2);
+ }
+ if(ancestor != null) return ancestor;
+ if(parent1 != null && parent2 != null) {
+ ancestor = getCommonAncestor(parent1, parent2);
+ }
+ return ancestor;
+ }
+
+ /**
+ * If the target element has ancestor in the specified list.
+ *
+ * @param element The element to be tested.
+ * @param elements The element list to search in.
+ * @return true if the element has an ancestor in the list.
+ */
+ private boolean hasAncestor(T element, List<T> elements) {
+ for (T node : elements) {
+ if (isAncestorOf(node, element)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Judges if the first element is an ancestor of the second element.
+ *
+ * @param element1 The first element to be tested.
+ * @param element2 The second element to be tested.
+ * @return true if the first element is the ancestor of the second element.
+ */
+ private boolean isAncestorOf(T element1, T element2) {
+ if (element2 == null) return false;
+ T parent = getParent(element2);
+ if (parent == element1) return true;
+ return isAncestorOf(element1, parent);
+ }
+
+ /**
+ * Get the parent of the specified element in the display thread.
+ *
+ * @param element The element
+ * @return its parent.
+ */
+ protected abstract T getParent(T element);
+}

Back to the top