diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2013-04-28 00:33:06 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2016-11-10 18:03:23 +0100 |
commit | 3e2f88773c61e2d0020b57161d0e901bec596c96 (patch) | |
tree | fe5c381e4a731600a60b4ac9862af672b5b7aa12 /Scala/patmat/src/main/scala/common/package.scala | |
parent | 12ef23e6da7b7186bec20d426c087d757260659d (diff) | |
download | coursera-3e2f88773c61e2d0020b57161d0e901bec596c96.zip coursera-3e2f88773c61e2d0020b57161d0e901bec596c96.tar.gz |
Scala : add patmat
Diffstat (limited to 'Scala/patmat/src/main/scala/common/package.scala')
-rw-r--r-- | Scala/patmat/src/main/scala/common/package.scala | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/Scala/patmat/src/main/scala/common/package.scala b/Scala/patmat/src/main/scala/common/package.scala new file mode 100644 index 0000000..f1c74c3 --- /dev/null +++ b/Scala/patmat/src/main/scala/common/package.scala @@ -0,0 +1,40 @@ +import java.io.File + +package object common { + + /** An alias for the `Nothing` type. + * Denotes that the type should be filled in. + */ + type ??? = Nothing + + /** An alias for the `Any` type. + * Denotes that the type should be filled in. + */ + type *** = Any + + + /** + * Get a child of a file. For example, + * + * subFile(homeDir, "b", "c") + * + * corresponds to ~/b/c + */ + def subFile(file: File, children: String*) = { + children.foldLeft(file)((file, child) => new File(file, child)) + } + + /** + * Get a resource from the `src/main/resources` directory. Eclipse does not copy + * resources to the output directory, then the class loader cannot find them. + */ + def resourceAsStreamFromSrc(resourcePath: List[String]): Option[java.io.InputStream] = { + val classesDir = new File(getClass.getResource(".").toURI) + val projectDir = classesDir.getParentFile.getParentFile.getParentFile.getParentFile + val resourceFile = subFile(projectDir, ("src" :: "main" :: "resources" :: resourcePath): _*) + if (resourceFile.exists) + Some(new java.io.FileInputStream(resourceFile)) + else + None + } +} |