From f131fb3108948af98285cbc508fdc514e41eafeb Mon Sep 17 00:00:00 2001 From: Mark Harrah Date: Fri, 5 Feb 2010 19:06:44 -0500 Subject: [PATCH] cleanup in launcher --- launch/ConfigurationParser.scala | 10 +++++----- launch/ListMap.scala | 2 +- launch/Pre.scala | 2 ++ launch/Using.scala | 2 +- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/launch/ConfigurationParser.scala b/launch/ConfigurationParser.scala index 20eb6895c..0f7d7ca9d 100644 --- a/launch/ConfigurationParser.scala +++ b/launch/ConfigurationParser.scala @@ -55,12 +55,12 @@ class ConfigurationParser extends NotNull def check(map: ListMap[String, _], label: String): Unit = if(map.isEmpty) () else error(map.keys.mkString("Invalid " + label + "(s): ", ",","")) def check[T](label: String, pair: (T, ListMap[String, _])): T = { check(pair._2, label); pair._1 } def id(map: LabelMap, name: String, default: String): (String, LabelMap) = - (getOrNone(map, name).getOrElse(default), map - name) - def getOrNone[K,V](map: ListMap[K,Option[V]], k: K) = map.get(k).getOrElse(None) + (orElse(getOrNone(map, name), default), map - name) + def getOrNone[K,V](map: ListMap[K,Option[V]], k: K) = orElse(map.get(k), None) def ids(map: LabelMap, name: String, default: List[String]) = { - val result = map(name).map(value => trim(value.split(",")).filter(isNonEmpty)).getOrElse(default) - (result, map - name) + val result = map(name).map(value => trim(value.split(",")).filter(isNonEmpty)) + (orElse(result, default), map - name) } def bool(map: LabelMap, name: String, default: Boolean): (Boolean, LabelMap) = { @@ -70,7 +70,7 @@ class ConfigurationParser extends NotNull def toFiles(paths: List[String]): List[File] = paths.map(toFile) def toFile(path: String): File = new File(path.replace('/', File.separatorChar))// if the path is relative, it will be resolved by Launch later def file(map: LabelMap, name: String, default: File): (File, LabelMap) = - (getOrNone(map, name).map(toFile).getOrElse(default), map - name) + (orElse(getOrNone(map, name).map(toFile), default), map - name) def getBoot(m: LabelMap): BootSetup = { diff --git a/launch/ListMap.scala b/launch/ListMap.scala index 726ef5b41..be83b152e 100644 --- a/launch/ListMap.scala +++ b/launch/ListMap.scala @@ -27,7 +27,7 @@ sealed class ListMap[K,V] private(backing: List[(K,V)]) extends Iterable[(K,V)] } object ListMap { - def apply[K,V](pairs: (K,V)*) = (empty[K,V] /: pairs)(_ + _) + def apply[K,V](pairs: (K,V)*) = new ListMap[K,V](pairs.toList.removeDuplicates) def empty[K,V] = new ListMap[K,V](Nil) private def remove[K,V](backing: List[(K,V)], k: K) = backing.filter(_._1 != k) } diff --git a/launch/Pre.scala b/launch/Pre.scala index 12c51edc1..e1b249976 100644 --- a/launch/Pre.scala +++ b/launch/Pre.scala @@ -38,4 +38,6 @@ object Pre n } def array(files: File*): Array[File] = toArray(files.toList) + /* Saves creating a closure for default if it has already been evaluated*/ + def orElse[T](opt: Option[T], default: T) = if(opt.isDefined) opt.get else default } diff --git a/launch/Using.scala b/launch/Using.scala index 0974c8032..4538ebb0c 100644 --- a/launch/Using.scala +++ b/launch/Using.scala @@ -4,7 +4,7 @@ import java.io.{Closeable, File, FileInputStream, FileOutputStream, InputStream, object Using extends NotNull { - def apply[R <: Closeable,T](create: => R)(f: R => T): T = withResource(create)(f) + def apply[R <: Closeable,T](create: R)(f: R => T): T = withResource(create)(f) def withResource[R <: Closeable,T](r: R)(f: R => T): T = try { f(r) } finally { r.close() } }