今天看到有人在Groovy的邮件列表上问Groovy能不能支持Ruby的case ... when表达式:
car = "Patriot"
manufacturer = case car
when "Focus": "Ford"
when "Navigator": "Lincoln"
when "Camry": "Toyota"
when "Civic": "Honda"
when "Patriot": "Jeep"
when "Jetta": "VW"
when "Ceyene": "Porsche"
when "Outback": "Subaru"
when "520i": "BMW"
when "Tundra": "Nissan"
else "Unknown"
end
puts "The " + car + " is made by " + manufacturer
然后Guillaume给出了这么一段代码:
def car = "Patriot"
def manufacturer = match(car) {
when "Focus", "Ford"
when "Navigator", "Lincoln"
when "Camry", "Toyota"
when "Civic", "Honda"
when "Patriot", "Jeep"
when "Jetta", "VW"
when "Ceyene", "Porsche"
when "Outback", "Subaru"
when "520i", "BMW"
when "Tundra", "Nissan"
otherwise "Unknown"
}
println "The $car is made by $manufacturer"
def match(obj, closure) {
closure.subject = obj
closure.when = { value, result ->
if (value == subject)
throw new MatchResultException(result: result)
}
closure.otherwise = { return it }
closure.resolveStrategy = Closure.DELEGATE_FIRST
try {
closure()
closure.otherwise()
} catch (MatchResultException r) {
r.result
}
}
class MatchResultException extends RuntimeException {
def result
}
我不是很喜欢里面用异常来控制程序的流程,而且觉得“when "Focus", "Ford"”中间的逗号不够直观,因此就在上面的代码的基础上做了一些修改:
def match(subject, closure) {
def whenMap = [:], otherwise = null
closure.when = { map -> whenMap.putAll(map) }
closure.otherwise = { otherwise = it }
closure.resolveStrategy = Closure.DELEGATE_FIRST
closure()
def result = whenMap.find { condition, value -> subject in condition }
return result ? result.value : otherwise
}
def manufacturer(car) {
match(car) {
when "Focus": "Ford"
when "Navigator": "Lincoln"
when "Camry": "Toyota"
when "Civic": "Honda"
when "Patriot": "Jeep"
when "Jetta": "VW"
when "Ceyene": "Porsche"
when "Outback": "Subaru"
when "520i": "BMW"
when "Tundra": "Nissan"
otherwise "Unknown"
}
}
println "The Patriot is made by ${manufacturer('Patriot')}"
println "The QQ is made by ${manufacturer('QQ')}"
以上代码在Groovy 1.6下编译通过。
分享到:
相关推荐
Groovy 学习笔记 Groovy 学习笔记
Groovy.for.Domain.Specific.Languages.pdf
Packtpub.Groovy.for.Domain.Specific.Languages.Jun.2010.rar
Start building powerful apps that take advantage of the dynamic scripting capabilities of the Groovy language, including what's new in Groovy version 3.0. This book covers Groovy fundamentals, such as...
Groovy
Manning.Groovy.in.Action.Jan.2007.
groovy-all-2.4.15.jar文件,MAC使用时需存放在/Users/用户名/.gradle/caches/jars-3/某一缓存目录下,找不到就都看一下,我遇到的问题是缓存目录中下载的是2.4.17版本,应该跟gradle版本升级有关
Manning.Groovy.in.Action.Jan.2007.pdf
groovy programming(2006.12).pdf
Apress.Beginning.Groovy.and.Grails.From.Novice.to.Professional.2008英文原版
Groovy Recipes Greasing the Wheels of Java.pdf
Groovy in Action, Second Edition is the undisputed definitive reference on the Groovy language. Written by core members of the Groovy language team, this book presents Groovy like no other can—from ...
在学习任何东西之前,最重要的是培养兴趣,Groovy世界最耀眼的技术之一--Grails相信大家早已耳闻,我将通过Grails实战系列文章 向您展现Grails的迷人风采,使您感受到Grails的魅力,以至疯狂地爱上Grails,并坠入...
最近的groovy-all-2.4.12.jar包供使用,解决as编译时报错问题
在一次代码拉取中,出现了以下问题:Could not download groovy-all.jar (org.codehaus.groovy:groovy-all:2.4.15) 详细的报错信息如下: // 报错信息如下 Could not resolve all files for configuration ':jcore-...
Groovy学习笔记,内容如下: 1.概念 2.基本语法 3.字符串 4.集合 5.类和闭包 6.控制语句 7.操作符重载 8.I/O操作 9.高级应用 10.正则表达式
groovy-all-1.8.1版 ,可用。
Groovy-朝花夕拾-mht.rar。Groovy学习资料。Grails学习资料。mht网页格式。
Groovy in Action Groovy实战 英文第2版 Groovy in Action Groovy实战 英文第2版