Create your own Groovy type conversion
06.02.2012Type conversion the standard way
Type conversion or casting is a programming language method for changing an object’s data type into another.
I’m sure you are familiar with this code that converts a String to an Integer.
def number = (Integer)'1'
def number = '1'.toInteger()
def number = '1' as Integer
If I want to convert the type of my own objects, I need to create a method to achieve this goal. I copy object properties to another object in a generic way; if a property exists on target object, I copy it from the source object.
class User {
String name
String city
Integer age
def toAdminUser() {
def adminUser = new AdminUser()
copyProperties(this, adminUser)
return adminUser
}
def copyProperties(source, target) {
source.properties.each { key, value ->
if (target.hasProperty(key) && !(key in ['class', 'metaClass'])) {
target[key] = value
}
}
}
}
class AdminUser {
String name
String city
Integer age
}
Now I can do something like this:
adminUser = user.toAdminUser()
Type conversion the fancy way
Great, but I want to use this fancy way to coerce one type to another:
adminUser = user as AdminUser
Simple, Groovy supports operator overloading and creating your own type conversion is really easy: we can override the asType() method.
class User {
String name
String city
Integer age
Object asType(Class clazz) {
if (clazz == AdminUser) {
def adminUser = new AdminUser()
copyProperties(this, adminUser)
return adminUser
}
else {
super.asType(clazz)
}
}
def copyProperties(source, target) {
source.properties.each { key, value ->
if (target.hasProperty(key) && !(key in ['class', 'metaClass'])) {
target[key] = value
}
}
}
}