Wednesday, December 14, 2011

Various index techniques

enum Coin {
    penny(1), nickel(5), dime(10), quarter(25)
    private final int value
    Coin(int value) { this.value = value }
    public int value() { return value }
}
assert 'penny' == Coin.penny.toString()
assert 1 == Coin.penny.value

Map Coin2 = [penny:1, nickel:5, dime:10, quarter:25]
assert 1 == Coin2.penny

List Coin3 = ['penny', 'nickel']
assert 1 == Coin3.indexOf('nickel')

Monday, December 5, 2011

Gretty

@GrabResolver(name='gretty', 
  root='http://groovypp.artifactoryonline.com/groovypp/libs-releases-local')
@Grab('org.mbte.groovypp:gretty:0.4.302') 
import org.mbte.gretty.httpserver.* 

GrettyServer server = []
server.groovy = [ 
    localAddress: new InetSocketAddress("localhost", 8080), 
    defaultHandler: { 
        response.redirect "/" 
    }, 
    "/:name": {
        get {
            response.text = "Hello ${request.parameters['name']}"
        } 
    } 
] 
server.start()

//see: https://github.com/groovypp/gretty/wiki/Getting-Started

see: Gretty, article

CSV file parser I/O

@Grab(group = 'net.sf.opencsv', module = 'opencsv', version = '2.3')
import au.com.bytecode.opencsv.CSVReader
import au.com.bytecode.opencsv.CSVParser
import au.com.bytecode.opencsv.CSVWriter

String TEST_FILE_NAME = 'C:\\test.csv'
String TEST_OUTPUT_FILE_NAME = 'C:\\testOut.csv'

List< String[] > rows = new CSVReader(
    new FileReader(new File(TEST_FILE_NAME)), 
    CSVParser.DEFAULT_SEPARATOR, 
    CSVParser.DEFAULT_ESCAPE_CHARACTER, 
    CSVParser.DEFAULT_QUOTE_CHARACTER, 1
).readAll()

List< String[] > rowsOver100 = rows.findAll { it[1].toInteger() > 100 }

File output = new File(TEST_OUTPUT_FILE_NAME)
if (output.exists()) {
    output.delete()
}

output.createNewFile()
output.withWriter { writer ->
    new CSVWriter(writer).writeAll(rowsOver100)
}

// for streaming, see also: http://www.groovy-tutorial.org/basic-csv/

Thursday, November 24, 2011

Spread-dot, a Terse Syntax for Collect or Each

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()

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: ");