aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/main/java/org/onap/vid/utils/Tree.kt
diff options
context:
space:
mode:
authorIttay Stern <ittay.stern@att.com>2019-01-10 12:42:59 +0000
committerGerrit Code Review <gerrit@onap.org>2019-01-10 12:42:59 +0000
commit659874df80409170e57b6dd6197eb3a81eb3468e (patch)
tree08e79fd57aa0ed299586fd88f69f87a5a2f5ded1 /vid-app-common/src/main/java/org/onap/vid/utils/Tree.kt
parent143784a849ece4fc35cc290e33f829b72bf1fc79 (diff)
parent6ad41e3ccd398a2721f41ad61c80b7bb03f7d127 (diff)
Merge "Merge from ECOMP's repository"
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.kt44
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 000000000..28f989bb3
--- /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
+ }
+}
+