org.geotools.util
Class CheckedArrayList<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<E>
          extended by java.util.ArrayList<E>
              extended by org.geotools.util.CheckedArrayList<E>
Type Parameters:
E - The type of elements in the list.
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<E>, java.util.Collection<E>, java.util.List<E>, java.util.RandomAccess, CheckedCollection<E>
Direct Known Subclasses:
XPath.StepList

public class CheckedArrayList<E>
extends java.util.ArrayList<E>
implements CheckedCollection<E>, org.opengis.util.Cloneable

A checked and synchronized List. Type checks are performed at run-time in addition of compile-time checks. The synchronization lock can be modified at runtime by overriding the getLock() method.

This class is similar to using the wrappers provided in Collections, minus the cost of indirection levels and with the addition of overrideable methods.

Since:
2.1
Version:
$Id: CheckedArrayList.java 30852 2008-07-03 15:59:42Z desruisseaux $
Author:
Jody Garnett (Refractions Research), Martin Desruisseaux (IRD)
See Also:
Collections.checkedList(java.util.List, java.lang.Class), Collections.synchronizedList(java.util.List), Serialized Form

Field Summary
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
CheckedArrayList(java.lang.Class<E> type)
          Constructs a list of the specified type.
CheckedArrayList(java.lang.Class<E> type, int capacity)
          Constructs a list of the specified type and initial capacity.
 
Method Summary
 boolean add(E element)
          Appends the specified element to the end of this list.
 void add(int index, E element)
          Inserts the specified element at the specified position in this list.
 boolean addAll(java.util.Collection<? extends E> collection)
          Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified Collection's Iterator.
 boolean addAll(int index, java.util.Collection<? extends E> collection)
          Inserts all of the elements in the specified collection into this list, starting at the specified position.
protected  void checkWritePermission()
          Checks if changes in this collection are allowed.
 void clear()
          Removes all of the elements from this list.
 CheckedArrayList<E> clone()
          Returns a shallow copy of this list.
 boolean contains(java.lang.Object o)
          Returns true if this list contains the specified element.
 void ensureCapacity(int minCapacity)
          Increases the capacity, if necessary, to ensure that it can hold the given number of elements.
protected  void ensureValidType(E element)
          Checks the type of the specified object.
 boolean equals(java.lang.Object o)
          Compares the specified object with this list for equality.
 E get(int index)
          Returns the element at the specified position in this list.
 java.lang.Class<E> getElementType()
          Returns the element type given at construction time.
protected  java.lang.Object getLock()
          Returns the synchronization lock.
 int hashCode()
          Returns the hash code value for this list.
 int indexOf(java.lang.Object o)
          Returns the index of the first occurrence of the specified element in this list, or -1 if none.
 boolean isEmpty()
          Returns true if this list contains no elements.
 java.util.Iterator<E> iterator()
          Returns an iterator over the elements in this list.
 int lastIndexOf(java.lang.Object o)
          Returns the index of the last occurrence of the specified element in this list, or -1 if none.
 E remove(int index)
          Removes the element at the specified position in this list.
 boolean remove(java.lang.Object o)
          Removes the first occurrence of the specified element from this list.
 boolean removeAll(java.util.Collection<?> c)
          Removes all of this list's elements that are also contained in the specified collection.
 boolean retainAll(java.util.Collection<?> c)
          Retains only the elements in this list that are contained in the specified collection.
 E set(int index, E element)
          Replaces the element at the specified position in this list with the specified element.
 int size()
          Returns the number of elements in this list.
 java.lang.Object[] toArray()
          Returns an array containing all of the elements in this list.
<T> T[]
toArray(T[] a)
          Returns an array containing all of the elements in this list in proper sequence.
 java.lang.String toString()
          Returns a string representation of this list.
 void trimToSize()
          Trims the capacity to the list's current size.
 
Methods inherited from class java.util.ArrayList
listIterator, listIterator, removeRange, subList
 
Methods inherited from class java.util.AbstractCollection
containsAll
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Collection
containsAll
 
Methods inherited from interface java.util.List
containsAll
 

Constructor Detail

CheckedArrayList

public CheckedArrayList(java.lang.Class<E> type)
Constructs a list of the specified type.

Parameters:
type - The element type (should not be null).

CheckedArrayList

public CheckedArrayList(java.lang.Class<E> type,
                        int capacity)
Constructs a list of the specified type and initial capacity.

Parameters:
type - The element type (should not be null).
capacity - The initial capacity.
Since:
2.4
Method Detail

getElementType

public java.lang.Class<E> getElementType()
Returns the element type given at construction time.

Specified by:
getElementType in interface CheckedCollection<E>
Returns:
The element type.
Since:
2.4

ensureValidType

