Groovy DSL (1) Command chains
2016-03-23 13:28
489 查看
Groovy可以方便的制作DSL,Command chain是支持它这么做的一个特性。
简单说,就是方法调用用约定好的规则省略括号和“ . ”等操作符,下面看例子
下面是一些实例:
下面例子是调用第三方包,或你自己实现的代码库来做DSL
简单说,就是方法调用用约定好的规则省略括号和“ . ”等操作符,下面看例子
// equivalent to: turn(left).then(right) turn left then right ** 这里省略了括号和点,一个方法间隔一个参数,当然,后面的方法是前面方法的返回值 ** // equivalent to: take(2.pills).of(chloroquinine).after(6.hours) take 2.pills of chloroquinine after 6.hours 这里规则是相同的,参数不同 // equivalent to: paint(wall).with(red, green).and(yellow) paint wall with red, green and yellow 这里展示多个参数的例子,即用逗号隔开 // with named parameters too // equivalent to: check(that: margarita).tastes(good) check that: margarita tastes good 参数为属性 // with closures as parameters // equivalent to: given({}).when({}).then({}) given { } when { } then { } 参数为闭包 // equivalent to: select(all).unique().from(names) select all unique() from names 没有参数的用空括号 // equivalent to: take(3).cookies // and also this: take(3).getCookies() take 3 cookies 如果一行共有奇数个词,则最后是属性调用
下面是一些实例:
show = { println it } square_root = { Math.sqrt(it) } def please(action) { [the: { what -> [of: { n -> action(what(n)) }] 创造DSL有很多方式,这里用map和闭包做的 }] } // equivalent to: please(show).the(square_root).of(100) please show the square_root of 100 // ==> 10.0
下面例子是调用第三方包,或你自己实现的代码库来做DSL
@Grab('com.google.guava:guava:r09') import com.google.common.base.* def result = Splitter.on(',').trimResults(CharMatcher.is('_' as char)).split("_a ,_b_ ,c__").iterator().toList() 调用了Google的guava库的splitter方法 @Grab('com.google.guava:guava:r09') import com.google.common.base.* def split(string) { [on: { sep -> [trimming: { trimChar -> Splitter.on(sep).trimResults(CharMatcher.is(trimChar as char)).split(string).iterator().toList() }] }] 继续用map和闭包做DSL } def result = Splitter.on(',').trimResults(CharMatcher.is('_' as char)).split("_a ,_b_ ,c__").iterator().toList() 正常调用 def result = split "_a ,_b_ ,c__" on ',' trimming '_\' 等价于这个
相关文章推荐
- pair 函数 (STL)
- 记一次foxmailQQ邮箱创建失败到解决方法
- 微软AI首席科学家:AlphaGo激发全球人工智能进入新时代
- 自动释放池工作原理,self = [super init]和定义属性时,什么时候用 assign、retain、copy 以及它们的之间的区别?
- 常用修图工具的一些使用技巧及问题解决方法——ai
- 菜鸟学算法之 POJ 3007 Organize Your Train part II
- leetcode 217. Contains Duplicate
- res\HTTPFilter.manifest : general error c1010070: Failed to load and parse the manifest. {_~
- retain/assign/strong/weak/copy/mutablecopy/autorelease区别
- hdoj A + B Again 2057 (16进制加法)
- your local repository contains non-ascii
- 迷信again
- 219. Contains Duplicate II
- 217. Contains Duplicate
- int main(int argc, char *argv[])的含义
- 成都某公司HP服务器双循环系统无法启动的RAID5数据恢复
- 217. Contains Duplicate
- 机器学习实战(三)——NaiveBayes朴素贝叶斯算法邮件分类
- Mybatis junit测试时报: parameter *** not found.available 错误处理
- 解决No such file or directory报错和command 'sudo' is avaible in '/usr/bin/sudo' the command could ..报错