Last active
August 29, 2015 14:03
-
-
Save ElectricCoffee/6f47431bf5122c187093 to your computer and use it in GitHub Desktop.
A simple bit of extension methods that lets you write "someFolder" / "otherFolder" / "LastFolder" to create a java.io.File with that kind of path. An important feature of this, is that it's completely OS agnostic, meaning you don't have to worry about it being \ in Windows and / in Unix-based OSs, you just write it, and it handles the rest from …
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
import java.io.File | |
/** | |
* A completely OS-Agnostic way of dealing with java.io.File paths | |
*/ | |
object RichPath { | |
sealed class RichBase[+A](left: A) { | |
/** | |
* Simple enrichment method designed to let you create a java.io.File | |
* by simply writing "folderA" / "folderB" / "folderC" | |
* @param right the right-side string | |
* @return new file with the given path | |
*/ | |
def /(right: String): File = new File(left.toString, right) | |
} | |
/** | |
* Enriched File class, lets you write | |
* someFile / "String" to create a file with that kind of path | |
* @param left what's written on the left side of the / | |
*/ | |
implicit class RichFile(left: File) extends RichBase(left) { | |
/** | |
* Replaces all the \ in a path name with \\ | |
* This is useful if you want to avoid the first letter in each subfolder being escaped | |
* Example: "C:\Users\Username\Desktop" becomes "C:\\User\\Username\\Desktop" | |
* This makes it compatible with json files, as well as in-program strings | |
* @return an escaped representation of the string path | |
*/ | |
def toEscapedString: String = left.toString.replaceAllLiterally("\\", "\\\\") | |
} | |
/** | |
* Enriched String class, lets you write | |
* "someString" / "String" to create a file with that kind of path | |
* @param left what's written on the left side of the / | |
*/ | |
implicit class RichString(left: String) extends RichBase(left) { | |
/** | |
* Provides a mean to turn a string into a file | |
* @return File based on string | |
*/ | |
def toFile: File = new File(left) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment