Add toString to Path

This commit is contained in:
Alexander Nozik 2021-01-12 18:34:25 +03:00
parent b916a038f7
commit 3334380693

View File

@ -19,28 +19,15 @@ import hep.dataforge.names.Name
import hep.dataforge.names.toName import hep.dataforge.names.toName
/** /**
*
*
* Path interface. * Path interface.
* *
* @author Alexander Nozik
* @version $Id: $Id
*/ */
public inline class Path(public val tokens: List<PathToken>) : Iterable<PathToken> { public inline class Path(public val tokens: List<PathToken>) : Iterable<PathToken> {
public val head: PathToken? get() = tokens.firstOrNull()
public val length: Int get() = tokens.size
/**
* Returns non-empty optional containing the chain without first segment in case of chain path.
*
* @return
*/
public val tail: Path? get() = if (tokens.isEmpty()) null else Path(tokens.drop(1))
override fun iterator(): Iterator<PathToken> = tokens.iterator() override fun iterator(): Iterator<PathToken> = tokens.iterator()
override fun toString(): String = tokens.joinToString(separator = PATH_SEGMENT_SEPARATOR)
public companion object { public companion object {
public const val PATH_SEGMENT_SEPARATOR: String = "/" public const val PATH_SEGMENT_SEPARATOR: String = "/"
@ -52,6 +39,19 @@ public inline class Path(public val tokens: List<PathToken>) : Iterable<PathToke
} }
} }
public val Path.length: Int get() = tokens.size
public val Path.head: PathToken? get() = tokens.firstOrNull()
/**
* Returns non-empty optional containing the chain without first segment in case of chain path.
*
* @return
*/
public val Path.tail: Path? get() = if (tokens.isEmpty()) null else Path(tokens.drop(1))
public operator fun Path.plus(path: Path): Path = Path(this.tokens + path.tokens) public operator fun Path.plus(path: Path): Path = Path(this.tokens + path.tokens)
public data class PathToken(val name: Name, val target: String? = null) { public data class PathToken(val name: Name, val target: String? = null) {