On Github Armaklan / ks-groovy
Descendant direct de Java.
Proche de Java (facile à apprendre).
Prévu pour collaborer et communiquer avec Java.
Java, mais en mieux !
public class Pizza {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String what() {
return "Hello, you eat a " + name;
}
}
Pizza pizz = new Pizza();
pizza.setName("Cannibale");
System.out.println(pizza.what());
public class Pizza {
String name;
String what() {
"Hello, you eat a #{name}"
}
}
def pizz = new Pizza(name: "Cannibale")
println perso.what()
Objectif : prise en main rapide
Moins de verbosité => moins de temps perdues
Typage dynamique
Implication ? Moins de code, plus de facilité.
Lambda, Closure, DSL, ...
Inclut les dernières innovations
def oldThan18 = { it.age >= 18 }
def listOldUser = users.findAll(oldThan18);
take 15.min, of: pause, after: 1.hours
On peut tout faire en groovy, de A à Z
Ecosystème Java + Ecosystème groovy !
Une règle : faites ce qu'il faut pour que cela fonctionne. Un outil : débrouillez-vous ! Olivier Lockert
import groovy.json.JsonSlurper;
String fileContents = new File('visu/replay.js').text
fileContents.eachLine{ line ->
def result = new JsonSlurper().parseText(line)
def game_length = result.game_length
def state = "perdu"
if(result.rank[0] == 1) { state = "gagné" }
printDataLine("Players", result.playernames)
printDataLine("Result", result.rank)
printDataLine("Status", result.status)
println "Vous avez $state la partie en $game_length tour."
}
def printDataLine(def label, def data) {
resultLine = "$label : \t"
data.each{player -> resultLine += "| $player \t"}
println(resultLine)
}
apply plugin: 'eclipse'
apply plugin: 'groovy'
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.0.5'
testCompile 'junit:junit:4.11'
}
task('copy', type: Copy) {
from(file('srcDir'))
into(buildDir)
}
Utiliser une api java en groovy ? Facile !
Plus simple et rapide pour la prise en main...
Surtout sur de petit travaux..
Peut être interprété et non compilé.
class PlanetTest extends spock.lang.Specification {
def "Calcul distance between to planet"() {
given: "Initialize game"
def game = new Game("")
def first = Planet.parse(firstTokens, game.planets)
def second = Planet.parse(secondTokens, game.planets)
expect: "Check distance correctly calcul"
Game.distance(first, second) == result
where:
firstTokens | secondTokens | result
['E',14.607868701123053,16.142563056352653,0,0,2] | ['E',19.60609476210437,13.972250406328175,0,18,2] | 6.0
['E',14.607868701123053,16.142563056352653,0,0,2] | ['M',0.5495124796968334,10.88556823190577,3,14] | 16.0
['E',9.904971573452196,4.426803509261771,0,58,2] | ['E',19.60609476210437,13.972250406328175,0,18,2] | 14.0
}
}
Test Failure: Calcul distance between to planet(PlanetTest)
Condition not satisfied:
Game.distance(first, second) == result
| | | | |
6.0 | | | 5.0
| | false
| EconomicPlanet@5797c0
EconomicPlanet@d6c5da
at PlanetTest.Calcul distance between to planet(Test.groovy:29)
Swing, Servlet, ...