/** * Factory method to create a growth list. * * @param <E> the type of the elements in the list * @param list the list to decorate, must not be null * @return a new growth list * @throws NullPointerException if list is null * @since 4.0 */ public static <E> GrowthList<E> growthList(final List<E> list) { return new GrowthList<>(list); }
/** * Decorate the add method to perform the growth behaviour. * <p> * If the requested index is greater than the current size, the list will * grow to the new size. Indices between the old size and the requested * size will be filled with <code>null</code>. * <p> * If the index is less than the current size, the value will be added to * the underlying list directly. * If the index is less than zero, the underlying list is called, which * will probably throw an IndexOutOfBoundsException. * * @param index the index to add at * @param element the object to add at the specified index * @throws UnsupportedOperationException if the underlying list doesn't implement set * @throws ClassCastException if the underlying list rejects the element * @throws IllegalArgumentException if the underlying list rejects the element */ @Override public void add(final int index, final E element) { final int size = decorated().size(); if (index > size) { decorated().addAll(Collections.<E>nCopies(index - size, null)); } decorated().add(index, element); }
/** * Decorate the set method to perform the growth behaviour. * <p> * If the requested index is greater than the current size, the list will * grow to the new size. Indices between the old size and the requested * size will be filled with <code>null</code>. * <p> * If the index is less than the current size, the value will be set onto * the underlying list directly. * If the index is less than zero, the underlying list is called, which * will probably throw an IndexOutOfBoundsException. * * @param index the index to set * @param element the object to set at the specified index * @return the object previously at that index * @throws UnsupportedOperationException if the underlying list doesn't implement set * @throws ClassCastException if the underlying list rejects the element * @throws IllegalArgumentException if the underlying list rejects the element */ @Override public E set(final int index, final E element) { final int size = decorated().size(); if (index >= size) { decorated().addAll(Collections.<E>nCopies(index - size + 1, null)); } return decorated().set(index, element); }
/** * Decorate the addAll method to perform the growth behaviour. * <p> * If the requested index is greater than the current size, the list will * grow to the new size. Indices between the old size and the requested * size will be filled with <code>null</code>. * <p> * If the index is less than the current size, the values will be added to * the underlying list directly. * If the index is less than zero, the underlying list is called, which * will probably throw an IndexOutOfBoundsException. * * @param index the index to add at * @param coll the collection to add at the specified index * @return true if the list changed * @throws UnsupportedOperationException if the underlying list doesn't implement set * @throws ClassCastException if the underlying list rejects the element * @throws IllegalArgumentException if the underlying list rejects the element */ @Override public boolean addAll(final int index, final Collection<? extends E> coll) { final int size = decorated().size(); boolean result = false; if (index > size) { decorated().addAll(Collections.<E>nCopies(index - size, null)); result = true; } return decorated().addAll(index, coll) | result; }