2. Groovy at Glance
◦ Features
◦ Data Types
◦ Closures
◦ Advance Examples
Grails
Demo
Outline
3. ◦ Agile and Dynamic language
◦ JVM bounded – compiled to java byte code
◦ Increase productivity
Java developers - almost-zero learning curve
Simplifies testing
Reducing scaffolding code
◦ Current version 2.0
◦ performance issues
Groovy at Glance
Intoduction
4. Semicolons are optional. Use them if you like
(though you must use them to put several
statements on one line).
The return keyword is optional.
Methods and classes are public by default.
Inner classes use closures instead.
Groovy at Glance
Intoduction
5. The throws clause in a method signature is
not checked by the Groovy compiler, because
there is no difference between checked and
unchecked exceptions
Groovy supports regular expressions natively
Groovy at Glance
Intoduction
8. Closures
◦ a "code block" or a method pointer that is defined and then
executed at a later point
◦ Simple Examples
def clos = { println “Hey Groovy!" }
clos() //prints “Hey Groovy!“
def closure = { param -> param + 1 }
def answer = [1, 2].collect(closure)
assert answer == [2, 3]
Groovy at Glance
Intoduction
9. Methods returning
def geocode(String location)
{
// implementation
return [48.824068, 2.531733] //for Paris, France
}
def (lat, long) = geocode("Paris, France")
assert lat == 48.824068 assert long == 2.531733
Safe navigation
◦ def foo = null
◦ def bar = foo?.something?.myMethod()
Groovy at Glance
Hands on…
10. Regular Expressions support
Extracting Hashtags from twitter:
◦ def val=“Every time #Israel is attacked their land expands, but they have not yet
received all the land #God promised them http://tinyurl.com/2fr96y6”
◦ def hashTags = val.findAll("#[A-Za-z0-9_]+")
Groovy at Glance
Hands on…
11. Constructing Objects
Class Expando{
def name
def location
def id
}
def bean = new Expando(name:"James", location:"London", id:123)
Groovy at Glance
Hands on…
12. File IO
◦ Text
def fileContent = new File(fileName).text
◦ EachLine
Nik.txt
My name is Ayush.
But you can call me Nik.
I am fully devoted to Java and want to something extra for java
def number=new File ('Nik.txt').eachLine {
line -> number++
println "$number: $line"
Groovy at Glance
Hands on…
13. static def CAR_RECORDS = ''' <records>
<car name="HSV Maloo" make="Holden" year="2006">
<country>Australia</country>
<record type="speed">Production Pickup Truck with speed of
271kph</record>
</car>
<car name="P50" make="Peel" year="1962">
<country>Isle of Man</country>
<record type="size">Smallest Street-Legal Car at 99cm wide and 59 kg in
weight</record>
</car>
<car name="Royale" make="Bugatti" year="1931">
<country>France</country>
<record type="price">Most Valuable Car at $15 million</record>
</car>
</records> '''
Groovy at Glance
Hands on…
14. XML Parsing XmlSluper (Similar to JSON parsing
JSONSluper() )
def records = new
XmlSlurper().parseText(XmlExamples.CAR_RECORDS)
◦ All Elements:
def allRecords = records.car
◦ Counting children:
assert 3 == allRecords.size() def allNodes =
records.depthFirst().collect{ it }
Groovy at Glance
Hands on…
15. Groovy at Glance
◦ Features
◦ Data Types
◦ Closures
◦ Advance Examples
Grails
Demo
Outline
16. Grails
◦ Open source web application platform
◦ Convention over Configuration
Any grails application looks the same
No need for XML mapping files
◦ Powerful MVC framework
Power by Spring and Hibernate
Model / Domain Layer
View Layer (GSP)
Controller
Introduction Grails