我正试图找出一种从Scala调用Java API的方法.基本上,有一个ContentValues是有几个类似的方法对象getAsString,getAsLong每个都具有各自不同的返回类型.
我可以换ContentValues入另一个对象,以便我可以添加一个根据T get[T]调用正确getAsXXX方法的方法吗?
我尝试了什么(没有用,抱怨含糊不清的隐式解决方案):
object SContentValuesConversions {
case class SContentGetter[ScalaType](val check: String => Boolean, val getter: String => ScalaType) {
def getTyped(key: String): Option[ScalaType] = {
if (!check(key)) None
else Some(getter(key))
}
}
implicit def SIntContentValues(cv: ContentValues) =
SContentGetter((cv containsKey _), (cv getAsInteger _))
implicit def SLongContentValues(cv: ContentValues) =
SContentGetter((cv containsKey _), (cv getAsLong _))
implicit def SStringContentValues(cv: ContentValues) =
SContentGetter((cv containsKey _), (cv getAsString _))
}
Run Code Online (Sandbox Code Playgroud)
您可以使用与CanBuildFrom集合特征相同的技术.
我们首先创建一个Getter案例类
case class Getter[T](getter: (ContentValues, String) => T) {
def getOpt(contentValues: ContentValues, key: String): Option[T] =
if (contentValues containsKey key) Some(getter(contentValues, key))
else None
}
Run Code Online (Sandbox Code Playgroud)
这允许我们创建一个ContentValues具有所需方法的包装器.
implicit class ContentValuesWrapper(val c: ContentValues) extends AnyVal {
def getAsOpt[T](key: String)(implicit getter: Getter[T]) =
getter.getOpt(c, key)
}
Run Code Online (Sandbox Code Playgroud)
现在,为了调用getAsOpt方法,ContentValues我们需要为正确的类型提供implicit实例Getter.
object Getter {
implicit val intGetter = Getter(_ getAsInteger _)
implicit val longGetter = Getter(_ getAsLong _)
implicit val stringGetter = Getter(_ getAsString _)
}
Run Code Online (Sandbox Code Playgroud)
现在,您可以getAsOpt在ContentValues实例上使用该方法.
// fake version of ContentValues
val c =
new ContentValues {
val m = Map("a" -> "1", "b" -> "2", "c" -> "3")
def getAsInteger(k: String): Int = getAsString(k).toInt
def getAsLong(k: String): Long = getAsString(k).toLong
def getAsString(k: String): String = m(k)
def containsKey(k: String): Boolean = m contains k
}
c.getAsOpt[Int]("a") //Option[Int] = Some(1)
c.getAsOpt[Long]("b") //Option[Long] = Some(2)
c.getAsOpt[String]("c") //Option[String] = Some(3)
c.getAsOpt[Int]("d") //Option[Int] = None
c.getAsOpt[Long]("e") //Option[Long] = None
c.getAsOpt[String]("f") //Option[String] = None
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
543 次 |
| 最近记录: |