1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
/*******************************************************************************
* Copyright (c) 2014 Wind River Systems, Inc. and others. All rights reserved.
* This program and the accompanying materials are made available under the terms
* of the Eclipse Public License v1.0 which accompanies this distribution, and is
* available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Wind River Systems - initial API and implementation
*******************************************************************************/
package org.eclipse.tcf.te.core.utils;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.runtime.Assert;
/**
* A base class whose subclasses should implement getParent to use its
* getAncestor(s) methods.
*
* @param <T> The element type.
*/
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);
}
|