Skip to content

Instantly share code, notes, and snippets.

@azhawkes
Created May 22, 2014 22:36
Show Gist options
  • Save azhawkes/5b38135baa0e96a6025f to your computer and use it in GitHub Desktop.
Save azhawkes/5b38135baa0e96a6025f to your computer and use it in GitHub Desktop.
Add a dynamic "commify" to Groovy's ArrayList
class BootStrap {
def init = { servletContext ->
ArrayList.metaClass.commify = {
StringBuilder output = new StringBuilder()
for (int i = 0; i < delegate.size(); i++) {
if (i == 0) {
output.append(delegate[i])
} else if (i == delegate.size() - 1) {
output.append(" and ${delegate[i]}")
} else {
output.append(", ${delegate[i]}")
}
}
return output.toString()
}
}
def destroy = {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment