Created
March 17, 2015 02:54
-
-
Save fmamud/b545bf452597b034aef5 to your computer and use it in GitHub Desktop.
01-groovy-welcome.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
________________ ______ | |
/ ____/_ __/ __ \ / ____/________ ____ _ ____ __ | |
/ /_ / / / / / / / / __/ ___/ __ \/ __ \ | / / / / / | |
/ __/ / / / /_/ / / /_/ / / / /_/ / /_/ / |/ / /_/ / | |
/_/ /_/ /_____/ \____/_/ \____/\____/|___/\__, / | |
/____/ | |
12/03/2015 | |
@ftmamud | |
https://friendstechday.wordpress.com/ | |
Welcome Groovy | |
*/ | |
// GroovyConsole | |
this | |
// No semicolons | |
print("Hello "); println("Java") | |
// numbers literals (G, L, I, D, F) | |
(0.1 + 0.2).class | |
12G.class | |
16.9G.class | |
0.1.class | |
0.1d.class | |
0.1d + 0.2d | |
// literals String, List, Maps, Range | |
'FTD Groovy' | |
"FTD Groovy" | |
/FTD Groovy/ | |
$/FTD Groovy/$ | |
// Java | |
List<String> lista = new ArrayList<String>(); | |
lista.add("FTD"); | |
lista.add("Java"); | |
println lista | |
// Groovy | |
List<Integer> lista = ["FTD", "Groovy", 2, true, 45G] | |
println lista | |
lista.class | |
for (v in lista) { | |
println v | |
} | |
def map = ["weslley": 26, faboy: 36, rodriguinho:24] | |
println map.getClass() | |
for (v in map) { | |
println v.key + " ---> " + v.value | |
} | |
def velocidadeMaxima = 0..100 | |
velocidadeMaxima.from | |
velocidadeMaxima.to | |
for (v in velocidadeMaxima) { | |
println v | |
} | |
for (v in 34..98) { | |
println v | |
} | |
// Def and type | |
def valor = "String" | |
println valor | |
valor = 13 | |
println valor | |
valor = true | |
println valor | |
// Classes as first-class citizens | |
println String.getName() | |
// Equals and == and is() | |
String a = new String("Joao") | |
String b = new String("Joao") | |
a == b | |
a.is(b) | |
// GStrings (interpolation, multiline) | |
// concatenation | |
String evento = "FTD" | |
String lang = "Groovy" | |
evento + lang | |
//interpolation | |
"$evento $lang".class | |
// The power of switch (All String types, Lists, Ranges, Classes, Closures) | |
def valor = "6" | |
switch (valor) { | |
case "300": println("Trinta"); break | |
case 24: println("Veado"); break | |
case String: println("Eu sei que egh uma String"); break; | |
case 10..20: println("10 ate 20"); break; | |
case [34, 10, 44]: println("Caiu no list"); break; | |
} | |
// Groovy Truth (zero, empty collections, empty maps, empty string, iterators, matchers) | |
def lista = [34] | |
if (!lista) println "Lista vazia" | |
lista = "" | |
if (!lista) println "String Vazia" | |
/* Operators */ | |
// Safe navigation operator: .? | |
class Cidade { | |
private String nome; | |
private Estado estado; | |
public Cidade(String nome, Estado estado) { | |
this.nome = nome; | |
this.estado = estado; | |
} | |
String getNome() { return this.nome; } | |
void setNome(String nome) { this.nome = nome; } | |
Estado getEstado() { return this.estado; } | |
} | |
class Estado { | |
private String nome; | |
public Estado(String nome) { | |
this.nome = nome; | |
} | |
String getNome() { return this.nome; } | |
void setNome(String nome) { this.nome = nome; } | |
} | |
Cidade sp = new Cidade("Sao Paulo", new Estado("SP")) | |
sp.getEstado().getNome(); | |
Cidade rj = new Cidade("Cidade Maravilhosa", null) | |
rj?.getEstado()?.getNome(); | |
// Elvis operator: ?: | |
Cidade saoCaetano = new Cidade("Sao Caetano", new Estado("SP")) | |
println saoCaetano != null ? saoCaetano : null | |
println saoCaetano ?: null | |
// Direct field access operator: .@ | |
// Method pointer operator: .& | |
// Pattern operator: ~ | |
// Find operator: =~ | |
// Match operator: ==~ | |
// Spread operator: *. | |
def lista = [1,2,3,4,5] | |
lista*.class | |
def resultado = [] | |
for (v in lista) { | |
resultado << v.class | |
} | |
resultado | |
// Spreading method arguments: *args | |
// Spread list elements: [a, *items, c] | |
// Spread map elements: [a:1, b:2, *:m1] | |
// Range operator 1..10, 'a'..'z' | |
for (l in 'a'..'z') println l | |
// Spaceship operator (compareTo) <=> | |
/* Subscript operator list[2], list[0..2] | |
(shortcut for getAt or putAt) | |
*/ | |
// Membership operator contains() in | |
// Identity operator ==, equals, is | |
// Coercion operator as | |
// Omitting parentheses | |
println("Hello") | |
println "Hello" | |
int soma(int a, int b) { | |
return a + b | |
} | |
soma 1, 1 | |
// Import aliasing | |
import java.math.BigDecimal as BD | |
BD n = new BD("0.3") | |
// Initializing beans with named parameters and the default constructor | |
class Pessoa { | |
private Integer id; | |
private String nome; | |
public String getNome() { return this.nome; } | |
public Integer getId() { return this.id; } | |
} | |
def w = new Pessoa(id: 1, nome: "Weslley") | |
// Using with() for repeated operations on the same bean - (Ex: Calendar) | |
Calendar hoje = Calendar.getInstance() | |
hoje.with { | |
clear() | |
set Calendar.DAY_OF_MONTH, 9 | |
set Calendar.MONTH, 2 | |
set Calendar.YEAR, 2015 | |
} | |
println hoje.get(Calendar.DAY_OF_MONTH) | |
import java.time.LocalDateTime | |
LocalDateTime.of 2015, 3, 12, 9, 18, 00 | |
// Catch any exception | |
try { | |
throw new Error() | |
} catch (e) { // Exception | |
println e.class | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment