Mar 21, 2013

GExcelAPI 0.3 Released!

GExcelAPI is a Groovy-Excel Wrapper introduced at the entry http://nobeans-en.blogspot.jp/2011/11/gexcelapi-for-groovyist-using-ms-excel.html.

Changelog of 0.3 is very tiny.
  • Supported Java 7
  • Upgraded POI to 3.9
You can get from the following repositories:
Sample of usage:
    @GrabResolver(name="my", root="https://repository-kobo.forge.cloudbees.com/release")
    @Grab("org.jggug.kobo:gexcelapi:0.3")
    import org.jggug.kobo.gexcelapi.GExcel

    def book = GExcel.open(args[0])

    assert book.sheets[0].A1.value == "Sheet1-A1"
The unit tests make the API look good:

Dec 7, 2012

Grails Improx Plugin 0.1, codenamed GrailsServ, released!


I'm very pleased to announce the release of improx plugin, which provides the way of using interactive mode from other process via TCP.

If you install this plugin to your application, you can invoke any Grails commands on an interactive mode, which started up in advance on a console, from other processes via a  TCP. This means that you can quickly run a test which is opened in you favorite editor or IDE. If you've appropriately set them up, you'd have only to push a key in order to run it.
(from Introduction of Improx Plugin User Guide)


In short, you can quickly run a single test which you're editing now, by one action. If you're frequently writing unit tests as TDD, this is very useful.


It's also easy to integrate with IDEs or editors, for example IntelliJ IDEA, Eclipse(GGTS/STS), Sublime Text 2, Vim, and so on.



To learn more about improx plugin, please read the user guide.


Please check it out and send me your feedback!

Nov 30, 2011

GExcelAPI for Groovyist using MS Excel

Do you love Excel?

If you want to automate operations which use Excel, you can use a famous library, Apache POI. But I can't say that it's easy enough, even if using from Groovy. Especially, a identification of a cell to use an index is too complicated.

If you want to read a value of a cell labeled "A1", you must write as follows:
File inputFile = ...
def book = new HSSFWorkbook(new POIFSFileSystem(new FileInputStream(inputFile)))
def sheet = book.getSheetAt(0) // 1st sheet
println "A1: "   sheet.getRow(0)?.getCell((short) 0)
println "A2: "   sheet.getRow(1)?.getCell((short) 0)
println "B1: "   sheet.getRow(0)?.getCell((short) 1)
...it's very difficult to read an write.

So I developed a wrapper library of Apache POI, called GExcelAPI. Version 0.2 was released at 2010-12-16. The name is very similar to "JExcelAPI" but there is no relationship. It's just a wrapper of "Apache POI".

By using GExcelAPI, you can rewrite the above sample:
File inputFile = ...
def book = new HSSFWorkbook(new POIFSFileSystem(new FileInputStream(inputFile)))
def sheet = book[0] // 1st sheet
println "A1: "   sheet.A1.value
println "A2: "   sheet.A2.value
println "B1: "   sheet.B1.value
You can directly use a label of a cell to specify it. It's intuitive and obvious.

GExcelAPI v0.2 has been released on my maven repository on Github. So you can use it via Grape.
@GrabResolver(name="kobo-maven-repo", root="https://github.com/kobo/maven-repo/raw/master/release")
@GrabConfig(systemClassLoader=true) // necessary if you invoke it by GroovyServ
@Grab("org.jggug.kobo:gexcelapi:0.2")
import org.jggug.kobo.gexcelapi.GExcel

def book = GExcel.open(args[0])
def sheet = book[0]
println sheet.A1.value

If you like GExcelAPI, check it out at the github.

To tell the truth, I'm not using Excel so much recently. So I hope that someone who are usually using GExcelAPI would become a committer of GExcelAPI ;-)


Oct 24, 2011

Invoking Groovy Script Directly on Vim using quickrun.vim and GroovyServ

There is an excellent vim plugin, quickrun.vim.

It makes your trial and error cycle much faster. It makes your script edited on vim invoke by shortcut keys. It's very useful for writing a script.

The plugin supports many programming languages including Groovy. But still the original Groovy command has the weak point; the starting-up of invocation is slow. Your "flow" or "zone" is interrupted by each invocation. But now, there is GroovyServ for you. It's very easy to configure your vim to use GroovyServ's groovyclient instead of original Groovy's command. Add the following lines at .vimrc:
let g:quickrun_config = {}
let g:quickrun_config.groovy = {'command' : 'groovyclient'}
Of course, GroovyServ must be installed and groovyclient must be added into PATH environment variables.

The default key bind to run a script is <Leader>r. If you want to modify it, add the following lines:
let g:quickrun_no_default_key_mappings = 1
nmap <Leader>r <Plug>(quickrun)
In my configuration, the <Leader> is a comma. So, when I input the keys of ",r", quickrun.vim works.

If you run a script by quickrun.vim first, a groovyserver's preparation might take several seconds. The starting-up messages aren't emitted to a result buffer, so you might think it's a freeze, but it's still working.
If you invoke groovyserver explicitly before you use quickrun.vim, the first invocation of quickrun.vim will be quite fast.

BTW, there are scripts which doesn't work well with GroovyServ. If your script is so, use original Groovy command on your terminal.

Enjoy your Groovy and Vim life!

Jul 29, 2011

High-speed start-up Jython/Clojure by GroovyServ

GroovyServ:
https://github.com/kobo/groovyserv

GroovyServ made a start-up time of the invocation of Groovy script very quick. However, it can be used not only for Groovy but for all JVM languages, e.g. Scala, Jython and Clojure, etc.

On Linux, when setting the following aliases, You can enjoy the power of GroovyServ for Jython:
alias gython="groovyclient -cp /tmp/jython.jar -e 'import org.python.util.jython; jython.main(args)' --"
$ time jython -c "print('Hello')"
Hello

real    0m2.503s
user    0m2.891s
sys     0m0.431s

$ time jython -c "print('Hello')"
Hello

real    0m2.613s
user    0m2.889s
sys     0m0.435s

$ time gython -c "print('Hello')"
Hello

real    0m0.959s
user    0m0.001s
sys     0m0.003s

$ time gython -c "print('Hello')"
Hello

real    0m1.156s
user    0m0.001s
sys     0m0.003s

Gython(Jython + GroovyServ) is faster by one second than normal Jython. In the above way, the overhead which groovyclient passes the classpath of the additional jython.jar to groovyserver at each invocation is so large. You can do it still faster by starting groovyserver with CLASSPATH environment variable which has jython.jar.

$ alias gython="groovyclient -e 'import org.python.util.jython; jython.main(args)' --"
$ export CLASSPATH=/tmp/jython.jar
$ groovyserver -r
$ time gython -c "print('Hello')"
Hello

real    0m0.909s
user    0m0.001s
sys     0m0.002s

$ time gython -c "print('Hello')"
Hello

real    0m0.076s
user    0m0.001s
sys     0m0.003s

$ time gython -c "print('Hello')"
Hello

real    0m0.045s
user    0m0.001s
sys     0m0.003s
Yeah!

Notice 1: I can't assure that a testing framework or a complex library works well. Do it by your self-responsibility ;-)

Notice 2: If your OS is Windows, use Groovy and GroovyServ which are installed from Zip archives. I experienced a trouble by using them of Windows-installer of version 1.8.0.

Examples in my .bashrc

alias glojureserver="env CLASSPATH=/usr/local/Cellar/clojure/1.2.0/clojure.jar groovyserver"
alias glojure="dgroovyclient -e 'import clojure.main;main.main(args)' --"
alias gythonserver="env CLASSPATH=/usr/local/Cellar/jython/2.5.2/libexec/jython.jar groovyserver"
alias gython="groovyclient -e 'import org.python.util.jython; jython.main(args)' --"
alias jvmserver="env CLASSPATH=/usr/local/Cellar/jython/2.5.2/libexec/jython.jar:/usr/local/Cellar/clojure/1.2.0/clojure.jar groovyserver"
Usage:
$ glojureserver -r -v
Groovy command path: /usr/local/bin/groovy (found at PATH)
GroovyServ home directory: /usr/local/Cellar/groovyserv/0.7/libexec
Original classpath: /usr/local/Cellar/clojure/1.2.0/clojure.jar
GroovyServ default classpath: /usr/local/Cellar/clojure/1.2.0/clojure.jar:/usr/local/Cellar/groovyserv/0.7/libexec/lib/*
Killed groovyserver of 56789(1961)
Restarting groovyserver
Starting....
groovyserver 59802(1961) is successfully started

$ time glojure -e "(println 'Hello)"
Hello

real    0m0.867s
user    0m0.001s
sys     0m0.004s

$ time glojure -e "(println 'Hello)"
Hello

real    0m0.053s
user    0m0.001s
sys     0m0.004s

$ gythonserver -r -v
Groovy command path: /usr/local/bin/groovy (found at PATH)
GroovyServ home directory: /usr/local/Cellar/groovyserv/0.7/libexec
Original classpath: /usr/local/Cellar/jython/2.5.2/libexec/jython.jar
GroovyServ default classpath: /usr/local/Cellar/jython/2.5.2/libexec/jython.jar:/usr/local/Cellar/groovyserv/0.7/libexec/lib/*
Killed groovyserver of 59802(1961)
Restarting groovyserver
Starting....
groovyserver 59938(1961) is successfully started

$ time gython -c "print('Hello')"
Hello

real    0m1.540s
user    0m0.001s
sys     0m0.004s

$ time gython -c "print('Hello')"
Hello

real    0m0.108s
user    0m0.001s
sys     0m0.003s

$ time gython -c "print('Hello')"
Hello

real    0m0.057s
user    0m0.001s
sys     0m0.004s

Dec 25, 2009

Sort by Multiple Keys in Groovy

kobo-commons@github

Problem (or limit) of the Groovy's original sort methods


You can sort collections, arrays, iterators and maps by a single key using only Groovy Core API, as follows:
assert [1, 2, 3, 4, 5] == [5, 3, 1, 4, 2].sort{ it }
It's very useful. I like this methods.

Now, when the following Person class and instances are given, it assumes that you want to sort some list of people ordered by lastName and familyName.
class Person {
    def lastName, familyName
}
def aa1 = new Person(lastName:'aa', familyName:1) // Are these strange names? Never mind!
def aa2 = new Person(lastName:'aa', familyName:2)
def b1  = new Person(lastName:'b',  familyName:1)
def b2  = new Person(lastName:'b',  familyName:2)
def c1  = new Person(lastName:'c',  familyName:1)
def c2  = new Person(lastName:'c',  familyName:2)
def c3  = new Person(lastName:'c',  familyName:3)

In the following sample, it seems to work as you expected.
def people = [b2, c2, c3, b1, c1]
assert [b1, b2, c1, c2, c3] == people.sort{ [ it.lastName, it.familyName ] }
                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                                          a list in closure has multiple keys
But it's just accidental.

See the following sample code. Do you think that Groovy’s original sort works as we expected?
// our expected result
def people = [c3, aa1, b2, c1, b1, aa2, c2]
assert [aa1, aa2, b1, b2, c1, c2, c3] == people.sort{ [ it.lastName, it.familyName ] }

The answer is NO. This code throws an AssertionException. Groovy’s sort method returns a list like the following:
// actual result.
def people = [c3, aa1, b2, c1, b1, aa2, c2]
assert [b1, b2, c1, c2, c3, aa1, aa2] == people.sort{ [ it.lastName, it.familyName ] }
                           ^^^^^^^^^^

The reason is as follows. The original sort logic is finally based on the value of Object#hashCode(), when the object doesn't implement a Comparable interface.
// groovy.util.OrderBy
public int compare(T object1, T object2) {
    for (Closure closure : closures) {
        Object value1 = closure.call(object1);
        Object value2 = closure.call(object2);
        if (value1 == value2) {
            continue;
        }
        if (value1 == null) {
            return -1;
        }
        if (value1 instanceof Comparable) {
            Comparable c1 = (Comparable) value1;
            int result = c1.compareTo(value2);
            if (result == 0) {
                continue;
            } else {
                return result;
            }
        }
        if (value1.equals(value2)) {
            continue;
        }
        return value1.hashCode() - value2.hashCode(); // hashCode!!!
    }
    return 0;
}
The value of "aa".hashCode() is larger than the value of "b".hashCode(), so you've gotten the above result.
This behavior confuses us very much.


Our Solution


We provide the sort methods which can sort rightly by multiple keys.

kobo-commons@github

Sample codes are as follows:
import org.jggug.kobo.commons.lang.CollectionUtils
CollectionUtils.extendMetaClass()
def people = [c3, aa1, b2, c1, b1, aa2, c2]
assert [aa1, aa2, b1, b2, c1, c2, c3] == people.sort([ { it.lastName }, { it.familyName } ])
assert [aa1, aa2, b1, b2, c1, c2, c3] == people.sort({ it.lastName }, { it.familyName })
assert [aa1, aa2, b1, b2, c1, c2, c3] == people.sort { it.lastName }, { it.familyName }
assert [aa1, aa2, b1, b2, c1, c2, c3] == people.sort { it.lastName } { it.familyName }

You can use the API as utility, too:
import org.jggug.kobo.commons.lang.CollectionUtils as CU
def people = [c3, aa1, b2, c1, b1, aa2, c2]
assert [aa1, aa2, b1, b2, c1, c2, c3] == CU.sort(people, [ { it.lastName }, { it.familyName } ])
assert [aa1, aa2, b1, b2, c1, c2, c3] == CU.sort(people, { it.lastName }, { it.familyName })

Note


We found that a useful API for this idea of sorting by multiple keys is already prepared as a constructor of groovy.util.OrderBy. But it isn’t used. Why?

Dec 15, 2009

Generating Proper equals() and hashCode() by @Equiv Annotations

We've released a small Groovy library named "kobo-commons" which has some convenience features for Groovy programming.


@Equiv annotation is one of the features.

If you annotate fields by @Equiv annotations, you can dynamically generate
proper equals() and hashCode() using specified fields.


If you wrote the following groovy code:
class Sample {
    @Equiv
    String name

    @Equiv
    String value

    String ignored
}

then you could use equals method and hashCode method:
def s1 = new Sample(name:'John', value:'Good', ignored:'Garbage')
def s2 = new Sample(name:'John', value:'Good', ignored:'Gold')
def s3 = new Sample(name:'John', value:'Bad',  ignored:'Garbage')
def s4 = new Sample(name:'Mike', value:'Good', ignored:'Garbage')
    
assert s1 == s1
assert s1 == s2
assert s1 != s3
assert s1 != s4
    
assert s1.hashCode() == s1.hashCode()
assert s1.hashCode() == s2.hashCode()
assert s1.hashCode() != s3.hashCode()
assert s1.hashCode() != s4.hashCode()


I think it's useful for entities, etc. You can keep your classes very simple.