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 classA view over a portion ofUnmodifiableArrayList. -
Field Summary
FieldsModifier and TypeFieldDescription(package private) final E[]The wrapped array.private static final longFor compatibility with different versions.Fields inherited from class java.util.AbstractList
modCount -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedUnmodifiableArrayList(E[] array) Creates a new instance wrapping the given array. -
Method Summary
Modifier and TypeMethodDescriptionfinal intReturns the size of the array backing this list.booleanReturnstrueif this list contains the specified element.booleanCompares 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.intReturns the index in this list of the first occurrence of the specified element, or -1 if the list does not contain the element.intlastIndexOf(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) intlower()Returns the index of the first valid element.intsize()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, setMethods inherited from class java.util.AbstractCollection
addAll, containsAll, isEmpty, remove, removeAll, retainAll, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, streamMethods 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, ornullif none.- Returns:
- the given array wrapped in an unmodifiable list, or
nullif 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:
getElementTypein 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.SubListonly. -
size
public int size()Returns the list size.- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceList<E>- Specified by:
sizein 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
UnmodifiableArrayListinstance 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:
lastIndexOfin interfaceList<E>- Overrides:
lastIndexOfin classAbstractList<E>- Parameters:
object- the element to search for.- Returns:
- the index of the last occurrence of the given object, or
-1.
-
contains
Returnstrueif this list contains the specified element.- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceList<E>- Overrides:
containsin classAbstractCollection<E>- Parameters:
object- the element to check for existence.- Returns:
trueif this collection contains the given element.
-
subList
Returns a view of the portion of this list between the specifiedlower, inclusive, andupper, exclusive.- Specified by:
subListin interfaceList<E>- Overrides:
subListin 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 whatArrayListdoes, but is not forbidden byList.toArray()javadoc neither.- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceList<E>- Overrides:
toArrayin 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:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceList<E>- Overrides:
toArrayin 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:
equalsin interfaceCollection<E>- Specified by:
equalsin interfaceList<E>- Overrides:
equalsin classAbstractList<E>- Parameters:
object- the object to compare with this list.- Returns:
trueif the given object is equal to this list.
-