import groovy.transform.CompileStatic
@CompileStatic
abstract class Person {
String firstName
String lastName
}
@CompileStatic
class Student extends Person {
Set<String> courses
}
@CompileStatic
final class Converter {
static <P extends Person> P toUser1(P person, Map map) { if (!map) {
return person
}
person.firstName = map['firstName']
person.lastName = map['lastName']
return person
}
static <P extends Person> P toUser2(P person, Map map) { if (!map) {
return person
}
person.tap {
firstName = map['firstName']
lastName = map['lastName']
}
}
static <P extends Person> P toUser3(P person, Map map) { person.tap {
firstName = map['firstName']
lastName = map['lastName']
}
}
static <P extends Person> P toUser4(P person, Map map) {
!map ? person : person.tap {
firstName = map['firstName']
lastName = map['lastName']
}
}
}
@CompileStatic
class Reproducer {
static void reproduce() {
Map map = [firstName: 'John', lastName: 'Doe']
println Converter.toUser1(new Student(), map).dump()
println Converter.toUser2(new Student(), map).dump()
println Converter.toUser3(new Student(), map).dump()
}
}
Reproducer.reproduce()