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 */ 059public class LazyList<E> extends AbstractSerializableListDecorator<E> { 060 061 /** Serialization version */ 062 private static final long serialVersionUID = -1708388017160694542L; 063 064 /** The factory to use to lazily instantiate the objects */ 065 private final Factory<? extends E> factory; 066 067 /** 068 * Factory method to create a lazily instantiating list. 069 * 070 * @param <E> the type of the elements in the list 071 * @param list the list to decorate, must not be null 072 * @param factory the factory to use for creation, must not be null 073 * @return a new lazy list 074 * @throws NullPointerException if list or factory is null 075 * @since 4.0 076 */ 077 public static <E> LazyList<E> lazyList(final List<E> list, final Factory<? extends E> factory) { 078 return new LazyList<>(list, factory); 079 } 080 081 //----------------------------------------------------------------------- 082 /** 083 * Constructor that wraps (not copies). 084 * 085 * @param list the list to decorate, must not be null 086 * @param factory the factory to use for creation, must not be null 087 * @throws NullPointerException if list or factory is null 088 */ 089 protected LazyList(final List<E> list, final Factory<? extends E> factory) { 090 super(list); 091 if (factory == null) { 092 throw new IllegalArgumentException("Factory must not be null"); 093 } 094 this.factory = factory; 095 } 096 097 //----------------------------------------------------------------------- 098 /** 099 * Decorate the get method to perform the lazy behaviour. 100 * <p> 101 * If the requested index is greater than the current size, the list will 102 * grow to the new size and a new object will be returned from the factory. 103 * Indexes in-between the old size and the requested size are left with a 104 * placeholder that is replaced with a factory object when requested. 105 * 106 * @param index the index to retrieve 107 * @return the element at the given index 108 */ 109 @Override 110 public E get(final int index) { 111 final int size = decorated().size(); 112 if (index < size) { 113 // within bounds, get the object 114 E object = decorated().get(index); 115 if (object == null) { 116 // item is a place holder, create new one, set and return 117 object = factory.create(); 118 decorated().set(index, object); 119 return object; 120 } 121 // good and ready to go 122 return object; 123 } 124 // we have to grow the list 125 for (int i = size; i < index; i++) { 126 decorated().add(null); 127 } 128 // create our last object, set and return 129 final E object = factory.create(); 130 decorated().add(object); 131 return object; 132 } 133 134 @Override 135 public List<E> subList(final int fromIndex, final int toIndex) { 136 final List<E> sub = decorated().subList(fromIndex, toIndex); 137 return new LazyList<>(sub, factory); 138 } 139 140}