Package org.apache.sis.internal.util
Class CheckedArrayList<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
java.util.ArrayList<E>
org.apache.sis.internal.util.CheckedArrayList<E>
- Type Parameters:
E- the type of elements in the list.
- All Implemented Interfaces:
Serializable,Cloneable,Iterable<E>,Collection<E>,List<E>,RandomAccess,CheckedContainer<E>
A checked
ArrayList.
The type checks are performed at run-time in addition to the compile-time checks.
Using this class is similar to wrapping an ArrayList using the methods provided
in the standard Collections class, except for the following differences:
- Avoid one level of indirection.
- Does not accept null elements.
sublist(…).set(…) or when using the list iterator.- Since:
- 0.3
- Version:
- 1.0
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classA wrapper around the given array for use byaddAll(Collection)only. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final longSerial version UID for compatibility with different versions.The element type.Fields inherited from class java.util.AbstractList
modCount -
Constructor Summary
ConstructorsConstructorDescriptionCheckedArrayList(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
Modifier and TypeMethodDescriptionvoidInserts the specified element at the specified position in this list.booleanAppends 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.static <E> CheckedArrayList<E>castOrCopy(Collection<?> collection, Class<E> type) Returns the given collection as aCheckedArrayListinstance of the given element type.private booleanensureValid(E element) Ensures that the given element is non-null and assignable to the type specified at construction time.ensureValidCollection(Collection<? extends E> collection) Ensures that all elements of the given collection can be added to this list.Returns the element type given at construction time.static StringillegalElement(Collection<?> collection, Object element, Class<?> expectedType) Invoked when an illegal element has been given to theadd(E)method.Replaces the element at the specified position in this list with the specified element.subList(int fromIndex, int toIndex) Returns a checked sublist.Methods inherited from class java.util.ArrayList
clear, clone, contains, ensureCapacity, forEach, get, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, removeIf, removeRange, replaceAll, retainAll, size, sort, spliterator, toArray, toArray, trimToSizeMethods inherited from class java.util.AbstractList
equals, hashCodeMethods inherited from class java.util.AbstractCollection
containsAll, toStringMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, streamMethods inherited from interface java.util.List
containsAll, equals, hashCode
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDSerial version UID for compatibility with different versions.- See Also:
-
type
The element type.
-
-
Constructor Details
-
CheckedArrayList
Constructs a list of the specified type.- Parameters:
type- the element type (cannot be null).
-
CheckedArrayList
Constructs a list of the specified type and initial capacity.- Parameters:
type- the element type (should not be null).capacity- the initial capacity.
-
-
Method Details
-
castOrCopy
Returns the given collection as aCheckedArrayListinstance of the given element type.- Type Parameters:
E- the element type.- Parameters:
collection- the collection ornull.type- the element type.- Returns:
- the given collection as a
CheckedArrayList, ornullif the given collection was null. - Throws:
ClassCastException- if an element is not of the expected type.- Since:
- 0.5
-
getElementType
Returns the element type given at construction time.- Specified by:
getElementTypein interfaceCheckedContainer<E>- Returns:
- the element type.
-
illegalElement
public static String illegalElement(Collection<?> collection, Object element, Class<?> expectedType) Invoked when an illegal element has been given to theadd(E)method. The element may be illegal either because null or because of invalid type. This method will perform only one of the following actions:- If a unmarshalling process is under way, then this method logs a warning and returns
null. Theadd(E)caller method shall returnfalsewithout throwing exception. This is a violation ofCollection.add(Object)contract, but is required for unmarshalling of empty XML elements (see SIS-139 and SIS-157). - If no unmarshalling process is under way, then this method returns a
Stringcontaining the error message to give to the exception to be thrown. Theadd(E)caller method is responsible to thrown an exception with that message. We let the caller throw the exception for reducing the stack trace depth, so the first element on the stack trace is the publicadd(E)method.
- Parameters:
collection- the collection in which the user attempted to add an invalid element.element- the element that the user attempted to add (may benull).expectedType- the type of elements that the collection expected.- Returns:
- the message to give to the exception to be thrown, or
nullif no message shall be thrown. - See Also:
- If a unmarshalling process is under way, then this method logs a warning and returns
-
ensureValid
Ensures that the given element is non-null and assignable to the type specified at construction time.- Parameters:
element- the object to check, ornull.- Returns:
trueif the instance is valid,falseif it shall be ignored.- Throws:
NullPointerException- if the given element isnull.ClassCastException- if the given element is not of the expected type.
-
ensureValidCollection
Ensures that all elements of the given collection can be added to this list.- Parameters:
collection- the collection to check, ornull.- Returns:
- the potentially filtered collection of elements to add.
- Throws:
NullPointerException- if an element isnull.ClassCastException- if an element is not of the expected type.
-
set
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.NullPointerException- if the given element isnull.ClassCastException- if the given element is not of the expected type.
-
add
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:
NullPointerException- if the given element isnull.ClassCastException- if the given element is not of the expected type.
-
add
Inserts 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.NullPointerException- if the given element isnull.ClassCastException- if the given element is not of the expected type.
-
addAll
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:
NullPointerException- if an element isnull.ClassCastException- if an element is not of the expected type.
-
addAll
Inserts 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:
NullPointerException- if an element isnull.ClassCastException- if an element is not of the expected type.
-
subList
Returns a checked sublist.Limitations
Current implementation checks only the type. It does not prevent the insertion ofnullvalues.
-