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.ArrayList; 020import java.util.Collection; 021import java.util.Collections; 022import java.util.List; 023 024/** 025 * Decorates another <code>List</code> to make it seamlessly grow when 026 * indices larger than the list size are used on add and set, 027 * avoiding most IndexOutOfBoundsExceptions. 028 * <p> 029 * This class avoids errors by growing when a set or add method would 030 * normally throw an IndexOutOfBoundsException. 031 * Note that IndexOutOfBoundsException IS returned for invalid negative indices. 032 * <p> 033 * Trying to set or add to an index larger than the size will cause the list 034 * to grow (using <code>null</code> elements). Clearly, care must be taken 035 * not to use excessively large indices, as the internal list will grow to 036 * match. 037 * <p> 038 * Trying to use any method other than add or set with an invalid index will 039 * call the underlying list and probably result in an IndexOutOfBoundsException. 040 * <p> 041 * Take care when using this list with <code>null</code> values, as 042 * <code>null</code> is the value added when growing the list. 043 * <p> 044 * All sub-lists will access the underlying list directly, and will throw 045 * IndexOutOfBoundsExceptions. 046 * <p> 047 * This class differs from {@link LazyList} because here growth occurs on 048 * set and add, where <code>LazyList</code> grows on get. However, they 049 * can be used together by decorating twice. 050 * 051 * @see LazyList 052 * @since 3.2 053 */ 054public class GrowthList<E> extends AbstractSerializableListDecorator<E> { 055 056 /** Serialization version */ 057 private static final long serialVersionUID = -3620001881672L; 058 059 /** 060 * Factory method to create a growth list. 061 * 062 * @param <E> the type of the elements in the list 063 * @param list the list to decorate, must not be null 064 * @return a new growth list 065 * @throws NullPointerException if list is null 066 * @since 4.0 067 */ 068 public static <E> GrowthList<E> growthList(final List<E> list) { 069 return new GrowthList<>(list); 070 } 071 072 //----------------------------------------------------------------------- 073 /** 074 * Constructor that uses an ArrayList internally. 075 */ 076 public GrowthList() { 077 super(new ArrayList<E>()); 078 } 079 080 /** 081 * Constructor that uses an ArrayList internally. 082 * 083 * @param initialSize the initial size of the ArrayList 084 * @throws IllegalArgumentException if initial size is invalid 085 */ 086 public GrowthList(final int initialSize) { 087 super(new ArrayList<E>(initialSize)); 088 } 089 090 /** 091 * Constructor that wraps (not copies). 092 * 093 * @param list the list to decorate, must not be null 094 * @throws NullPointerException if list is null 095 */ 096 protected GrowthList(final List<E> list) { 097 super(list); 098 } 099 100 //----------------------------------------------------------------------- 101 /** 102 * Decorate the add method to perform the growth behaviour. 103 * <p> 104 * If the requested index is greater than the current size, the list will 105 * grow to the new size. Indices between the old size and the requested 106 * size will be filled with <code>null</code>. 107 * <p> 108 * If the index is less than the current size, the value will be added to 109 * the underlying list directly. 110 * If the index is less than zero, the underlying list is called, which 111 * will probably throw an IndexOutOfBoundsException. 112 * 113 * @param index the index to add at 114 * @param element the object to add at the specified index 115 * @throws UnsupportedOperationException if the underlying list doesn't implement set 116 * @throws ClassCastException if the underlying list rejects the element 117 * @throws IllegalArgumentException if the underlying list rejects the element 118 */ 119 @Override 120 public void add(final int index, final E element) { 121 final int size = decorated().size(); 122 if (index > size) { 123 decorated().addAll(Collections.<E>nCopies(index - size, null)); 124 } 125 decorated().add(index, element); 126 } 127 128 //----------------------------------------------------------------------- 129 /** 130 * Decorate the addAll method to perform the growth behaviour. 131 * <p> 132 * If the requested index is greater than the current size, the list will 133 * grow to the new size. Indices between the old size and the requested 134 * size will be filled with <code>null</code>. 135 * <p> 136 * If the index is less than the current size, the values will be added to 137 * the underlying list directly. 138 * If the index is less than zero, the underlying list is called, which 139 * will probably throw an IndexOutOfBoundsException. 140 * 141 * @param index the index to add at 142 * @param coll the collection to add at the specified index 143 * @return true if the list changed 144 * @throws UnsupportedOperationException if the underlying list doesn't implement set 145 * @throws ClassCastException if the underlying list rejects the element 146 * @throws IllegalArgumentException if the underlying list rejects the element 147 */ 148 @Override 149 public boolean addAll(final int index, final Collection<? extends E> coll) { 150 final int size = decorated().size(); 151 boolean result = false; 152 if (index > size) { 153 decorated().addAll(Collections.<E>nCopies(index - size, null)); 154 result = true; 155 } 156 return decorated().addAll(index, coll) | result; 157 } 158 159 //----------------------------------------------------------------------- 160 /** 161 * Decorate the set method to perform the growth behaviour. 162 * <p> 163 * If the requested index is greater than the current size, the list will 164 * grow to the new size. Indices between the old size and the requested 165 * size will be filled with <code>null</code>. 166 * <p> 167 * If the index is less than the current size, the value will be set onto 168 * the underlying list directly. 169 * If the index is less than zero, the underlying list is called, which 170 * will probably throw an IndexOutOfBoundsException. 171 * 172 * @param index the index to set 173 * @param element the object to set at the specified index 174 * @return the object previously at that index 175 * @throws UnsupportedOperationException if the underlying list doesn't implement set 176 * @throws ClassCastException if the underlying list rejects the element 177 * @throws IllegalArgumentException if the underlying list rejects the element 178 */ 179 @Override 180 public E set(final int index, final E element) { 181 final int size = decorated().size(); 182 if (index >= size) { 183 decorated().addAll(Collections.<E>nCopies(index - size + 1, null)); 184 } 185 return decorated().set(index, element); 186 } 187 188}