All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.justai.jaicf.model.state.StatePath.kt Maven / Gradle / Ivy

package com.justai.jaicf.model.state

class StatePath {

    private val path: List

    constructor(vararg path: String) {
        this.path = listOf(*path)
    }

    constructor(path: List) {
        this.path = path
    }

    override fun toString(): String {
        return if (isRoot) {
            "/"
        } else {
            path.joinToString(separator = "/")
        }
    }

    fun stepUp(): StatePath {
        return StatePath(path.subList(0, path.size - 1))
    }

    val isRoot: Boolean
        get() = path.size == 1 && path[0] == ""

    val name: String
        get() = path[path.size - 1]

    val components: Array
        get() = path.toTypedArray()

    val parents: Array
        get() {
            val ret = arrayOfNulls(path.size - 1)
            for (i in (path.size - 2) downTo 1) {
                ret[i] = path.subList(0, i + 1).joinToString(separator = "/")
            }
            if (path.size > 1) {
                ret[0] = "/"
            }
            return ret.requireNoNulls()
        }

    val parent: String
        get() {
            val ret = path.subList(0, path.size - 1).joinToString(separator = "/")
            return if (ret.isEmpty()) {
                "/"
            } else ret
        }

    fun resolve(subpath: String): StatePath {
        val path = parse(subpath)

        return if (subpath.startsWith("/")) {
            path.normalized()
        } else {
            StatePath(this.path + path.path).normalized()
        }
    }

    private fun normalized(): StatePath {
        val normalizedPath = mutableListOf()

        for ((index, item) in this.path.withIndex()) {
            when {
                item == "" && index != 0 -> continue
                item == CUR -> continue
                item == UP -> normalizedPath.removeLastOrNull()
                else -> normalizedPath.add(item)
            }
        }
        return StatePath(normalizedPath)
    }

    companion object {

        private val ROOT = ""
        private val CUR = "."
        private val UP = ".."

        fun root() =
            StatePath(ROOT)

        fun parse(path: String): StatePath {
            if (path.matches(Regex("/+"))) {
                return root()
            }
            val s = path.split("/".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()

            return StatePath(*s)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy