com.iparelan.util
Class ArrayWrapperList<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<E>
          extended by com.iparelan.util.ArrayWrapperList<E>
Type Parameters:
E - The List element type.
All Implemented Interfaces:
Iterable<E>, Collection<E>, List<E>

public class ArrayWrapperList<E>
extends AbstractList<E>

A List implementation that wraps an array. Unlike ArrayList, this implementation does not create a private, internal-only array, rather it wraps an array provided as a constructor argument. Using a wrapped array means that "double copies" can be avoided. The trade-off is that encapsulation is broken. Care must be taken when using this class to ensure that the data structure remains consistent.

This List implementation is useful is situations where a "double copy" is to be avoided and the Arrays.asList(T...) method cannot be used because the List produced by that method does not override the AbstractList.add(int, Object) method of AbstractList.

One such situation occurs when a BlockingQueue drainTo(Collection<? super E>, int) operation takes place. The drainTo() method attempts to write data to the List produced by Arrays.asList(T...), but an UnsupportedOperationException is raised when AbstractList.add(int, Object) is invoked.

The maximum size() of this list is fixed by the length of the wrapped array.

This List does not permit a null element.

The initial size of this List is defined to be the least index of all null wrapped array elements.

Altering the wrapped array elements by any means other than invoking methods of this class causes the behavior of all future method invocations to be undefined.

This implementation is not thread-safe. Consider Collections#synchronizedList(List<T>).

Version:
July 2008
Author:
Greg Mattes

Field Summary
private  E[] array
           
private  int elementCount
           
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
ArrayWrapperList(E[] array)
          Creates a new ArrayWrapperList.
 
Method Summary
 void add(int index, E element)
           
 E get(int index)
           
protected  void removeRange(int fromIndex, int toIndex)
           
 int size()
           
 
Methods inherited from class java.util.AbstractList
add, addAll, clear, equals, hashCode, indexOf, iterator, lastIndexOf, listIterator, listIterator, remove, set, subList
 
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray
 

Field Detail

array

private final E[] array

elementCount

private int elementCount
Constructor Detail

ArrayWrapperList

public ArrayWrapperList(E[] array)
Creates a new ArrayWrapperList.

Parameters:
array - The array to wrap. May not be null.
Throws:
IllegalArgumentException - Iff array is null.
Method Detail

size

public int size()
Specified by:
size in interface Collection<E>
Specified by:
size in interface List<E>
Specified by:
size in class AbstractCollection<E>

removeRange

protected void removeRange(int fromIndex,
                           int toIndex)
Overrides:
removeRange in class AbstractList<E>

get

public E get(int index)
Specified by:
get in interface List<E>
Specified by:
get in class AbstractList<E>

add

public void add(int index,
                E element)
Specified by:
add in interface List<E>
Overrides:
add in class AbstractList<E>
Throws:
UnsupportedOperationException
ClassCastException
NullPointerException
IndexOutOfBoundsException
IllegalArgumentException - Iff the capacity of this list would be exceeded by this operation.


Copyright © 2008 Iparelan Solutions, LLC. All rights reserved.