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
,SequencedCollection<E>
,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 class
A wrapper around the given array for use byaddAll(Collection)
only. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final long
Serial 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 TypeMethodDescriptionvoid
Inserts the specified element at the specified position in this list.boolean
Appends the specified element to the end of this list.boolean
addAll
(int index, Collection<? extends E> collection) Inserts all of the elements in the specified collection into this list, starting at the specified position.boolean
addAll
(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 aCheckedArrayList
instance of the given element type.private boolean
ensureValid
(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 String
illegalElement
(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
addFirst, addLast, clear, clone, contains, ensureCapacity, equals, forEach, get, getFirst, getLast, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, removeFirst, removeIf, removeLast, removeRange, replaceAll, retainAll, size, sort, spliterator, toArray, toArray, trimToSize
Methods inherited from class java.util.AbstractCollection
containsAll, toString
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, stream, toArray
Methods inherited from interface java.util.List
containsAll, reversed
-
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 aCheckedArrayList
instance 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
, ornull
if 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:
getElementType
in 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 returnfalse
without 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
String
containing 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
null
if 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:
true
if the instance is valid,false
if 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:
set
in interfaceList<E>
- Overrides:
set
in 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:
add
in interfaceCollection<E>
- Specified by:
add
in interfaceList<E>
- Overrides:
add
in 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:
add
in interfaceList<E>
- Overrides:
add
in 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:
addAll
in interfaceCollection<E>
- Specified by:
addAll
in interfaceList<E>
- Overrides:
addAll
in classArrayList<E>
- Parameters:
collection
- the elements to be inserted into this list.- Returns:
true
if 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:
addAll
in interfaceList<E>
- Overrides:
addAll
in classArrayList<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:
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 ofnull
values.
-