View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections.list;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Collections;
22  import java.util.List;
23  
24  /**
25   * Decorates another <code>List</code> to make it seamlessly grow when
26   * indices larger than the list size are used on add and set,
27   * avoiding most IndexOutOfBoundsExceptions.
28   * <p>
29   * This class avoids errors by growing when a set or add method would
30   * normally throw an IndexOutOfBoundsException.
31   * Note that IndexOutOfBoundsException IS returned for invalid negative indices.
32   * <p>
33   * Trying to set or add to an index larger than the size will cause the list
34   * to grow (using <code>null</code> elements). Clearly, care must be taken
35   * not to use excessively large indices, as the internal list will grow to
36   * match.
37   * <p>
38   * Trying to use any method other than add or set with an invalid index will
39   * call the underlying list and probably result in an IndexOutOfBoundsException.
40   * <p>
41   * Take care when using this list with <code>null</code> values, as
42   * <code>null</code> is the value added when growing the list.
43   * <p>
44   * All sub-lists will access the underlying list directly, and will throw
45   * IndexOutOfBoundsExceptions.
46   * <p>
47   * This class differs from {@link LazyList} because here growth occurs on
48   * set and add, where <code>LazyList</code> grows on get. However, they
49   * can be used together by decorating twice.
50   *
51   * @see LazyList
52   * @since 3.2
53   * @version $Id: GrowthList.java 1429905 2013-01-07 17:15:14Z ggregory $
54   */
55  public class GrowthList<E> extends AbstractSerializableListDecorator<E> {
56  
57      /** Serialization version */
58      private static final long serialVersionUID = -3620001881672L;
59  
60      /**
61       * Factory method to create a growth list.
62       *
63       * @param <E> the type of the elements in the list
64       * @param list  the list to decorate, must not be null
65       * @return a new growth list
66       * @throws IllegalArgumentException if list is null
67       */
68      public static <E> GrowthList<E> growthList(final List<E> list) {
69          return new GrowthList<E>(list);
70      }
71  
72      //-----------------------------------------------------------------------
73      /**
74       * Constructor that uses an ArrayList internally.
75       */
76      public GrowthList() {
77          super(new ArrayList<E>());
78      }
79  
80      /**
81       * Constructor that uses an ArrayList internally.
82       *
83       * @param initialSize  the initial size of the ArrayList
84       * @throws IllegalArgumentException if initial size is invalid
85       */
86      public GrowthList(final int initialSize) {
87          super(new ArrayList<E>(initialSize));
88      }
89  
90      /**
91       * Constructor that wraps (not copies).
92       *
93       * @param list  the list to decorate, must not be null
94       * @throws IllegalArgumentException if list is null
95       */
96      protected GrowthList(final List<E> list) {
97          super(list);
98      }
99  
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 }