Project

General

Profile

Revision 2d726275

ID2d7262752d99677e4ff7cc573961d90a4558cd1f
Parent 820ca4ec
Child 09e88a38

Added by Andreas Müller 7 months ago

ref #1447 add ancestor id splitter to TreeIndex

View differences:

cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/TreeIndex.java
26 26
 *
27 27
 * @author a.mueller
28 28
 * @since 02.12.2016
29
 *
30 29
 */
31 30
public class TreeIndex {
32 31

  
32
    public static String sep = "#";
33

  
34
    //regEx, we also allow the tree itself to have a tree index (e.g. #t1#)
35
    //this may change in future as not necessarily needed
36
    private static String regEx = sep+"[a-z](\\d+"+sep+")+";
37
    private static Pattern pattern = Pattern.compile(regEx);
38

  
39
    private static TreeIndexComparator comparator = new TreeIndexComparator();
40

  
41
    private String treeIndex;
42

  
43
//***************** FACTORY ********************************/
44

  
33 45
    public static TreeIndex NewInstance(String treeIndex){
34 46
        return new TreeIndex(treeIndex);
35 47
    }
36 48

  
37

  
38
    /**
39
     * @param subtree
40
     * @return
41
     */
42 49
    public static TreeIndex NewInstance(TaxonNode node) {
43 50
        if (node == null){
44 51
            return null;
......
47 54
        }
48 55
    }
49 56

  
50

  
51
    /**
52
     * @param stringList
53
     * @return
54
     */
55 57
    public static List<TreeIndex> NewListInstance(List<String> stringList) {
56 58
        List<TreeIndex> result = new ArrayList<>();
57 59
        for (String string: stringList){
......
60 62
        return result;
61 63
    }
62 64

  
63
    //regEx, we also allow the tree itself to have a tree index (e.g. #t1#)
64
    //this may change in future as not necessarily needed
65
    private static String regEx = "#[a-z](\\d+#)+";
66
    private static Pattern pattern = Pattern.compile(regEx);
67

  
68
    private static TreeIndexComparator comparator = new TreeIndexComparator();
69

  
70
    private String treeIndex;
65
// ******************* CONSTRUCTOR ********************/
71 66

  
72 67
    private TreeIndex(String treeIndex){
73 68
        if (! pattern.matcher(treeIndex).matches()){
......
78 73

  
79 74
// ************** METHODS ***************************************/
80 75

  
81
    /**
82
     * @param taxonTreeIndex
83
     * @return
84
     */
85 76
    public boolean hasChild(TreeIndex childCandidateTreeIndex) {
86 77
        return childCandidateTreeIndex.treeIndex.startsWith(treeIndex);
87 78
    }
88 79

  
89

  
90 80
    /**
91 81
     * Returns a new TreeIndex instance which represents the parent of this tree index.
92 82
     * Returns null if this tree index already represents the root node of the tree.
93
     * @return
94 83
     */
95 84
    public TreeIndex parent(){
96 85
        int index = treeIndex.substring(0, treeIndex.length()-1).lastIndexOf(ITreeNode.separator);
......
126 115
// ********************** STATIC METHODS  *****************************/
127 116

  
128 117
    /**
118
     * Returns a list of string based tree node ids of all ancestors. Starts with the highest ancestor.
119
     *
120
     * @param includeRoot if the root node which has no data attached should be included
121
     * @param includeTree if the tree node which precedes the treeindex representing the tree should be included
122
     */
123
    public List<String> parentNodeIds(boolean includeRoot, boolean includeTree){
124
        String[] splits = treeIndex.split(sep);
125
        List<String> result = new ArrayList<>();
126
        for (int i = 1; i<splits.length; i++){  //the first split is empty
127
            if (i > 2 || i == 1 && includeTree || i == 2 && includeRoot){
128
                result.add(splits[i]);
129
            }
130
        }
131
        return result;
132
    }
133

  
134
    /**
135
     * Returns a list of integer based tree node ids of all ancestors. Starts with the highest ancestor.
136
     * @param treeIndex the tree index
137
     * @param includeRoot if the root node which has no data attached should be included
138
     */
139
    public List<Integer> parentNodeIds(boolean includeRoot){
140
        List<String> split = parentNodeIds(includeRoot, false);
141
        List<Integer> result = new ArrayList<>();
142
        for (String str:split){
143
            result.add(Integer.valueOf(str));
144
        }
145
        return result;
146
    }
147

  
148
    /**
129 149
     * Creates a list for the given tree indexes and sorts them in ascending
130 150
     * order.
131 151
     * @param treeIndexSet
......
137 157
        return result;
138 158
    }
139 159

  
140

  
141 160
    /**
142 161
     * Creates a list for the given tree indexes and sorts them in descending
143 162
     * order.
144
     * @param treeIndexSet
145
     * @return
146 163
     */
147 164
    public static List<TreeIndex> sortDesc(Collection<TreeIndex> treeIndexSet) {
148 165
        List<TreeIndex> result = sort(treeIndexSet);
......
173 190
        return result;
174 191
    }
175 192

  
176

  
177 193
// **************************** EQUALS *****************************/
178 194

  
179 195
    @Override
......
192 208

  
193 209
// *************************** toString() ***********************
194 210

  
195

  
196

  
197 211
    @Override
198 212
    public String toString(){
199 213
        return treeIndex;
......
201 215

  
202 216
    /**
203 217
     * Null save toString method.
204
     * @param treeIndex
205
     * @return
206 218
     */
207 219
    public static String toString(TreeIndex treeIndex) {
208 220
        return treeIndex == null? null: treeIndex.toString();
209 221
    }
210 222

  
211

  
212
    /**
213
     * @param treeIndexes
214
     * @return
215
     */
216 223
    public static List<String> toString(Collection<TreeIndex> treeIndexes) {
217 224
        List<String> result = new ArrayList<>();
218 225
        for (TreeIndex treeIndex : treeIndexes){
......
220 227
        }
221 228
        return result;
222 229
    }
223

  
224

  
225

  
226 230
}
cdmlib-model/src/test/java/eu/etaxonomy/cdm/model/common/TreeIndexTest.java
8 8
*/
9 9
package eu.etaxonomy.cdm.model.common;
10 10

  
11
import java.util.List;
12

  
11 13
import org.junit.Assert;
12 14
import org.junit.Before;
13 15
import org.junit.Test;
......
15 17
/**
16 18
 * @author a.mueller
17 19
 * @since 12.09.2018
18
 *
19 20
 */
20 21
public class TreeIndexTest {
21 22

  
......
70 71
        Assert.assertTrue("Index should be tree", indexTree.isTree());
71 72
    }
72 73

  
74
    @Test
75
    public void testParentNodeIds() {
76
        TreeIndex myIndex = TreeIndex.NewInstance("#t1#222#33#444#515#");
77
        List<Integer> nodeIds = myIndex.parentNodeIds(false);
78
        Assert.assertEquals("Last index should be 515", (Integer)515, nodeIds.get(nodeIds.size()-1));
79
        List<String> nodeIdsStr = myIndex.parentNodeIds(true, true);
80
        Assert.assertEquals("1st index should be 't1'", "t1", nodeIdsStr.get(0));
81
        Assert.assertEquals("3rd index should be '33'", "33", nodeIdsStr.get(2));
82
    }
83

  
73 84
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)