Class UnmodifiableArrayList<E>
- Type Parameters:
E
- the type of elements in the list.
- All Implemented Interfaces:
Serializable
,Iterable<E>
,Collection<E>
,List<E>
,RandomAccess
,CheckedContainer<E>
- Direct Known Subclasses:
DefaultParameterDescriptorGroup.AsList
,UnmodifiableArrayList.SubList
CheckedContainer
interface.
WARNING! Type safety hole
ThegetElementType()
return type is Class<E>
, but its implementation actually
returns Class<? extends E>
. This contract violation is possible because Java arrays are
covariant (at the contrary of collections). In order to avoid such contract violation, callers
must ensure that the type of array elements in exactly E
, not a subtype
of E
. This class has no way to verify that condition. This class is not in the public API
for this reason.
Note that the public API, Containers.unmodifiableList(Object[])
,
returns List<? extends E>
, which is okay.
- Since:
- 0.3
- Version:
- 0.3
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
A view over a portion ofUnmodifiableArrayList
. -
Field Summary
FieldsModifier and TypeFieldDescription(package private) final E[]
The wrapped array.private static final long
For compatibility with different versions.Fields inherited from class java.util.AbstractList
modCount
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
UnmodifiableArrayList
(E[] array) Creates a new instance wrapping the given array. -
Method Summary
Modifier and TypeMethodDescriptionfinal int
Returns the size of the array backing this list.boolean
Returnstrue
if this list contains the specified element.boolean
Compares this list with the given object for equality.get
(int index) Returns the element at the specified index.Returns the element type of the wrapped array.int
Returns the index in this list of the first occurrence of the specified element, or -1 if the list does not contain the 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 the element.(package private) int
lower()
Returns the index of the first valid element.int
size()
Returns the list size.subList
(int lower, int upper) Returns a view of the portion of this list between the specifiedlower
, inclusive, andupper
, exclusive.E[]
toArray()
Returns a copy of the backing array.<T> T[]
toArray
(T[] dest) Copies the backing array in the given one if the list fits in the given array.static <E> UnmodifiableArrayList<E>
wrap
(E[] array) Creates a new instance wrapping the given array.static <E> UnmodifiableArrayList<E>
wrap
(E[] array, int lower, int upper) Creates a new instance wrapping a subregion of the given array.Methods inherited from class java.util.AbstractList
add, add, addAll, clear, hashCode, iterator, listIterator, listIterator, remove, removeRange, set
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, isEmpty, remove, removeAll, retainAll, toString
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
Methods inherited from interface java.util.List
addAll, containsAll, isEmpty, remove, removeAll, replaceAll, retainAll, sort, spliterator
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDFor compatibility with different versions.- See Also:
-
array
The wrapped array.
-
-
Constructor Details
-
UnmodifiableArrayList
Creates a new instance wrapping the given array. A direct reference to the given array is retained (i.e. the array is not cloned). Consequently, the given array shall not be modified after construction if this list is intended to be immutable.This constructor is for sub-classing only. Users should invoke the
wrap(Object[])
static method instead.The argument type is intentionally
E[]
instead ofE...
in order to force the caller to instantiate the array explicitly, in order to make sure that the array type is the intended one.WARNING! Type safety hole
Callers must ensure that the type of array elements in exactlyE
, not a subtype ofE
. See class javadoc for more information.- Parameters:
array
- the array to wrap.
-
-
Method Details
-
wrap
Creates a new instance wrapping the given array. A direct reference to the given array is retained (i.e. the array is not cloned). Consequently, the given array shall not be modified after construction if the returned list is intended to be immutable.WARNING! Type safety hole
Callers must ensure that the type of array elements in exactlyE
, not a subtype ofE
. If the caller is okay withList<? extends E>
, then (s)he should useContainers.unmodifiableList(Object[])
instead. See class javadoc for more information.The argument type is intentionally
E[]
instead ofE...
in order to force the caller to instantiate the array explicitly, in order to make sure that the array type is the intended one.- Type Parameters:
E
- the type of elements in the list.- Parameters:
array
- the array to wrap, ornull
if none.- Returns:
- the given array wrapped in an unmodifiable list, or
null
if the given array was null.
-
wrap
Creates a new instance wrapping a subregion of the given array. A direct reference to the given array is retained (i.e. the array is not cloned). Consequently, the specified sub-region of the given array shall not be modified after construction if the returned list is intended to be immutable.This method does not check the validity of the given index. The check must be done by the caller.
WARNING! Type safety hole
Callers must ensure that the type of array elements in exactlyE
, not a subtype ofE
. If the caller is okay withList<? extends E>
, then (s)he should useContainers.unmodifiableList(Object[])
instead. See class javadoc for more information.- 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.
-
getElementType
Returns the element type of the wrapped array. The default implementation returnsarray.getClass().getComponentType()
.- Specified by:
getElementType
in interfaceCheckedContainer<E>
- Returns:
- the type of elements in the list.
-
lower
int lower()Returns the index of the first valid element. To be overridden byUnmodifiableArrayList.SubList
only. -
size
public int size()Returns the list size.- Specified by:
size
in interfaceCollection<E>
- Specified by:
size
in interfaceList<E>
- Specified by:
size
in classAbstractCollection<E>
- Returns:
- the size of this list.
-
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 tosize()
except if this instance is a sublist, in which case the value returned by this method is greater thansize()
.This method can be used as a hint for choosing a
UnmodifiableArrayList
instance to keep when there is a choice between many equal instances. 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.
-
get
Returns the element at the specified index. -
indexOf
Returns the index in this list of the first occurrence of the specified element, or -1 if the list does not contain the element. -
lastIndexOf
Returns the index in this list of the last occurrence of the specified element, or -1 if the list does not contain the element.- Specified by:
lastIndexOf
in interfaceList<E>
- Overrides:
lastIndexOf
in classAbstractList<E>
- Parameters:
object
- the element to search for.- Returns:
- the index of the last occurrence of the given object, or
-1
.
-
contains
Returnstrue
if this list contains the specified element.- Specified by:
contains
in interfaceCollection<E>
- Specified by:
contains
in interfaceList<E>
- Overrides:
contains
in classAbstractCollection<E>
- Parameters:
object
- the element to check for existence.- Returns:
true
if this collection contains the given element.
-
subList
Returns a view of the portion of this list between the specifiedlower
, inclusive, andupper
, exclusive.- Specified by:
subList
in interfaceList<E>
- Overrides:
subList
in classAbstractList<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.- See Also:
-
toArray
Returns a copy of the backing array. Note that the array type isE[]
rather thanObject[]
. This is not whatArrayList
does, but is not forbidden byList.toArray()
javadoc neither.- Specified by:
toArray
in interfaceCollection<E>
- Specified by:
toArray
in interfaceList<E>
- Overrides:
toArray
in classAbstractCollection<E>
- Returns:
- a copy of the wrapped array.
-
toArray
public <T> T[] toArray(T[] dest) Copies the backing array in the given one if the list fits in the given array. If the list does not fit in the given array, returns the collection in a new array.- Specified by:
toArray
in interfaceCollection<E>
- Specified by:
toArray
in interfaceList<E>
- Overrides:
toArray
in classAbstractCollection<E>
- Type Parameters:
T
- the type of array element.- Parameters:
dest
- the array where to copy the elements if the list can fits in the array.- Returns:
- the given array, or a newly created array if this list is larger than the given array.
-
equals
Compares this list with the given object for equality.- Specified by:
equals
in interfaceCollection<E>
- Specified by:
equals
in interfaceList<E>
- Overrides:
equals
in classAbstractList<E>
- Parameters:
object
- the object to compare with this list.- Returns:
true
if the given object is equal to this list.
-