Groovy の in キーワード
groovy.time パッケージを調べていたら知らない in の使い方をしていた。
http://groovy.codehaus.org/JN0545-Dates
System.setProperty('user.timezone', 'GMT') def c= new GregorianCalendar( 2002, Calendar.JUNE, 30 ) assert c.lenient c.set( Calendar.DATE, 31 ); c.time assert String.format('%tF %<ta', c) == '2002-07-01 Mon' c= new GregorianCalendar( 2002, Calendar.JUNE, 30 ) c.lenient= false c.set( Calendar.DATE, 31 ) try{ c.time; assert 0 }catch(e){ assert e in IllegalArgumentException }
e が IllegalArgumentException か判定しているように見える。
そんなことできたかなと "Groovy in keyword" で検索したら Hit した。
こちらは Collection に対して使用している。
Groovy in Action は載せるのを忘れていたらしい。
Groovy のサイトには載っていて isCase と同じ意味の演算子みたいだ。
http://docs.codehaus.org/display/GROOVY/JN2535-Control
class A{ boolean isCase(Object o){ if(o == 'A') return true else return false } } def a= new A() assert a.isCase('A') assert 'A' in a //more common, shortcut syntax for isCase() assert ! (a.isCase('Z')) assert ! ('Z' in a) //more common, shortcut syntax for isCase()
ちなみに、最初に調べていた groovy.time は経過時間を扱い二日前とか三日後と表現できる。*1
import groovy.time.* //reuse Extras category from a previous example... use( [Extras, groovy.time.TimeCategory] ){ def today= new Date(), tomorrow= today + 1, dayAfter= today + 2, nextWeek= today + 7 //days-only Date arithmetic assert ( today + 7.days ).toString() == nextWeek.toString() //use Date and duration together assert ( today.plus(7.days) ).toString() == ( today + 7.days ).toString() //alternative method name assert ( 7.days + today ).toString() == nextWeek.toString() //commutative assert ( nextWeek - 6.days ).toString() == tomorrow.toString() assert ( nextWeek.minus(6.days) ).toString() == tomorrow.toString() //alternative method name assert ( nextWeek - dayAfter ).toString() == 5.days.toString() //subtract two dates to get a duration //some handy operations... [2.days.ago, 3.days.from.now, 3.days.from.today].each{ assert it.class == java.sql.Date } }
追記 2011-04-06
http://dist.groovy.codehaus.org/distributions/ を調べてみた。
- in キーワードは 1.0 で既に存在する
- groovy.time パッケージは 1.1-BETA から存在する
『Groovy イン・アクション』の訳者後書きによると 『Groovy in Action』が出版されたときは 1.0 が最新バージョンだったらしい。
はてなブックマークの groovy タグを調べていたら in キーワードの使用例を発見した。
*1:これも載っていなかったような