001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.collections4.list; 018 019import java.util.List; 020 021import org.apache.commons.collections4.Factory; 022 023/** 024 * Decorates another <code>List</code> to create objects in the list on demand. 025 * <p> 026 * When the {@link #get(int)} method is called with an index greater than 027 * the size of the list, the list will automatically grow in size and return 028 * a new object from the specified factory. The gaps will be filled by null. 029 * If a get method call encounters a null, it will be replaced with a new 030 * object from the factory. Thus this list is unsuitable for storing null 031 * objects. 032 * <p> 033 * For instance: 034 * 035 * <pre> 036 * Factory<Date> factory = new Factory<Date>() { 037 * public Date create() { 038 * return new Date(); 039 * } 040 * } 041 * List<Date> lazy = LazyList.decorate(new ArrayList<Date>(), factory); 042 * Date date = lazy.get(3); 043 * </pre> 044 * 045 * After the above code is executed, <code>date</code> will contain 046 * a new <code>Date</code> instance. Furthermore, that <code>Date</code> 047 * instance is the fourth element in the list. The first, second, 048 * and third element are all set to <code>null</code>. 049 * <p> 050 * This class differs from {@link GrowthList} because here growth occurs on 051 * get, where <code>GrowthList</code> grows on set and add. However, they 052 * could easily be used together by decorating twice. 053 * <p> 054 * This class is Serializable from Commons Collections 3.1. 055 * 056 * @see GrowthList 057 * @since 3.0 058 * @version $Id: LazyList.html 972421 2015-11-14 20:00:04Z tn $ 059 */ 060public class LazyList<E> extends AbstractSerializableListDecorator<E> { 061 062 /** Serialization version */ 063 private static final long serialVersionUID = -1708388017160694542L; 064 065 /** The factory to use to lazily instantiate the objects */ 066 private final Factory<? extends E> factory; 067 068 /** 069 * Factory method to create a lazily instantiating list. 070 * 071 * @param <E> the type of the elements in the list 072 * @param list the list to decorate, must not be null 073 * @param factory the factory to use for creation, must not be null 074 * @return a new lazy list 075 * @throws IllegalArgumentException if list or factory is null 076 * @since 4.0 077 */ 078 public static <E> LazyList<E> lazyList(final List<E> list, final Factory<? extends E> factory) { 079 return new LazyList<E>(list, factory); 080 } 081 082 //----------------------------------------------------------------------- 083 /** 084 * Constructor that wraps (not copies). 085 * 086 * @param list the list to decorate, must not be null 087 * @param factory the factory to use for creation, must not be null 088 * @throws IllegalArgumentException if list or factory is null 089 */ 090 protected LazyList(final List<E> list, final Factory<? extends E> factory) { 091 super(list); 092 if (factory == null) { 093 throw new IllegalArgumentException("Factory must not be null"); 094 } 095 this.factory = factory; 096 } 097 098 //----------------------------------------------------------------------- 099 /** 100 * Decorate the get method to perform the lazy behaviour. 101 * <p> 102 * If the requested index is greater than the current size, the list will 103 * grow to the new size and a new object will be returned from the factory. 104 * Indexes in-between the old size and the requested size are left with a 105 * placeholder that is replaced with a factory object when requested. 106 * 107 * @param index the index to retrieve 108 * @return the element at the given index 109 */ 110 @Override 111 public E get(final int index) { 112 final int size = decorated().size(); 113 if (index < size) { 114 // within bounds, get the object 115 E object = decorated().get(index); 116 if (object == null) { 117 // item is a place holder, create new one, set and return 118 object = factory.create(); 119 decorated().set(index, object); 120 return object; 121 } 122 // good and ready to go 123 return object; 124 } 125 // we have to grow the list 126 for (int i = size; i < index; i++) { 127 decorated().add(null); 128 } 129 // create our last object, set and return 130 final E object = factory.create(); 131 decorated().add(object); 132 return object; 133 } 134 135 @Override 136 public List<E> subList(final int fromIndex, final int toIndex) { 137 final List<E> sub = decorated().subList(fromIndex, toIndex); 138 return new LazyList<E>(sub, factory); 139 } 140 141}