From dcdc4d3e6ed08f37f7acd75c79ddea607d2e8e2a Mon Sep 17 00:00:00 2001 From: Jérémy Zurcher Date: Wed, 27 Mar 2013 23:25:43 +0100 Subject: Scala : add scala/example --- Scala/example/src/main/scala/common/package.scala | 40 +++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Scala/example/src/main/scala/common/package.scala (limited to 'Scala/example/src/main/scala/common') diff --git a/Scala/example/src/main/scala/common/package.scala b/Scala/example/src/main/scala/common/package.scala new file mode 100644 index 0000000..f1c74c3 --- /dev/null +++ b/Scala/example/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 + } +} -- cgit v1.2.3