Sunday, October 7, 2012

Learning Linux Commands

I always had my development environment on Windows operating system so I never got a good hands-on on unix commands. This becomes a handicap when you have to troubleshoot a production issue once in a while as most of the time production environments are on Linux.

I have always been on the lookout for the ways to get a unix emulator or something similar on Windows. Earlier I tried Cygwin which is a good tool to get Linux look and feel environment on Windows. The only issue with this is that you have to install a large number of tools. On your workstations it may not be possible because of the restrictions imposed by employers.

Recently I stumbled upon another cool site called linuxzoo. I wish I had known about this site earlier. It allows you to have a remote private Linux machine with root access. This machine will be under your complete control, and you have to start it and stop it just like any normal machine!


So, If you are interested in learning Unix/Linux commands and you don't have access to Linux box get yourself registerd on http://linuxzoo.net.   Happy Learning!!

Tuesday, October 2, 2012

Say hello to Groovy - Part 3 (Collections)


In my previous blog i talked about various operators and control structures available in Groovy. Today i am going to talk about Groovy collections. Like Java, we have List and Map in Groovy. Groovy also has something called Range.

 
Lists

List is an ordered collection of objects. Groovy lists are indexed using [] operator. We can use [] to define an empty list and << to add items to a list.

Following code snippet shows how a list is defined in Groovy.

List Methods

Lists support many useful methods. Following code snippet illustrate the usage of some of the list methods.


Maps
Map is a collection of key value pair. We can use [:] to define an empty map. If the keys in a Map is String, it can act as a bean.
Following code snippet shows how to define and use Maps in Groovy.


Map Methods 
Maps support many useful methods. Following code snippet illustrate the usage of some of the Map methods.


Range

Range can be used to specify a list of sequential values. Range is defined using the first and last value of the sequence. Range can also be defined to exclude the last value. Use .. operator to include first and last value. Use ..< operator to exclude last value.

Ranges can be used for any Java object which implements java.lang.Comparable for comparison and also have methods next () and previous() to return the next / previous item in the range.

follwoing code snippet shows how to define and use Range in Groovy.

Range Methods

Like Lists and Maps, Range also support many useful methods. Following code snippet illustrate the usage of some of the Range methods.










Friday, September 7, 2012

Say hello to Groovy – Part 2 (Operators & Control Structures)

In my last blog, I gave an introduction to Groovy. We looked at the typical hello world program in Java as well as Groovy to understand how some of the additional features provided by Groovy helps to cut down on a lot of boilerplate code that you would have otherwise written in Java. In this blog, I will concentrate on some of the operators and control structures which are unique to Groovy.

Groovy Truth

Like Java, Groovy supports standard conditional operators in a Boolean expression. In addition to this, any type can be evaluated as a Boolean expression in Groovy. This is possible because Groovy has rules to convert a non-boolean objects to a Boolean value. E.g. In case of string, non-empty strings evaluate to true


Following are some of the rules that you should know: 
1. Empty collection evaluates to false. 
2. Non-zero numbers evaluates to true. 
3. A non-null object reference evaluates to true. 
4. Non-empty Map evaluates to true. 
5. Iterators/enumerators evaluates to false if it has no further elements. 

Operator Overloading

In Java we use the == operator to see if the variables are referring to the same object instance. Groovy overrides == operator on types such as String, Integer, List etc., to check for content equality rather than object equality. To test if two variables are referring to the same object instance in Groovy we use the is () method.





Elvis Operator (?:)


The "Elvis operator" is similar to Java's ternary operator. It can be used for returning a 'sensible default' value if an expression resolves to false or null. E.g. In below snippet, if username is null or empty, “Anonymous” would get printed. 


Safe Navigation Operator (?.)


In Java, it’s a common practice to check for null, before invoking a method on the object to avoid NullPointerException. In Groovy, this is achieved in lesser lines of code using the Safe Navigation operator. The safe navigation operator will simply return null instead of throwing an exception, e.g: In the below code snippet, null would get printed and no NPE will be thrown.



Looping

In addition to all looping constructs available in Java, Groovy provides some extra ways to loop and execute a piece of code. Groovy extends the Integer class with the step (), upto () and times () methods. If we have a List in Groovy we can loop through the items on the list with the each () and eachWithIndex () methods. We will revisit this with examples when I cover closure and collections.


Exception Handling

Groovy doesn’t force you to handle exceptions within the method signature not even for checking exceptions. You are free to choose how and when you handle the exception. The exception is passed on to the calling code until it is handled. E.g. the following is a valid code in Groovy however if you were to write the same code in Java, the compiler would have forced you to handle java.net.MalformedURLException.