List foo = ['a','bc'] assert (foo*.size() == foo.collect{it.size()}) // *. is basically a super terse syntax for .collect -- i.e. call exactly one operation on each 'it' // also, if you don't want to save the results, * is basically a super terse .each threads*.join()
Thursday, November 24, 2011
Spread-dot, a Terse Syntax for Collect or Each
Read Lines in Zip File's (File) Entries
//to read a file: import java.io.*; import java.util.*; import java.util.zip.*; String fname = "C:\\test.zip" ZipFile zf = new ZipFile(fname) zf.entries().each { entry -> if (!entry.directory) { String entryFileName = entry.name println entryFileName InputStream inp = zf.getInputStream(entryFileName); inp.eachLine { line -> println line } } } //to write a file: new AntBuilder().zip(destfile:"zipped.zip", basedir=".", excludes="lib .svn plugins *.class") //http://groovy-almanac.org/creating-a-zipfile-with-antbuilder/
Thursday, November 17, 2011
Class, @Delegate
class A { String doIt() {println 'do A'} } class B { String doIt() {println 'do B'} } def pc = { println ( (it == A ? '' : 'x: ') + it.getName()) } pc(A) pc(B) //fail pc(A1) //fail def a = new A() pc(a.getClass()) class A1 extends A { @Delegate B b = new B() @Delegate A a = new A() // red herring } class C { @Delegate B b = new B() @Delegate A a = new A() // red herring } def a1 = new A1() a1.doIt() // do A def c = new C() c.doIt() // do B
Monday, November 7, 2011
Command line input
def input = System.console().&readLine
def pwd = System.console().&readPassword
String username = input("username: ");
String password = pwd("password: ");
Wednesday, November 2, 2011
Subscribe to:
Posts (Atom)