Created
October 27, 2009 21:17
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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