diff options
Diffstat (limited to 'vid-app-common/src/main/java/org/onap/vid/utils/Tree.kt')
-rw-r--r-- | vid-app-common/src/main/java/org/onap/vid/utils/Tree.kt | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/vid-app-common/src/main/java/org/onap/vid/utils/Tree.kt b/vid-app-common/src/main/java/org/onap/vid/utils/Tree.kt new file mode 100644 index 00000000..28f989bb --- /dev/null +++ b/vid-app-common/src/main/java/org/onap/vid/utils/Tree.kt @@ -0,0 +1,44 @@ +package org.onap.vid.utils + +data class Node<T>(val value:T, val children:MutableMap<T, Node<T>> = hashMapOf()) + +data class Tree<T>(private val root:Node<T>) { + + constructor(value: T) : this(Node(value)) + + fun getRootValue():T { + return root.value; + } + + fun addPath(vararg path: T) { + addPath(path.asList()) + } + + fun addPath(path:Collection<T>) { + var currentNode = root + path.forEach { + currentNode = currentNode.children.getOrPut(it) {Node(it)} + } + } + + fun getSubTree(vararg path: T): Tree<T>? { + return getSubTree(path.asList()) + } + + fun getSubTree(path:Collection<T>): Tree<T>? { + var currentNode:Node<T> = root + path.forEach { + currentNode = currentNode.children[it] ?: return null + } + return Tree(currentNode) + } + + fun isPathExist(vararg path: T): Boolean { + return isPathExist(path.asList()) + } + + fun isPathExist(path:Collection<T>): Boolean { + return getSubTree(path)!=null + } +} + |