aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/main/java/org/onap/vid/utils/Tree.kt
blob: 28f989bb3aecb40d7bb505a47fdd3d0f73c14c22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
    }
}