org.geotoolkit.util.collection
Class UnmodifiableArrayList<E>

Object
  extended by AbstractCollection<E>
      extended by AbstractList<E>
          extended by UnmodifiableArrayList<E>
Type Parameters:
E - The type of elements in the list.
All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, List<E>, CheckedCollection<E>, CheckedContainer<E>

public class UnmodifiableArrayList<E>
extends AbstractList<E>
implements CheckedCollection<E>, Serializable

An unmodifiable view of an array. Invoking

UnmodifiableArrayList.wrap(array);
is equivalent to
Collections.unmodifiableList(Arrays.asList(array));
But this class uses one less level of indirection.

Since:
2.1
Version:
3.00
Author:
Martin Desruisseaux (IRD)
See Also:
Serialized Form
Module:
utility/geotk-utility (download)    View source code for this class

Field Summary
 
Fields inherited from class AbstractList
modCount
 
Constructor Summary
protected UnmodifiableArrayList(E... array)
          Creates a new instance of an array list.
 
Method Summary
 int arraySize()
          Returns the size of the array backing this list.
 boolean contains(Object object)
          Returns true if this collection contains the specified element.
 boolean equals(Object object)
          Compares this list with the given object for equality.
 E get(int index)
          Returns the element at the specified index.
 Class<E> getElementType()
          Returns the element type of the wrapped array.
 int indexOf(Object object)
          Returns the index in this list of the first occurrence of the specified element, or -1 if the list does not contain this element.
 int lastIndexOf(Object object)
          Returns the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element.
 int size()
          Returns the list size.
 UnmodifiableArrayList<E> subList(int lower, int upper)
          Returns a view of the portion of this list between the specified lower, inclusive, and upper, exclusive.
static
<E> UnmodifiableArrayList<E>
wrap(E... array)
          Creates a new instance of an array list.
static
<E> UnmodifiableArrayList<E>
wrap(E[] array, int lower, int upper)
          Creates a new instance of an array list over a subregion of the given array.
 
Methods inherited from class AbstractList
add, add, addAll, clear, hashCode, iterator, listIterator, listIterator, remove, removeRange, set
 
Methods inherited from class AbstractCollection
addAll, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
 
Methods inherited from class Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface Collection
add, addAll, clear, containsAll, hashCode, isEmpty, iterator, remove, removeAll, retainAll, toArray, toArray
 
Methods inherited from interface List
addAll, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray
 

Constructor Detail

UnmodifiableArrayList

protected UnmodifiableArrayList(E... array)
Creates a new instance of an array list. A direct reference to the given array is retained (i.e. the array is not cloned). Consequently the given array should not be modified after construction if this list is intended to be immutable.

This constructor is for subclassing only. Users should invoke the wrap(E...) static factory method, which provides more convenient handling of parameterized types.

Parameters:
array - The array to wrap.
Method Detail

wrap

public static <E> UnmodifiableArrayList<E> wrap(E... array)
Creates a new instance of an array list. A direct reference to the given array is retained (i.e. the array is not cloned). Consequently the given array should not be modified after construction if this list is intended to be immutable.

Type Parameters:
E - The type of elements in the list.
Parameters:
array - The array to wrap, or null if none.
Returns:
The given array wrapped in an unmodifiable list, or null if the given array was null.
Since:
2.5

wrap

public static <E> UnmodifiableArrayList<E> wrap(E[] array,
                                                int lower,
                                                int upper)
                                     throws IndexOutOfBoundsException
Creates a new instance of an array list over a subregion of the given array. A direct reference to the given array is retained (i.e. the array is not cloned). Consequently the given array should not be modified after construction if this list is intended to be immutable.

Type Parameters:
E - The type of elements in the list.
Parameters:
array - The array to wrap.
lower - low endpoint (inclusive) of the sublist.
upper - high endpoint (exclusive) of the sublist.
Returns:
The given array wrapped in an unmodifiable list.
Throws:
IndexOutOfBoundsException - If the lower or upper value are out of bounds.
Since:
3.00

getElementType

public Class<E> getElementType()
Returns the element type of the wrapped array. The default implementation returns the value of Class.getComponentType().

Specified by:
getElementType in interface CheckedCollection<E>
Specified by:
getElementType in interface CheckedContainer<E>
Returns:
The type of elements in the list.

size

public int size()
Returns the list size.

Specified by:
size in interface Collection<E>
Specified by:
size in interface List<E>
Specified by:
size in class AbstractCollection<E>

arraySize

public final int arraySize()
Returns the size of the array backing this list. This is the length of the array given to the constructor. It is equal to size() except if this instance is a sublist, in which case the value returned by this method is greater than size().

This method is sometime used as a hint for choosing a UnmodifiableArrayList instance to keep, given a choice. Note that a greater value is not necessarily more memory consuming, since the backing array may be shared by many sublists.

Returns:
The length of the backing array.
Since:
3.00

get

public E get(int index)
Returns the element at the specified index.

Specified by:
get in interface List<E>
Specified by:
get in class AbstractList<E>

indexOf

public int indexOf(Object object)
Returns the index in this list of the first occurrence of the specified element, or -1 if the list does not contain this element.

Specified by:
indexOf in interface List<E>
Overrides:
indexOf in class AbstractList<E>
Parameters:
object - The element to search for.
Returns:
The index of the first occurrence of the given object, or -1.

lastIndexOf

public int lastIndexOf(Object object)
Returns the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element.

Specified by:
lastIndexOf in interface List<E>
Overrides:
lastIndexOf in class AbstractList<E>
Parameters:
object - The element to search for.
Returns:
The index of the last occurrence of the given object, or -1.

contains

public boolean contains(Object object)
Returns true if this collection contains the specified element.

Specified by:
contains in interface Collection<E>
Specified by:
contains in interface List<E>
Overrides:
contains in class AbstractCollection<E>
Parameters:
object - The element to check for existence.
Returns:
true if this collection contains the given element.

subList

public UnmodifiableArrayList<E> subList(int lower,
                                        int upper)
                                 throws IndexOutOfBoundsException
Returns a view of the portion of this list between the specified lower, inclusive, and upper, exclusive.

Specified by:
subList in interface List<E>
Overrides:
subList in class AbstractList<E>
Parameters:
lower - low endpoint (inclusive) of the sublist.
upper - high endpoint (exclusive) of the sublist.
Returns:
A view of the specified range within this list.
Throws:
IndexOutOfBoundsException - If the lower or upper value are out of bounds.

equals

public boolean equals(Object object)
Compares this list with the given object for equality.

Specified by:
equals in interface Collection<E>
Specified by:
equals in interface List<E>
Overrides:
equals in class AbstractList<E>
Parameters:
object - The object to compare with this list.
Returns:
true if the given object is equal to this list.


Copyright © 2009-2012 Geotoolkit.org. All Rights Reserved.