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 */ 017 package org.apache.commons.collections.list; 018 019 import java.util.ArrayList; 020 import java.util.Collection; 021 import java.util.Collections; 022 import 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 Commons Collections 3.2 053 * @version $Revision: 155406 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $ 054 * 055 * @author Stephen Colebourne 056 * @author Paul Legato 057 */ 058 public class GrowthList extends AbstractSerializableListDecorator { 059 060 /** Serialization version */ 061 private static final long serialVersionUID = -3620001881672L; 062 063 /** 064 * Factory method to create a growth list. 065 * 066 * @param list the list to decorate, must not be null 067 * @throws IllegalArgumentException if list is null 068 */ 069 public static List decorate(List list) { 070 return new GrowthList(list); 071 } 072 073 //----------------------------------------------------------------------- 074 /** 075 * Constructor that uses an ArrayList internally. 076 */ 077 public GrowthList() { 078 super(new ArrayList()); 079 } 080 081 /** 082 * Constructor that uses an ArrayList internally. 083 * 084 * @param initialSize the initial size of the ArrayList 085 * @throws IllegalArgumentException if initial size is invalid 086 */ 087 public GrowthList(int initialSize) { 088 super(new ArrayList(initialSize)); 089 } 090 091 /** 092 * Constructor that wraps (not copies). 093 * 094 * @param list the list to decorate, must not be null 095 * @throws IllegalArgumentException if list is null 096 */ 097 protected GrowthList(List list) { 098 super(list); 099 } 100 101 //----------------------------------------------------------------------- 102 /** 103 * Decorate the add method to perform the growth behaviour. 104 * <p> 105 * If the requested index is greater than the current size, the list will 106 * grow to the new size. Indices between the old size and the requested 107 * size will be filled with <code>null</code>. 108 * <p> 109 * If the index is less than the current size, the value will be added to 110 * the underlying list directly. 111 * If the index is less than zero, the underlying list is called, which 112 * will probably throw an IndexOutOfBoundsException. 113 * 114 * @param index the index to add at 115 * @param element the object to add at the specified index 116 * @throws UnsupportedOperationException if the underlying list doesn't implement set 117 * @throws ClassCastException if the underlying list rejects the element 118 * @throws IllegalArgumentException if the underlying list rejects the element 119 */ 120 public void add(int index, Object element) { 121 int size = getList().size(); 122 if (index > size) { 123 getList().addAll(Collections.nCopies(index - size, null)); 124 } 125 getList().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 public boolean addAll(int index, Collection coll) { 149 int size = getList().size(); 150 boolean result = false; 151 if (index > size) { 152 getList().addAll(Collections.nCopies(index - size, null)); 153 result = true; 154 } 155 return (getList().addAll(index, coll) | result); 156 } 157 158 //----------------------------------------------------------------------- 159 /** 160 * Decorate the set method to perform the growth behaviour. 161 * <p> 162 * If the requested index is greater than the current size, the list will 163 * grow to the new size. Indices between the old size and the requested 164 * size will be filled with <code>null</code>. 165 * <p> 166 * If the index is less than the current size, the value will be set onto 167 * the underlying list directly. 168 * If the index is less than zero, the underlying list is called, which 169 * will probably throw an IndexOutOfBoundsException. 170 * 171 * @param index the index to set 172 * @param element the object to set at the specified index 173 * @return the object previously at that index 174 * @throws UnsupportedOperationException if the underlying list doesn't implement set 175 * @throws ClassCastException if the underlying list rejects the element 176 * @throws IllegalArgumentException if the underlying list rejects the element 177 */ 178 public Object set(int index, Object element) { 179 int size = getList().size(); 180 if (index >= size) { 181 getList().addAll(Collections.nCopies((index - size) + 1, null)); 182 } 183 return getList().set(index, element); 184 } 185 186 }