1
|
/**
|
2
|
* Copyright (C) 2016 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.model.common;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.Collection;
|
13
|
import java.util.Collections;
|
14
|
import java.util.HashMap;
|
15
|
import java.util.List;
|
16
|
import java.util.Map;
|
17
|
import java.util.regex.Pattern;
|
18
|
|
19
|
import eu.etaxonomy.cdm.compare.TreeIndexComparator;
|
20
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
21
|
import eu.etaxonomy.cdm.model.term.TermNode;
|
22
|
|
23
|
/**
|
24
|
* A class to handle tree indexes as used in {@link TaxonNode}, {@link TermNode}
|
25
|
* etc.<BR>
|
26
|
* Might be come a hibernate user type in future.
|
27
|
*
|
28
|
* @author a.mueller
|
29
|
* @since 02.12.2016
|
30
|
*/
|
31
|
public class TreeIndex {
|
32
|
|
33
|
public static String sep = "#";
|
34
|
|
35
|
//regEx, we also allow the tree itself to have a tree index (e.g. #t1#)
|
36
|
//this may change in future as not necessarily needed
|
37
|
private static String regEx = sep+"[a-z](\\d+"+sep+")+";
|
38
|
private static Pattern pattern = Pattern.compile(regEx);
|
39
|
|
40
|
private static TreeIndexComparator comparator = new TreeIndexComparator();
|
41
|
|
42
|
private String treeIndex;
|
43
|
|
44
|
//***************** FACTORY ********************************/
|
45
|
|
46
|
public static TreeIndex NewInstance(String treeIndex){
|
47
|
return new TreeIndex(treeIndex);
|
48
|
}
|
49
|
|
50
|
public static TreeIndex NewInstance(TaxonNode node) {
|
51
|
if (node == null){
|
52
|
return null;
|
53
|
}else{
|
54
|
return new TreeIndex(node.treeIndex());
|
55
|
}
|
56
|
}
|
57
|
|
58
|
public static List<TreeIndex> NewListInstance(List<String> stringList) {
|
59
|
List<TreeIndex> result = new ArrayList<>();
|
60
|
for (String string: stringList){
|
61
|
result.add(new TreeIndex(string));
|
62
|
}
|
63
|
return result;
|
64
|
}
|
65
|
|
66
|
// ******************* CONSTRUCTOR ********************/
|
67
|
|
68
|
private TreeIndex(String treeIndex){
|
69
|
if (! pattern.matcher(treeIndex).matches()){
|
70
|
throw new IllegalArgumentException("Given string is not a valid tree index");
|
71
|
}
|
72
|
this.treeIndex = treeIndex;
|
73
|
}
|
74
|
|
75
|
// ************** METHODS ***************************************/
|
76
|
|
77
|
public boolean hasChild(TreeIndex childCandidateTreeIndex) {
|
78
|
return childCandidateTreeIndex.treeIndex.startsWith(treeIndex);
|
79
|
}
|
80
|
|
81
|
/**
|
82
|
* Returns a new TreeIndex instance which represents the parent of this tree index.
|
83
|
* Returns null if this tree index already represents the root node of the tree.
|
84
|
*/
|
85
|
public TreeIndex parent(){
|
86
|
int index = treeIndex.substring(0, treeIndex.length()-1).lastIndexOf(ITreeNode.separator);
|
87
|
try {
|
88
|
TreeIndex result = index < 0 ? null : NewInstance(treeIndex.substring(0, index+1));
|
89
|
return result;
|
90
|
} catch (Exception e) {
|
91
|
//it is not a valid treeindex anymore
|
92
|
return null;
|
93
|
}
|
94
|
}
|
95
|
|
96
|
public boolean isTreeRoot(){
|
97
|
int count = 0;
|
98
|
for (char c : this.treeIndex.toCharArray()){
|
99
|
if (c == '#') {
|
100
|
count++;
|
101
|
}
|
102
|
}
|
103
|
return count == 3;
|
104
|
}
|
105
|
|
106
|
public boolean isTree(){
|
107
|
int count = 0;
|
108
|
for (char c : this.treeIndex.toCharArray()){
|
109
|
if (c == '#') {
|
110
|
count++;
|
111
|
}
|
112
|
}
|
113
|
return count == 2;
|
114
|
}
|
115
|
|
116
|
// ********************** STATIC METHODS *****************************/
|
117
|
|
118
|
/**
|
119
|
* Returns a list of string based tree node ids of all ancestors. Starts with the highest ancestor.
|
120
|
*
|
121
|
* @param includeRoot if the root node which has no data attached should be included
|
122
|
* @param includeTree if the tree node which precedes the treeindex representing the tree should be included
|
123
|
*/
|
124
|
public List<String> parentNodeIds(boolean includeRoot, boolean includeTree){
|
125
|
String[] splits = treeIndex.split(sep);
|
126
|
List<String> result = new ArrayList<>();
|
127
|
for (int i = 1; i<splits.length; i++){ //the first split is empty
|
128
|
if (i > 2 || i == 1 && includeTree || i == 2 && includeRoot){
|
129
|
result.add(splits[i]);
|
130
|
}
|
131
|
}
|
132
|
return result;
|
133
|
}
|
134
|
|
135
|
/**
|
136
|
* Returns a list of integer based tree node ids of all ancestors. Starts with the highest ancestor.
|
137
|
* @param treeIndex the tree index
|
138
|
* @param includeRoot if the root node which has no data attached should be included
|
139
|
*/
|
140
|
public List<Integer> parentNodeIds(boolean includeRoot){
|
141
|
List<String> split = parentNodeIds(includeRoot, false);
|
142
|
List<Integer> result = new ArrayList<>();
|
143
|
for (String str:split){
|
144
|
result.add(Integer.valueOf(str));
|
145
|
}
|
146
|
return result;
|
147
|
}
|
148
|
|
149
|
/**
|
150
|
* Creates a list for the given tree indexes and sorts them in ascending
|
151
|
* order.
|
152
|
* @param treeIndexSet
|
153
|
* @return
|
154
|
*/
|
155
|
public static List<TreeIndex> sort(Collection<TreeIndex> treeIndexSet) {
|
156
|
List<TreeIndex> result = new ArrayList<>(treeIndexSet);
|
157
|
Collections.sort(result, comparator);
|
158
|
return result;
|
159
|
}
|
160
|
|
161
|
/**
|
162
|
* Creates a list for the given tree indexes and sorts them in descending
|
163
|
* order.
|
164
|
*/
|
165
|
public static List<TreeIndex> sortDesc(Collection<TreeIndex> treeIndexSet) {
|
166
|
List<TreeIndex> result = sort(treeIndexSet);
|
167
|
Collections.reverse(result);
|
168
|
return result;
|
169
|
}
|
170
|
|
171
|
public static Map<TreeIndex, TreeIndex> group(Collection<TreeIndex> groupingIndexes, Collection<TreeIndex> toBeGroupedIndexes){
|
172
|
|
173
|
//for larger groupingIndexes we could optimize this by sorting both collections
|
174
|
//prior to loop. This way we do traverse both lists once
|
175
|
Map<TreeIndex, TreeIndex> result = new HashMap<>();
|
176
|
List<TreeIndex> descSortedGroupingIndexes = sortDesc(groupingIndexes);
|
177
|
|
178
|
for (TreeIndex toBeGrouped : toBeGroupedIndexes) {
|
179
|
boolean groupFound = false;
|
180
|
for (TreeIndex groupingIndex : descSortedGroupingIndexes){
|
181
|
if (groupingIndex.hasChild(toBeGrouped)){
|
182
|
result.put(toBeGrouped, groupingIndex);
|
183
|
groupFound = true;
|
184
|
break;
|
185
|
}
|
186
|
}
|
187
|
if (!groupFound){
|
188
|
result.put(toBeGrouped, null);
|
189
|
}
|
190
|
}
|
191
|
return result;
|
192
|
}
|
193
|
|
194
|
// **************************** EQUALS *****************************/
|
195
|
|
196
|
@Override
|
197
|
public int hashCode() {
|
198
|
return treeIndex.hashCode();
|
199
|
}
|
200
|
|
201
|
@Override
|
202
|
public boolean equals(Object obj) {
|
203
|
if (obj instanceof TreeIndex){
|
204
|
return treeIndex.equals(((TreeIndex)obj).treeIndex);
|
205
|
}else{
|
206
|
return false;
|
207
|
}
|
208
|
}
|
209
|
|
210
|
// *************************** toString() ***********************
|
211
|
|
212
|
@Override
|
213
|
public String toString(){
|
214
|
return treeIndex;
|
215
|
}
|
216
|
|
217
|
/**
|
218
|
* Null save toString method.
|
219
|
*/
|
220
|
public static String toString(TreeIndex treeIndex) {
|
221
|
return treeIndex == null? null: treeIndex.toString();
|
222
|
}
|
223
|
|
224
|
public static List<String> toString(Collection<TreeIndex> treeIndexes) {
|
225
|
List<String> result = new ArrayList<>();
|
226
|
for (TreeIndex treeIndex : treeIndexes){
|
227
|
result.add(treeIndex.toString());
|
228
|
}
|
229
|
return result;
|
230
|
}
|
231
|
}
|