Showing posts with label groovy. Show all posts
Showing posts with label groovy. Show all posts

2012-11-30

How to install Groovy on Windows?

Groovy is a great scripting language for the Java platform.

The first step to install it is to download the archive (ZIP).
http://groovy.codehaus.org/Download?nc

The second step is to unzip to a folder.
For example, C:\Java\groovy\2.0.5

Set GROOVY_HOME to the installation folder.

Then, edit the PATH variable, to add, in the beginning:
%GROOVY_HOME%\bin;

Finally, open a new console and execute the following command that should print the installed Groovy version:
groovy -version

2012-10-05

Groovy Date Format one-liner

new Date().format("yyyy-MM-dd")

This method has been added to java.util.Date




Current date (mm/dd/yyyy) (Groovy forum at JavaRanch)
:

import org.codehaus.groovy.runtime.*;

class Run{
static void main(def args){
def dateTime = new Date()
println DateGroovyMethods.format(dateTime, 'MM/dd/yyyy')
}
}

Groovy File One Liners

Groovy Scripting and Programming: 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

2012-10-04

Groovy one-liner to extract file name without extension

Does groovy have an easy way to get a filename without the extension? - Stack Overflow:

file.name.lastIndexOf('.').with {it != -1 ? file.name[0..<it] : file.name}

 or with a simple regexp:

file.name.replaceFirst(~/\.[^\.] $/, ''

2012-09-20

You code, you learn - Groovy to Java

Here is one mistake you might end up doing:

"I'll code in Groovy first and then convert to Java. It should be a piece of cake!"

Be prepared for a bitter and hard to chew cake!

Groovy is very loose on typing so you'll end up doing all sorts of accidental programming errors, for instance, you can easily use methods that are not accessible on a static context. Generics are also a pain.

To convert the code you will have to do major refactoring with redesign of the class hierarchies.

I wasted 2 weeks on a project when I had to convert a 200 class application from Groovy to Java.

You code, you learn!

2012-04-13

Java vs Groovy on overriden method call

In Java, suppose we have the following class:

public class Do {
    public void doSomething(Object arg) {
        System.out.println("Do " + arg.toString());
    }
}

We then extend it in the following class:

public class DoDifferently extends Do {
    public void doSomething(Object arg) {
        System.out.println("Do different " + arg.toString());
    }
    public void doSomething(String arg) {
        System.out.println("Do different string " + arg.toString());
    }
}

Now let's use these classes in our program:

Do o = new Do();
o.doSomething("x");

executes the Object method.

DoDifferently o2 = new DoDifferently();
o2.doSomething("x");

executes the String method because it is more specific.

What about the following code?

Do o3 = new DoDifferently();
o3.doSomething("x");

It will execute the Object method, because Java uses the reference type to determine which method to call.

What about Groovy?

The code above is also valid in Groovy.

The String method is always executed when available, because Groovy uses the most specific method for the argument type, determined dynamically at run-time.

2012-03-02

Groovy properties

Groovy generates getters and setters for the public fields of a class.
These default methods can be overridden as usual.
If a field is final, then Groovy generates only the getter method.

Groovy provides a special operator, called the field operator .@, that bypasses the generated accessor methods.

2012-03-01

Groovy DSL

One of the selling points for Groovy is writing Domain Specific Languages (DSL).
I have to admit that I am not fully convinced. There are many limitations to the syntax.
The basic idea is that Nouns in the DSL are implemented as Objects and Verbs in the DSL as Functions.

Someday I would like to read the following books on DSLs:
Martin Fowler, 2010
Debasish Ghosh, 2010

2012-02-29

Groovy

I have been using Groovy for almost 2 years now, and it has been a great experience.

I have a preference for typed languages, but there is no doubt that scripting languages have enormous utility too. Groovy is very flexible in this aspect. You can program using types - in this case Groovy is just Java with some nice syntax sugar - or you can program without types - in this case Groovy is a scripting language with an incredible set of libraries: anything that runs on the Java Virtual Machine.

I read that the Groovy team is considering adding more features to allow better performance in the type-based programs. This is expected for the 2.0 release.

Even Java is becoming easier to use with the Project Coin language changes.
By the way, Groovy 1.9 will include JDK 7 support including project coin language changes and invokeDynamic to improve performance.

2011-11-03

Groovy and Gradle

The next generation of Java and Ant (+Maven): Groovy and Gradle.



There are still some improvements required but these tools represent major steps forward in programmer productivity.

2010-10-11

Groovy regular expression operators

The Java regular expression (regex) typical invocation sequence is:

     Pattern p = Pattern.compile("a*b");
     Matcher m = p.matcher("aaaaab");
     boolean b = m.matches();

A matches method is defined by the Pattern class as a convenience for when a regular expression is used just once. This method compiles an expression and matches an input sequence against it in a single invocation. The statement

    boolean b = Pattern.matches("a*b", "aaaaab");

Groovy is a promising new language for the Java platform that includes the following regular expressions operators that greatly simplify the use of java.util.regex:

~ creates a Pattern from String

=~ creates a Matcher, and in a boolean context, it is "true" if it has at least one match, "false" otherwise.

==~ tests if String matches the pattern