Skip to content

Instantly share code, notes, and snippets.

@heharkon
Last active February 5, 2018 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heharkon/db7072d0ee42fd8fbc16ec403efc777d to your computer and use it in GitHub Desktop.
Save heharkon/db7072d0ee42fd8fbc16ec403efc777d to your computer and use it in GitHub Desktop.
LPath
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