-
-
Save heharkon/db7072d0ee42fd8fbc16ec403efc777d to your computer and use it in GitHub Desktop.
LPath
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class LPath(root: Boolean, folders: Vector[String], file: Option[String]) { | |
def fromRoot: LPath = LPath(true, folders, file) | |
def withFile(file: String): LPath = LPath(root, folders, Some(file)) | |
def withFolder(f: String) = LPath(root, folders ++ Vector(f), file) | |
def withFolders(fs: List[String]) = LPath(root, folders ++ fs.toVector, file) | |
override def toString: String = s"${if (root) "/" else ""}${if (folders.length > 0) folders.mkString("/") + "/" else ""}${file.getOrElse("")}" | |
def ++(other: LPath): LPath = join(other) | |
def join(other: LPath): LPath = { | |
other match { | |
case LPath(false, _, _) if file.isEmpty => LPath(root, folders ++ other.folders, other.file) | |
case _ => | |
if (other.root) | |
throw new Exception("Cannot append with a path containing root.") | |
else | |
throw new Exception("Cannot append to a path with file.") | |
} | |
} | |
} | |
object LPath { | |
def apply(): LPath = LPath(false, Vector.empty, None) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment