Sunday, April 19, 2009

Groovier collections

I have been groovying almost over a year now.There are many remarkable things that groovy do in a very simple and intuitive manner .If you haven't start using it, you are already late.Today , i am going to discuss how groovy takes using collection to another level.Currently, i use it for routine scripts and code generation.
Java Collections framework is one of the most used SDK API (esp. in enterprise applications).This Api allows you do handle ,manipulate and represent the Collection of any Java objects.This greatly reduces programming effort where as boosting programming performance and fostering reuse.
Groovy adds to this many useful operators and tweaks to make working with collection api more entertaining experience.
Following is the ways you create a List in groovy:


L=[0,1,2,3]
println L.class //prints class java.util.ArrayList

This showing that ArrayList is used by groovy behind the scenes to create a List representation.

To work with LinkedList do this

L = new LinkedList([0,1,2,3]);
println L.class


To access, any element from the List , you can use array-like notation familiar to most of Java developers.Other ways to retrieve the elements are get and getAt methods expecting the index (-ve index allowed).

println L[2] //prints 2
println L[-3] //prints 1
println L.get(0) //prints 0
println L.getAt(1) //prints 1

The most frequent operation done on a list is to add elements to it.This can be acheived using +=,<< or using add method.To merge 2 lists use, addAll or + operator.

L = [0,1,2,3]
L += 4
L << 5
L.add(6)
println L //prints [0,1,2,3,4,5,6]
L.addAll(["a","b"])
println L // prints [0,1,2,3,4,5,6,a,b]
K = L + [99,100]
println K // prints [0,1,2,3,4,5,6,a,b.99.100]


Removing an element is equally easy.Use -,-= or call minus.remove() method of list is too supported.

L = [0,1,2,3]
L -= [3]
println L //prints [0,1,2]
L = L.minus(2)
println L //prints [0,1]
L.remove(0)
println L //prints [1]


Reversing the content of the list can be done as

L = [0,1,2,3]
println L.reverse() //prints [3,2,1,0]


Using map is easier than ever, the content left to the : is the key and to the right is the value.

M= ['a':'A','b':'B','c':'C']
println M // prints [a:A,b:B,c:C]
println M.getClass() //prints java.util.LinkedHashMap.We cannot use .class as . is reserved to access the key-values
println new HashMap(['a':'A','b':'B','c':'C']) // prints [a:A,b:B,c:C]


Retreiving value can be done numerous ways.

M= ['a':'A','b':'B','c':'C']
println M.a // prints A
println M['a'] // prints A
println M.get('c') // prints C
println M.getAt('b') // prints B
println M.getAt('x') // prints null as no key is matched.


Adding new key-value pairs too has various alternative.

M= ['a':'A','b':'B','c':'C']
M['d'] = 'D'
println M // prints ['a':'A','b':'B','c':'C','d':'D']
M.put('e','E')
println M // prints ['a':'A','b':'B','c':'C','d':'D','e':'E']
M.putAt('f','F')
println M // prints ['a':'A','b':'B','c':'C','d':'D','e':'E','f':'F']

Remove a pair with remove method paasing the Key.

M= ['a':'A','b':'B','c':'C']
M.remove('b') = 'D'
println M // prints ['a':'A','c':'C']


i'll cover more groovy stuff in upcoming posts.
till then...happy Groovying ;)

P.S : Syntax high lighting help from here.

Read more...

Saturday, April 04, 2009

Freemind on Ubuntu

Today i installed JDK6u13 and wanted to try out Freemind , one of the best free mind mapping tools.As i am still very new to mind mapping but a seasoned Java developer , i thought that i will start rolling soon with this tool.But , it turned otherwise.

After unzipping the freemind (binaries for any operating system - max - all inclusive) version to /opt,i chmodED it as executable.Now ,i tried to start it with the shell script file provided and what i got was

Exception in thread "main" java.awt.AWTError: Cannot load AWT toolkit: gnu.java.awt.peer.gtk.GtkToolkit
at java.awt.Toolkit.getDefaultToolkit(libgcj.so.70)
at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(libgcj.so.70)
at java.awt.Window.(libgcj.so.70)
at java.awt.Frame.(libgcj.so.70)
at javax.swing.JFrame.(libgcj.so.70)
at freemind.main.FreeMind.(FreeMind.java:107)
at freemind.main.FreeMind.main(FreeMind.java:647)
Caused by: java.lang.UnsatisfiedLinkError: libgtkpeer: libgtkpeer.so: cannot open shared object file: No such file or directory
at java.lang.Runtime._load(libgcj.so.70)
at java.lang.Runtime.loadLibrary(libgcj.so.70)
at java.lang.System.loadLibrary(libgcj.so.70)
at gnu.java.awt.peer.gtk.GtkToolkit.(libgcj.so.70)
at java.lang.Class.initializeClass(libgcj.so.70)
at java.lang.Class.forName(libgcj.so.70)
at java.awt.Toolkit.getDefaultToolkit(libgcj.so.70)
...6 more


Even though , i installed JDK6u13 and had it as my default java executable after updating the .profile file, then to some how this script was picking up the java from gcj package.

so i started to dig and find out is this happening.Sooner using sudo update-java-alternatives -l i was able to find out that i only had gcj installed and registerd in ubuntu.

X@X-desktop:/opt/Freemind$ sudo update-java-alternatives -l
java-gcj 1041 /usr/lib/jvm/java-gcj


Now, i was in search of a resource which will show me how to get java from JDK6u13 to be my default.After a quick google , i found this.So next set i ran

X@X-desktop:/opt/Freemind$ sudo update-alternatives --install "/usr/bin/java" "java" "/opt/jdk1.6.0_13/bin/java" 1
X@X-desktop:/opt/Freemind$ sudo update-alternatives --set java /opt/jdk1.6.0_13/bin/java

And then started the shell script..VOILA..it started










Drop me a cooment if this helps you , or you have good mind mapping sites/online book urls.

Read more...

Sociofluid