Sunday, October 10, 2010

Meta Programming

I went to the NoFluffJustStuff symposium a few weeks ago in Atlanta. (This is a great conference for JVM programmers.) One of the things I learned about was meta programming (programming the programming language, so to speak), since I moved from Java to Groovy, I never really even thought stuff like this was possible.

One of the things that you can do in groovy is add methods to classes (that you don't have source code for) without inheriting the original class, check this example out, its really cool!!

String.metaClass.getCity<<{ return delegate.split(',')[0]}
String.metaClass.getState<<{ return delegate.split(',')[1].trim().split(' ')[0] }
String.metaClass.getZip<<{ return delegate.split(',')[1].trim().split(' ')[1] }

cszLine="Cumming, GA 30040"
println "Input: $cszLine"
println "City: ${cszLine.getCity()}"
println "State: ${cszLine.getState()}"
println "Zip: ${cszLine.getZip()}"
println ""
cszLine="St. Croix, USVI 00820"
println "Input: $cszLine"
println "City: ${cszLine.getCity()}"
println "State: ${cszLine.getState()}"
println "Zip: ${cszLine.getZip()}"

Input: Cumming, GA 30040
City: Cumming
State: GA
Zip: 30040

Input: St. Croix, USVI 00820
City: St. Croix
State: USVI
Zip: 00820

So what I did was add 3 methods to String class. getCity(), getState(), and getZip(). Now whenever I have  the City/State/Zip formatted string I can parse out t\eeach element.  This is much cleaner from a programming standpoint than creating and calling functions. Of course, the data has to be formatted correctly to work without throwing an exception.  (By the way delegate is how you reference the class your adding methods to.)

This just seems so cool to me, and you can do this for any class. If you have to do a particular calculation on an integer, you could create a method on Integer to do so. if you want dates formatted a special way you could add a method to the Date class, the possibilities are endless.

Saturday, May 1, 2010

Getting Groovy gStrings to work with sql queries

If you are building dynamic SQL queries with gstrings, you may have had issues embedding SQL date strings in them, the way I solved this was to explicitly define the query as a string:

date="{ts '2010-05-01 00:00:00'}"
String query=""" 
   order_date >${date}
sql.eachLine(query),{println it })

Monday, March 15, 2010

The Ups and the Downs of the Groovy Console

Like most scripting languages, Groovy has a programming editor -- the Groovy Console . Groovy's editor is home grown, (and not SciTE). There are, however,a few things you need to be aware of with the Groovy Console.
  1. It does not save when you run the program (so get used to saving before each run) 
  2. I've yet to get the interrupt button to work while its running, so I've had to kill the process (thus issue 1 above).
  3. If you are working on a program (and have not saved it recently) and go to open another program (file-->open) you're not prompted to save the first file before its replaced. So save often.
I'm willing to bet that as Groovy matures, that the these issues will get resolved.

Sunday, March 7, 2010

Groovy File One Liners

One of the nice things about Groovy if you are a Java programmer is the ease that you can manipulate files with just one like of code.

Create or replace a file  and write a line of text to it:

   new File("myfile.txt").write("Here is a line: \"whats a nice programmer like you doing at a blog like this\" \n");

Append to a file (creates the if it doesn't exist)

  new File("myfile.txt").append("\"Looking for a some code like this\"\n");

Read the second line (First line is line 0) of the file

  println new File("myfile.txt").readLines()[1] //what was the response

Saturday, March 6, 2010

Argh..Oracle has taken over the Java page

While I was adding links to this page I realized that sometime in the last month, Oracle took over the page. I can't find anything. Oh well, lets hope the change is good in the long run.

println("Hello World")

think that its about programming in Groovy. But It may just be groovy. (bad pun)

What is Groovy anyway?

Groovy is a programming language based on java. Its what happens when you take mix Perl and Java, and sprinkle on some Ruby.

Groovy is weakly typed, unlike java, but shares most of java's syntax (as well as the java virtual machine), but guess what -- you don't need semicolons at the end of the line. You can use pretty much any java class and syntax you want, and that means that any java programmer can learn it quite quickly. Once you get the hang of it you can pound out code pretty quick. (BTW, Groovy is the core language of the Grails web framework.) Groovy comes with a simple editor (the groovy console) , as well as a shell for scripting fun, but for heavy duty programming fun its supported by Eclipse, and NetBeans (which I use) .

You can find plenty more Groovy propaganda (and doc) at the Groovy Hompage

The current version of Groovy can be downloaded for Linux and Windows ( No current OSx support -- whats up with that? I use all three OS's)