[add][mod] added getKey and getKeyOrElse for Map[String, Any] in mapUtil

- [mod] use getKey and getKeyOrElse from mapUtil in FunctionArgs
This commit is contained in:
2020-01-29 01:19:11 -06:00
parent c7013eddd4
commit 4e5d9570ad
2 changed files with 16 additions and 5 deletions

View File

@@ -3,6 +3,7 @@ package scim.datastruct
import scim.lib.ConcurrentBroadcast._
import scim.lib.simInterfaces._
import scim.lib.configInterfaces._
import scim.lib.mapUtil._
import scala.collection.parallel.ForkJoinTaskSupport
import scala.util.control.NonFatal
@@ -125,11 +126,10 @@ case class Group(name: String, sensorParent: Option[String] = None){
case class Truth() extends Result[Option[Double]]
case class FunctionArgs(args: Map[String, Any]) {
def getParam[A](param: String): Option[A] = {
// just try to get the param, otherwise use fallback
try { Some(args(param).asInstanceOf[A]) } catch { case NonFatal(_) => None }
args.getKey[A](param)
}
def getParam[A](param: String, fallback: A): A = {
getParam(param).getOrElse(fallback)
args.getKeyOrElse[A](param, fallback)
}
}

View File

@@ -6,6 +6,7 @@ import java.io.{File, FileInputStream}
import scim.datastruct.FunctionArgs
import scala.collection.immutable.ListMap
import scala.util.control.NonFatal
@@ -117,6 +118,16 @@ package object convertUtil {
package object mapUtil {
implicit class MapGet(map: Map[String, Any]) {
def getKey[A](key: String): Option[A] = {
// just try to get the param, otherwise use fallback
try { Some(map(key).asInstanceOf[A]) } catch { case NonFatal(_) => None }
}
def getKeyOrElse[A](key: String, fallback: A): A = {
getKey[A](key).getOrElse(fallback)
}
}
def traversableToMap[A, B](traversable: Traversable[Any], getTuple: (Any) => (A, B) = (e: Any) => e.asInstanceOf[(A, B)]._1 -> e.asInstanceOf[(A, B)]._2): Map[A, B] = {
// Traversable[Tuple[A, B]] can be converted via .map
// .foldLeft used in favor of .map().toMap as the latter needs two traversals