protected void ensureValidType(E element)
                        throws java.lang.IllegalArgumentException
Checks the type of the specified object. The default implementation ensure that the object is assignable to the type specified at construction time.

Parameters:
element - the object to check, or null.
Throws:
java.lang.IllegalArgumentException - if the specified element is not of the expected type.

checkWritePermission

protected void checkWritePermission()
                             throws java.lang.UnsupportedOperationException
Checks if changes in this collection are allowed. This method is automatically invoked after this collection got the lock and before any operation that may change the content. The default implementation does nothing (i.e. this collection is modifiable). Subclasses should override this method if they want to control write access.

Throws:
java.lang.UnsupportedOperationException - if this collection is unmodifiable.
Since:
2.5

getLock

protected java.lang.Object getLock()
Returns the synchronization lock. The default implementation returns this. Subclasses that override this method should be careful to update the lock reference when this list is cloned.

Returns:
The synchronization lock.
Since:
2.5

iterator

public java.util.Iterator<E> iterator()
Returns an iterator over the elements in this list.

Specified by:
iterator in interface java.lang.Iterable<E>
Specified by:
iterator in interface java.util.Collection<E>
Specified by:
iterator in interface java.util.List<E>
Overrides:
iterator in class java.util.ArrayList<E>

size

public int size()
Returns the number of elements in this list.

Specified by:
size in interface java.util.Collection<E>
Specified by:
size in interface java.util.List<E>
Overrides:
size in class java.util.ArrayList<E>

isEmpty

public boolean isEmpty()
Returns true if this list contains no elements.

Specified by:
isEmpty in interface java.util.Collection<E>
Specified by:
isEmpty in interface java.util.List<E>
Overrides:
isEmpty in class java.util.ArrayList<E>

contains

public boolean contains(java.lang.Object o)
Returns true if this list contains the specified element.

Specified by:
contains in interface java.util.Collection<E>
Specified by:
contains in interface java.util.List<E>
Overrides:
contains in class java.util.ArrayList<E>

indexOf

public int indexOf(java.lang.Object o)
Returns the index of the first occurrence of the specified element in this list, or -1 if none.

Specified by:
indexOf in interface java.util.List<E>
Overrides:
indexOf in class java.util.ArrayList<E>

lastIndexOf

public int lastIndexOf(java.lang.Object o)
Returns the index of the last occurrence of the specified element in this list, or -1 if none.

Specified by:
lastIndexOf in interface java.util.List<E>
Overrides:
lastIndexOf in class java.util.ArrayList<E>

get

public E get(int index)
Returns the element at the specified position in this list.

Specified by:
get in interface java.util.List<E>
Overrides:
get in class java.util.ArrayList<E>

set

public E set(int index,
             E element)
      throws java.lang.IllegalArgumentException,
             java.lang.UnsupportedOperationException
Replaces the element at the specified position in this list with the specified element.

Specified by:
set in interface java.util.List<E>
Overrides:
set in class java.util.ArrayList<E>
Parameters:
index - index of element to replace.
element - element to be stored at the specified position.
Returns:
the element previously at the specified position.
Throws:
java.lang.IndexOutOfBoundsException - if index out of range.
java.lang.IllegalArgumentException - if the specified element is not of the expected type.
java.lang.UnsupportedOperationException - if this collection is unmodifiable.

add

public boolean add(E element)
            throws java.lang.IllegalArgumentException,
                   java.lang.UnsupportedOperationException
Appends the specified element to the end of this list.

Specified by:
add in interface java.util.Collection<E>
Specified by:
add in interface java.util.List<E>
Overrides:
add in class java.util.ArrayList<E>
Parameters:
element - element to be appended to this list.
Returns:
always true.
Throws:
java.lang.IllegalArgumentException - if the specified element is not of the expected type.
java.lang.UnsupportedOperationException - if this collection is unmodifiable.

add

public void add(int index,
                E element)
         throws java.lang.IllegalArgumentException,
                java.lang.UnsupportedOperationException
Inserts the specified element at the specified position in this list.

Specified by:
add in interface java.util.List<E>
Overrides:
add in class java.util.ArrayList<E>
Parameters:
index - index at which the specified element is to be inserted.
element - element to be inserted.
Throws:
java.lang.IndexOutOfBoundsException - if index out of range.
java.lang.IllegalArgumentException - if the specified element is not of the expected type.
java.lang.UnsupportedOperationException - if this collection is unmodifiable.

addAll

public boolean addAll(java.util.Collection<? extends E> collection)
               throws java.lang.IllegalArgumentException,
                      java.lang.UnsupportedOperationException
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified Collection's Iterator.

