我有以下代码来进行正则表达式模式匹配。
class UserAgent(val ua: String) {
val iOS = """.*(iPad|iPhone|iPod).*OS ([0-9_]+).*""".r
val Android = """.*Android ([0-9.]+).*""".r
val WindowsPhone = """.*Windows Phone OS.*""".r
val WebOS = """.*webOS.*""".r
val BlackBerry = """.*BlackBerry.*""".r
val WAPBrowser = """.*(MIDP|UP\.Browser|Obigo|Polaris|BREW|Brew|NetFront).*""".r
var platform = "UnknownPlatform"
var platformVersion = "UnknownVersion"
ua match {
case iOS(_, version) => {
platform = "IOS"
platformVersion = version.replace("_", ".")
}
case Android(version) => {
platform = "Android"
platformVersion = version
}
case WindowsPhone() => platform = "WindowsPhone"
case WebOS() => platform = "WebOS"
case BlackBerry() => platform = "BlackBerry"
case WAPBrowser(p) => platform = "WAPBrowser"
}
}
可以使用(简化的)代码进行测试:
val tests = for (
str <- List("Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7",
"Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
"bla bla Windows Phone OS bla bla",
"bla bla webOS bla bla",
"bla bla BlackBerry bla bla",
"LG-LX600 Polaris/6.0 MMP/2.0 Profile/MIDP-2.1 Configuration/CLDC-1.1")
) yield new UserAgent(str)
tests foreach { ua => println(ua.platform + " " + ua.platformVersion) }
这不是非常 scala-ish,因为我的类UserAgent
有可变变量platform
和platformVersion
. 我想要实现的是具有不变性,因此要有类似 a 的东西trait
,它定义platform
和platformVersion
子类,它们将返回不同的值。我缺少的是如何创建一个构造函数来实例化并返回这样的子类。