Package org.geotools.util
Class CheckedArrayList<E>
- Object
-
- AbstractCollection<E>
-
- AbstractList<E>
-
- ArrayList<E>
-
- CheckedArrayList<E>
-
- Type Parameters:
E- The type of elements in the list.
- All Implemented Interfaces:
Serializable,Cloneable,Iterable<E>,Collection<E>,List<E>,RandomAccess,Cloneable,CheckedCollection<E>
- Direct Known Subclasses:
XPathUtil.StepList
public class CheckedArrayList<E> extends ArrayList<E> implements CheckedCollection<E>, Cloneable
A checked and synchronizedList. Type checks are performed at run-time in addition of compile-time checks. The synchronization lock can be modified at runtime by overriding thegetLock()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
- Author:
- Jody Garnett (Refractions Research), Martin Desruisseaux (IRD)
- See Also:
Collections.checkedList(java.util.List<E>, java.lang.Class<E>),Collections.synchronizedList(java.util.List<T>), Serialized Form
-
-
Field Summary
-
Fields inherited from class AbstractList
modCount
-
-
Constructor Summary
Constructors Constructor Description CheckedArrayList(Class<E> type)Constructs a list of the specified type.CheckedArrayList(Class<E> type, int capacity)Constructs a list of the specified type and initial capacity.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(int index, E element)Inserts the specified element at the specified position in this list.booleanadd(E element)Appends the specified element to the end of this list.booleanaddAll(int index, Collection<? extends E> collection)Inserts all of the elements in the specified collection into this list, starting at the specified position.booleanaddAll(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.protected voidcheckWritePermission()Checks if changes in this collection are allowed.voidclear()Removes all of the elements from this list.CheckedArrayList<E>clone()Returns a shallow copy of this list.booleancontains(Object o)Returnstrueif this list contains the specified element.voidensureCapacity(int minCapacity)Increases the capacity, if necessary, to ensure that it can hold the given number of elements.protected voidensureValidType(E element)Checks the type of the specified object.booleanequals(Object o)Compares the specified object with this list for equality.Eget(int index)Returns the element at the specified position in this list.Class<E>getElementType()Returns the element type given at construction time.protected ObjectgetLock()Returns the synchronization lock.inthashCode()Returns the hash code value for this list.intindexOf(Object o)Returns the index of the first occurrence of the specified element in this list, or -1 if none.booleanisEmpty()Returnstrueif this list contains no elements.Iterator<E>iterator()Returns an iterator over the elements in this list.intlastIndexOf(Object o)Returns the index of the last occurrence of the specified element in this list, or -1 if none.Eremove(int index)Removes the element at the specified position in this list.booleanremove(Object o)Removes the first occurrence of the specified element from this list.booleanremoveAll(Collection<?> c)Removes all of this list's elements that are also contained in the specified collection.booleanretainAll(Collection<?> c)Retains only the elements in this list that are contained in the specified collection.Eset(int index, E element)Replaces the element at the specified position in this list with the specified element.intsize()Returns the number of elements in this list.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.StringtoString()Returns a string representation of this list.voidtrimToSize()Trims the capacity to the list's current size.-
Methods inherited from class ArrayList
forEach, listIterator, listIterator, removeIf, removeRange, replaceAll, sort, spliterator, subList
-
Methods inherited from class AbstractCollection
containsAll
-
Methods inherited from interface Collection
parallelStream, removeIf, stream, toArray
-
Methods inherited from interface List
containsAll
-
-
-
-
Method Detail
-
getElementType
public Class<E> getElementType()
Returns the element type given at construction time.- Specified by:
getElementTypein interfaceCheckedCollection<E>- Returns:
- The element type.
- Since:
- 2.4
-
ensureValidType
protected void ensureValidType(E element) throws 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, ornull.- Throws:
IllegalArgumentException- if the specified element is not of the expected type.
-
checkWritePermission
protected void checkWritePermission() throws UnsupportedOperationExceptionChecks 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:
UnsupportedOperationException- if this collection is unmodifiable.- Since:
- 2.5
-
getLock
protected Object getLock()
Returns the synchronization lock. The default implementation returnsthis. 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
-
size
public int size()
Returns the number of elements in this list.
-
isEmpty
public boolean isEmpty()
Returnstrueif this list contains no elements.
-
contains
public boolean contains(Object o)
Returnstrueif this list contains the specified element.
-
indexOf
public int indexOf(Object o)
Returns the index of the first occurrence of the specified element in this list, or -1 if none.
-
lastIndexOf
public int lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this list, or -1 if none.- Specified by:
lastIndexOfin interfaceList<E>- Overrides:
lastIndexOfin classArrayList<E>
-
get
public E get(int index)
Returns the element at the specified position in this list.
-
set
public E set(int index, E element) throws IllegalArgumentException, UnsupportedOperationException
Replaces the element at the specified position in this list with the specified element.- Specified by:
setin interfaceList<E>- Overrides:
setin classArrayList<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:
IndexOutOfBoundsException- if index out of range.IllegalArgumentException- if the specified element is not of the expected type.UnsupportedOperationException- if this collection is unmodifiable.
-
add
public boolean add(E element) throws IllegalArgumentException, UnsupportedOperationException
Appends the specified element to the end of this list.- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceList<E>- Overrides:
addin classArrayList<E>- Parameters:
element- element to be appended to this list.- Returns:
- always
true. - Throws:
IllegalArgumentException- if the specified element is not of the expected type.UnsupportedOperationException- if this collection is unmodifiable.
-
add
public void add(int index, E element) throws IllegalArgumentException, UnsupportedOperationExceptionInserts the specified element at the specified position in this list.- Specified by:
addin interfaceList<E>- Overrides:
addin classArrayList<E>- Parameters:
index- index at which the specified element is to be inserted.element- element to be inserted.- Throws:
IndexOutOfBoundsException- if index out of range.IllegalArgumentException- if the specified element is not of the expected type.UnsupportedOperationException- if this collection is unmodifiable.
-
addAll
public boolean addAll(Collection<? extends E> collection) throws IllegalArgumentException, 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:
addAllin interfaceCollection<E>- Specified by:
addAllin interfaceList<E>- Overrides:
addAllin classArrayList<E>- Parameters:
collection- the elements to be inserted into this list.- Returns:
trueif this list changed as a result of the call.- Throws:
IllegalArgumentException- if at least one element is not of the expected type.UnsupportedOperationException- if this collection is unmodifiable.
-
addAll
public boolean addAll(int index, Collection<? extends E> collection) throws IllegalArgumentException, UnsupportedOperationExceptionInserts all of the elements in the specified collection into this list, starting at the specified position.- Specified by:
addAllin interfaceList<E>- Overrides:
addAllin classArrayList<E>- Parameters:
index- index at which to insert first element fromm the specified collection.collection- elements to be inserted into this list.- Returns:
trueif this list changed as a result of the call.- Throws:
IllegalArgumentException- if at least one element is not of the expected type.UnsupportedOperationException- if this collection is unmodifiable.
-
remove
public E remove(int index) throws UnsupportedOperationException
Removes the element at the specified position in this list.
-
remove
public boolean remove(Object o) throws UnsupportedOperationException
Removes the first occurrence of the specified element from this list.- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceList<E>- Overrides:
removein classArrayList<E>- Throws:
UnsupportedOperationException- if this collection is unmodifiable.
-
removeAll
public boolean removeAll(Collection<?> c) throws UnsupportedOperationException
Removes all of this list's elements that are also contained in the specified collection.- Specified by:
removeAllin interfaceCollection<E>- Specified by:
removeAllin interfaceList<E>- Overrides:
removeAllin classArrayList<E>- Throws:
UnsupportedOperationException- if this collection is unmodifiable.
-
retainAll
public boolean retainAll(Collection<?> c) throws UnsupportedOperationException
Retains only the elements in this list that are contained in the specified collection.- Specified by:
retainAllin interfaceCollection<E>- Specified by:
retainAllin interfaceList<E>- Overrides:
retainAllin classArrayList<E>- Throws:
UnsupportedOperationException- if this collection is unmodifiable.
-
trimToSize
public void trimToSize()
Trims the capacity to the list's current size.- Overrides:
trimToSizein classArrayList<E>
-
ensureCapacity
public void ensureCapacity(int minCapacity)
Increases the capacity, if necessary, to ensure that it can hold the given number of elements.- Overrides:
ensureCapacityin classArrayList<E>
-
clear
public void clear() throws UnsupportedOperationExceptionRemoves all of the elements from this list.- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceList<E>- Overrides:
clearin classArrayList<E>- Throws:
UnsupportedOperationException- if this collection is unmodifiable.
-
toArray
public Object[] toArray()
Returns an array containing all of the elements in this list.
-
toArray
public <T> T[] toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence.
-
toString
public String toString()
Returns a string representation of this list.- Overrides:
toStringin classAbstractCollection<E>
-
equals
public boolean equals(Object o)
Compares the specified object with this list for equality.
-
hashCode
public int hashCode()
Returns the hash code value for this list.
-
clone
public CheckedArrayList<E> clone()
Returns a shallow copy of this list.
-
-