This document is a useful summary for people who don't know the Groovy language.
We list here most of the Groovy code that we use inside the Cytomine code.
This is not all the Groovy rules, this is the main used rules inside Cytomine (non-exhaustive!!!).
Java way | Groovy way | Note |
---|---|---|
statement; | statement
| ; is not mandatory at the end of a statement. |
return var; | var | If method is not void and no return statement, Groovy return the last exec statement in a function |
Object var; int var; List var; int foo(int var) { return var; } | def var int number def foo(def var) { var } | Auto typing. Java way is OK in Groovy too |
class User { private String name; public String getName() {...} public void setName(String name) {...} } | class User { private String name; //getter/setter auto generated } | Getter and setter auto generated for private members. |
| def object = ... object.newField = "test"
| You can add new field or new method to an object at Runtime. |
java.io.* java.lang.* java.math.BigDecimal java.math.BigInteger java.net.* java.util.* groovy.lang.* groovy.util.* |
| Groovy auto import all these packages. Aliases can be used in imports with "as" keyword (as in Python). |
if (annotation.getTerm() != null && annotation.getTerm().getUser() != null && annotation.getTerm().getUser().getId() == 1) { ... } | if (annotation.getTerm()?.getUser()?.getUser() == 1) { ... } | Safe navigation operator " ?. " |
String name = p.getName() != null ? p.getName() : ""; | def name = p.getName() ?: "" | Elvis operator " ?: ". To assign a value or return a default value if it is evaluated to false. |
try { methodMayThrowException() } catch(...) {...} | methodMayThrowException() | |
String s = "hello " + name String s = "hello " + user.getName(); | String s = "hello $name" String s = "hello ${user.getName()} | GStrings |
List<String> list = new list<String>(); | def list = [] def listNotEmpty = [1,2,3] println listNotEmpty[0] // prints 1 println listNotEmpty[-1] // prints 3 // Find in list if (x in list) { ... } | Syntacttic sugar for lists. Similar to Python lists. |
Map<Long,String> map = new Map<Long, String>(); String x = map.get(2); | def map = [:] def mapNotEmpty = [1:"a",2:"b",3:"c"] def x = map[2] //or map.get(2) def k = 'key3' def map = [ 'key1': 1, key2: 2, // Skip quotes, seen as String (k): 3 // Put () for variable ] assert map[k] == map['key3'] == map.key3 | Syntactic sugar for maps. |
for(String item : list) { doSomething(item); } | list.each { doSomething(it); } | Apply function on each element of a list. By default, the current item in each{} is it. .each {item -> ...} You can get the index (current position in list): .eachWithIndex {item, i -> ...} |
List<User> users = ... List<String> names = ... for(User user : users) { names.add(user.getName()); } | def names = users.collect{it.getName()) | Build a list from anohter one. |
System.out.println("hello"); | println("hello") println "hello" | Print. |
+/- Java 8 lambdas | def square = { it * it } println square(2) // prints 2 def power = { x, y -> Math.pow(x, y) } println power(2, 3) // prints 8 | Closures. |
Boolean x = (a == b); // Equality of primitive types x = a.equals(b); // Equality of instances | def x = a.is(b) // Equality of primitive types x = (a == b) // Equality of instances | Equals behaviour. |