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 * @version $Id: GrowthList.html 972421 2015-11-14 20:00:04Z tn $
054 */
055public class GrowthList<E> extends AbstractSerializableListDecorator<E> {
056
057    /** Serialization version */
058    private static final long serialVersionUID = -3620001881672L;
059
060    /**
061     * Factory method to create a growth list.
062     *
063     * @param <E> the type of the elements in the list
064     * @param list  the list to decorate, must not be null
065     * @return a new growth list
066     * @throws IllegalArgumentException if list is null
067     * @since 4.0
068     */
069    public static <E> GrowthList<E> growthList(final List<E> list) {
070        return new GrowthList<E>(list);
071    }
072
073    //-----------------------------------------------------------------------
074    /**
075     * Constructor that uses an ArrayList internally.
076     */
077    public GrowthList() {
078        super(new ArrayList<E>());
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(final int initialSize) {
088        super(new ArrayList<E>(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(final List<E> 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    @Override
121    public void add(final int index, final E element) {
122        final int size = decorated().size();
123        if (index > size) {
124            decorated().addAll(Collections.<E>nCopies(index - size, null));
125        }
126        decorated().add(index, element);
127    }
128
129    //-----------------------------------------------------------------------
130    /**
131     * Decorate the addAll method to perform the growth behaviour.
132     * <p>
133     * If the requested index is greater than the current size, the list will
134     * grow to the new size. Indices between the old size and the requested
135     * size will be filled with <code>null</code>.
136     * <p>
137     * If the index is less than the current size, the values will be added to
138     * the underlying list directly.
139     * If the index is less than zero, the underlying list is called, which
140     * will probably throw an IndexOutOfBoundsException.
141     *
142     * @param index  the index to add at
143     * @param coll  the collection to add at the specified index
144     * @return true if the list changed
145     * @throws UnsupportedOperationException if the underlying list doesn't implement set
146     * @throws ClassCastException if the underlying list rejects the element
147     * @throws IllegalArgumentException if the underlying list rejects the element
148     */
149    @Override
150    public boolean addAll(final int index, final Collection<? extends E> coll) {
151        final int size = decorated().size();
152        boolean result = false;
153        if (index > size) {
154            decorated().addAll(Collections.<E>nCopies(index - size, null));
155            result = true;
156        }
157        return decorated().addAll(index, coll) | result;
158    }
159
160    //-----------------------------------------------------------------------
161    /**
162     * Decorate the set method to perform the growth behaviour.
163     * <p>
164     * If the requested index is greater than the current size, the list will
165     * grow to the new size. Indices between the old size and the requested
166     * size will be filled with <code>null</code>.
167     * <p>
168     * If the index is less than the current size, the value will be set onto
169     * the underlying list directly.
170     * If the index is less than zero, the underlying list is called, which
171     * will probably throw an IndexOutOfBoundsException.
172     *
173     * @param index  the index to set
174     * @param element  the object to set at the specified index
175     * @return the object previously at that index
176     * @throws UnsupportedOperationException if the underlying list doesn't implement set
177     * @throws ClassCastException if the underlying list rejects the element
178     * @throws IllegalArgumentException if the underlying list rejects the element
179     */
180    @Override
181    public E set(final int index, final E element) {
182        final int size = decorated().size();
183        if (index >= size) {
184            decorated().addAll(Collections.<E>nCopies(index - size + 1, null));
185        }
186        return decorated().set(index, element);
187    }
188
189}