Skip to content

Instantly share code, notes, and snippets.

@joshuatbrown
Created October 27, 2009 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshuatbrown/219964 to your computer and use it in GitHub Desktop.
Save joshuatbrown/219964 to your computer and use it in GitHub Desktop.
A method for list that only adds the object if it's not null
List.metaClass.addIfNotNull = {
if (it != null) add(it)
}
def list = []
list.addIfNotNull(null)
assert 0 == list.size()
list.addIfNotNull("foo")
assert 1 == list.size()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment