我为 scala 中的模式匹配编写了以下内容:
scala> val rowMap = Map("abc" -> null)
rowMap: scala.collection.immutable.Map[String,Null] = Map(abc -> null)
scala> val postgresKey = "abc"
postgresKey: String = abc
scala> val h2Key = "xyz"
h2Key: String = xyz
建议选项:
scala> (rowMap.get(postgresKey).map(_.asInstanceOf[String]) , rowMap.get(h2Key).map(_.asInstanceOf[String])) match
| {
| case (Some(value), _) if (value != null && value.trim.nonEmpty) => value
| case (None, Some(value)) if (value != null && value.trim.nonEmpty) => value
| case (_ , _) => "0.0"
| }
res9: String = 0.0
我的代码:
scala> (rowMap.get(postgresKey).map(_.asInstanceOf[String]) , rowMap.get(h2Key).map(_.asInstanceOf[String])) match
| {
| case (Some(value), _) if (value != null ) => if (value.trim.nonEmpty) value else "0.0"
| case (None, Some(value)) if (value != null ) => if (value.trim.nonEmpty) value else "0.0"
| case (_ , _) => "0.0"
| }
res10: String = 0.0
建议的选项是否比我的代码有任何性能优势。我知道它会在它看到 a 的那一刻&&
停止比较。因此,我们节省了 1 个比较。value.trim.nonEmpty
null
我确实明白阅读起来也更干净。在建议的方法中还有什么更好的方法吗?
编辑 1:我被告知要避免使用null
. 然而,它是来自 anorm's 的输入defaultParser
。由于该场景难以复制,因此我给出了上面的示例。就我而言,单元测试在 H2 中,实际数据库是 postgres。
以下是代码片段:
val anormQuery = SQL(query)
// map the anorm Row as per the input params, differentiate into aggregated and group cols
val tmp = anormQuery.as(anormQuery.defaultParser.*)
logger.info(" Printing the result set object " + tmp.toString())
val finalSqlResultset = tmp.map(row ⇒ {
// Anorm row.asMap has this behaviour that it adds either a leading dot(.) or <tablename>. in front of the map keys (the columns/alias in sql) based on whether it is H2 or Postgres
val rowMap = for ((k, v) ← row.asMap) yield (k, v)
logger.info("Print the resultSet as map : " + rowMap.toString())
val aggregates = expressionsToAggregate.map(input ⇒ {
val (anormKey, postgresKey) = doesColumnHaveAlias.get(input.alias.getOrElse("UnknownAlias")).getOrElse(("Unknown", "Unknown"))
//
val aggResult = AggregatedValue(
input.alias.get,
(rowMap.get(postgresKey).map(_.asInstanceOf[String]), rowMap.get(anormKey).map(_.asInstanceOf[String])) match {
case (Some(value), _) if (value != null && value.trim.nonEmpty) ⇒ value
case (None, Some(value)) if (value != null && value.trim.nonEmpty) ⇒ value
case (_, _) ⇒ "0.0"
})
logger.info("## TRACE 1 ##" + aggResult)
aggResult
})
})