Grails supports command object binding . Request parameters are bound to properties of a given command class. From this context, a special-case is the use of lazy list properties.
A lazy list grows whenever an index greater than its size is requested. Apache Commons provides a LazyList decorator implementation .
The decorate method returns a java.util.List descendant overwriting List#get(index). Whenever get(index) is called with an index greater than size, the list grows to the specified index and the factory creates a default value for this index. The gaps between the old and the new index will be filled with null.
LazyList can be used in Grails command classes for dynamic lists.
Whenever dynamicProperty[index] is accessed the list is grown to the specified index if needed and the provided factory creates a default value ("1" in the code listing above).
Doin' it the Groovy way
Groovy 2.0 comes with a new GDK method emulating Commons LazyList behavior: List.withDefault. Whenever a list is created by calling withDefault it returns a lazy list with exactly the same properties as described above:
As shown in the code listing, the gap is filled with null placeholders. There is another property of lazy lists that is worth noting at this point. Any subsequent calls to get(index) never return null, as null is only used as placeholder object. Thus, when calling get(index) referencing a slot with a placeholder, the default value will be returned. Or turning it around: this variant of lazy list never returns null when get(index) is called.
Lazy and Eager Lists
So far only half the truth has been told. Internally, withDefault redirects to withLazyDefault, as there is another lazy list variant available: withEagerDefault.
This method can be used to create an eager instead of a lazy list. Again, eager lists grow as indices greater than the actual size are accessed, but it does fill gaps with default values instead of null placeholders. As a consequence, eager lists might hold null values as valid list elements too.
As this feature comes with Groovy 1.8.7 and 2.0, it will be available with one of the next Grails versions. Once bundled, commands objects can be defined without using Apache Commons, in a truly Groovy way :-)