Merge from ECOMP's repository
[vid.git] / vid-app-common / src / main / java / org / onap / vid / utils / Tree.kt
1 package org.onap.vid.utils
2
3 data class Node<T>(val value:T, val children:MutableMap<T, Node<T>> = hashMapOf())
4
5 data class Tree<T>(private val root:Node<T>) {
6
7     constructor(value: T) : this(Node(value))
8
9     fun getRootValue():T {
10         return root.value;
11     }
12
13     fun addPath(vararg path: T) {
14         addPath(path.asList())
15     }
16
17     fun addPath(path:Collection<T>) {
18         var currentNode = root
19         path.forEach {
20             currentNode = currentNode.children.getOrPut(it) {Node(it)}
21         }
22     }
23
24     fun getSubTree(vararg path: T): Tree<T>? {
25         return getSubTree(path.asList())
26     }
27
28     fun getSubTree(path:Collection<T>): Tree<T>? {
29         var currentNode:Node<T> = root
30         path.forEach {
31             currentNode = currentNode.children[it] ?: return null
32         }
33         return Tree(currentNode)
34     }
35
36     fun isPathExist(vararg path: T): Boolean {
37         return isPathExist(path.asList())
38     }
39
40     fun isPathExist(path:Collection<T>): Boolean {
41         return getSubTree(path)!=null
42     }
43 }
44