Julien Marcil
Source: TIOBE Programming Community Index for September 2013
Source: TIOBE Programming Community Index for September 2013
Source: http://langpop.corger.nl/
public class HelloWorld {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String greet() {
return "Hello " + name;
}
public static void main(String[] args) {
HelloWorld helloWorld = new HelloWorld();
helloWorld.setName("Groovy");
System.out.println( helloWorld.greet() );
}
}
public class HelloWorld {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String greet() {
return "Hello " + name;
}
public static void main(String[] args) {
HelloWorld helloWorld = new HelloWorld();
helloWorld.setName("Groovy");
System.out.println( helloWorld.greet() );
}
}
class HelloWorld {
def name
def greet() { "Hello ${name}" }
}
helloWorld = new HelloWorld(name: "Groovy")
println helloWorld.greet()
class HelloWorld < Struct.new(:name)
def greet
"Hello #{name}"
end
end
hello_world = HelloWorld.new("Ruby")
puts hello_world.greet
La syntaxe Groovy est riche et flexible. Voici quelque examples.
foxtype = 'quick'
foxcolor = ['b', 'r', 'o', 'w', 'n']
assert 'The quick brown fox' == "The $foxtype ${foxcolor.join()} fox"
|
|
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
Java |
|
---|---|
Groovy |
|
Java |
|
---|---|
Groovy |
|
Java |
|
---|---|
Groovy |
|
def v = 3
v = 'avion'
v = false
v = new StringBuffer()
v = null
def f = { list, value -> list << value }
x = []
f(x, 1)
f(x, 2)
f(x, 3)
assert x == [1, 2, 3]
def s = "Hello Groovy"
String.metaClass.shout = { delegate.toUpperCase() }
assert s.shout() == "HELLO GROOVY"
class SingletonCalculator {
private total = 0
def add(a) { total += a }
}
class CalculatorMetaClass extends MetaClassImpl {
private final static INSTANCE = new SingletonCalculator()
CalculatorMetaClass() { super(SingletonCalculator) }
def invokeConstructor(Object[] arguments) { return INSTANCE }
}
def registry = GroovySystem.metaClassRegistry
registry.setMetaClass(SingletonCalculator, new CalculatorMetaClass())
def calc = new SingletonCalculator()
class Author {
static hasMany = [books: Book]
String name
}
class Book {
static belongsTo = [author: Author]
String title
}
class Book {
static belongsTo = [author: Author]
String title
Date releaseDate
String ISBN
BigDecimal price
static constraints = {
title(blank: false, maxSize:100)
isbn(blank: false, matches:"[0-9]13" minSize:13 maxSize:13, unique: true)
price(nullable: true, min: 0.0, max: 9999.99, scale: 2)
}
static mapping = {
table "books"
price column: "sales_price"
}
def beforeInsert() { dateCreated = new Date() }
}
// Create
def a = new Author(name: "King")
a.save()
// Read
def a = Author.get(1);
// Update
def a = Author.get(1);
a.name = "Stephen King"
a.save()
// Delete
def a = Author.get(1);
a.delete()
Book.findByTitle("The Stand")
Book.findByTitleLike("Harry Pot%")
Book.findByReleaseDateBetween(firstDate, secondDate)
Book.findByReleaseDateGreaterThan(someDate)
Book.findByTitleLikeOrReleaseDateLessThan("%Something%", someDate)
def query = Person.where {
firstName == "Julien"
}
Person julien = query.find()
def children = Book.findAll { age in 0..17 }
def criteria = Account.withCriteria {
between("balance", 500, 1000)
eq("branch", "London")
or {
like("holderFirstName", "Fred%")
like("holderFirstName", "Barney%")
}
maxResults(10)
order("holderLastName", "desc")
}
class BookService {
static transactional = true
static scope = "singleton" //prototype, request, flash,
//conversation, session, singleton
def authorService // the AuthorService will be injected
def updateSomething() {
// Business Logic
}
@Transactional(readOnly = true)
def listBooks() {
Book.list()
}
}
class BookController {
def list() {
def map = [books: Book.list(params), total: Book.count()]
render(view: "books", model: map)
}
def show() {
[book: Book.get(params.id)]
}
}
class BookController {
def json()
{
render Book.list(params) as JSON
}
def xml()
{
render Book.list(params) as XML
}
}
<!DOCTYPE html>
<html>
<head>
<title>${book?.title}</title>
<r:require module="jquery"/>
<r:layoutResources/>
</head>
<body>
Title: ${book?.title}
Author: ${book?.author?.name}
<r:layoutResources/>
</body>
</html>
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/"(view:"/index")
"500"(view:'/error')
"404"(view:'/404')
}
}
Spring Security | LDAP | Mongo |
Quartz | Resources | Neo4J |
jQuery | Rabbit MQ | |
Joda | Zipped Resources | Redis |
Searchable | Console | ... |