Specified by:
addAll in interface java.util.Collection<E>
Specified by:
addAll in interface java.util.List<E>
Overrides:
addAll in class java.util.ArrayList<E>
Parameters:
collection - the elements to be inserted into this list.
Returns:
true if this list changed as a result of the call.
Throws:
java.lang.IllegalArgumentException - if at least one element is not of the expected type.
java.lang.UnsupportedOperationException - if this collection is unmodifiable.

addAll

public boolean addAll(int index,
                      java.util.Collection<? extends E> collection)
               throws java.lang.IllegalArgumentException,
                      java.lang.UnsupportedOperationException
Inserts all of the elements in the specified collection into this list, starting at the specified position.

Specified by:
addAll in interface java.util.List<E>
Overrides:
addAll in class java.util.ArrayList<E>
Parameters:
index - index at which to insert first element fromm the specified collection.
collection - elements to be inserted into this list.
Returns:
true if this list changed as a result of the call.
Throws:
java.lang.IllegalArgumentException - if at least one element is not of the expected type.
java.lang.UnsupportedOperationException - if this collection is unmodifiable.

remove

public E remove(int index)
         throws java.lang.UnsupportedOperationException
Removes the element at the specified position in this list.

Specified by:
remove in interface java.util.List<E>
Overrides:
remove in class java.util.ArrayList<E>
Throws:
java.lang.UnsupportedOperationException - if this collection is unmodifiable.

remove

public boolean remove(java.lang.Object o)
               throws java.lang.UnsupportedOperationException
Removes the first occurrence of the specified element from this list.

Specified by:
remove in interface java.util.Collection<E>
Specified by:
remove in interface java.util.List<E>
Overrides:
remove in class java.util.ArrayList<E>
Throws:
java.lang.UnsupportedOperationException - if this collection is unmodifiable.

removeAll

public boolean removeAll(java.util.Collection<?> c)
                  throws java.lang.UnsupportedOperationException
Removes all of this list's elements that are also contained in the specified collection.

Specified by:
removeAll in interface java.util.Collection<E>
Specified by:
removeAll in interface java.util.List<E>
Overrides:
removeAll in class java.util.ArrayList<E>
Throws:
java.lang.UnsupportedOperationException - if this collection is unmodifiable.

retainAll

public boolean retainAll(java.util.Collection<?> c)
                  throws java.lang.UnsupportedOperationException
Retains only the elements in this list that are contained in the specified collection.

Specified by:
retainAll in interface java.util.Collection<E>
Specified by:
retainAll in interface java.util.List<E>
Overrides:
retainAll in class java.util.ArrayList<E>
Throws:
java.lang.UnsupportedOperationException - if this collection is unmodifiable.

trimToSize

public void trimToSize()
Trims the capacity to the list's current size.

Overrides:
trimToSize in class java.util.ArrayList<E>

ensureCapacity

public void ensureCapacity(int minCapacity)
Increases the capacity, if necessary, to ensure that it can hold the given number of elements.

Overrides:
ensureCapacity in class java.util.ArrayList<E>

clear

public void clear()
           throws java.lang.UnsupportedOperationException
Removes all of the elements from this list.

Specified by:
clear in interface java.util.Collection<E>
Specified by:
clear in interface java.util.List<E>
Overrides:
clear in class java.util.ArrayList<E>
Throws:
java.lang.UnsupportedOperationException - if this collection is unmodifiable.

toArray

public java.lang.Object[] toArray()
Returns an array containing all of the elements in this list.

Specified by:
toArray in interface java.util.Collection<E>
Specified by:
toArray in interface java.util.List<E>
Overrides:
toArray in class java.util.ArrayList<E>

toArray

public <T> T[] toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence.

Specified by:
toArray in interface java.util.Collection<E>
Specified by:
toArray in interface java.util.List<E>
Overrides:
toArray in class java.util.ArrayList<E>
Type Parameters:
T - The type of array elements.

toString

public java.lang.String toString()
Returns a string representation of this list.

Overrides:
toString in class java.util.AbstractCollection<E>

equals

public boolean equals(java.lang.Object o)
Compares the specified object with this list for equality.

Specified by:
equals in interface java.util.Collection<E>
Specified by:
equals in interface java.util.List<E>
Overrides:
equals in class java.util.AbstractList<E>

hashCode

public int hashCode()
Returns the hash code value for this list.

Specified by:
hashCode in interface java.util.Collection<E>
Specified by:
hashCode in interface java.util.List<E>
Overrides:
hashCode in class java.util.AbstractList<E>

clone

public CheckedArrayList<E> clone()
Returns a shallow copy of this list.

Overrides:
clone in class java.util.ArrayList<E>
Returns:
A shallow copy of this list.


Copyright © 1996-2010 Geotools. All Rights Reserved.