Decorates another
List
to create objects in the list on demand.
When the {@link #get(int)} method is called with an index greater thanthe size of the list, the list will automatically grow in size and return a new object from the specified factory. The gaps will be filled by null. If a get method call encounters a null, it will be replaced with a new object from the factory. Thus this list is unsuitable for storing null objects.
For instance:
Factory<Date> factory = new Factory<Date>() { public Date create() { return new Date(); } } List<Date> lazy = LazyList.decorate(new ArrayList<Date>(), factory); Date date = lazy.get(3);
After the above code is executed,
date
will contain a new
Date
instance. Furthermore, that
Date
instance is the fourth element in the list. The first, second, and third element are all set to
null
.
This class differs from {@link GrowthList} because here growth occurs onget, where GrowthList
grows on set and add. However, they could easily be used together by decorating twice.
This class is Serializable from Commons Collections 3.1.
@see GrowthList
@since 3.0
@version $Id: LazyList.java 1479405 2013-05-05 21:58:52Z tn $