我正在尝试在scala中转换此代码http://uadetector.sourceforge.net/usage.html#improve_performance,但却变得非常糟糕
object CachedUserAgentStringParser extends UserAgentStringParser {
private val parser = UADetectorServiceFactory.getCachingAndUpdatingParser
private val cache = CacheBuilder.newBuilder().maximumSize(100).expireAfterWrite(2, TimeUnit.HOURS)
.build()
override def getDataVersion(): String = parser.getDataVersion
override def parse(userAgentString: String): ReadableUserAgent = {
var result = cache.getIfPresent(userAgentString)
if (result == null) {
result = parser.parse(userAgentString)
cache.put(userAgentString, result)
}
result
}
override def shutdown() {
parser.shutdown()
}
}
获取错误类型
mismatch; found : net.sf.uadetector.ReadableUserAgent required: Nothing
result = parser.parse(userAgentString)
^
cache.put(userAgentString, result)
^
答案 0 :(得分:4)
您需要为cache
变量指定类型参数。
private val cache: Cache[String, ReadableUserAgent] =
CacheBuilder.newBuilder().maximumSize(100).expireAfterWrite(2, TimeUnit.HOURS).build()
以下是一个可编辑的示例:https://gist.github.com/tkawachi/2b68ca16d1b317c1d1dd