Package org.djutils.immutablecollections
Class ImmutableVector<E>
java.lang.Object
org.djutils.immutablecollections.ImmutableAbstractCollection<E>
org.djutils.immutablecollections.ImmutableAbstractList<E>
org.djutils.immutablecollections.ImmutableVector<E>
- Type Parameters:
E
- the type of content of this ImmutableVector
- All Implemented Interfaces:
Serializable
,Iterable<E>
,RandomAccess
,ImmutableCollection<E>
,ImmutableList<E>
public class ImmutableVector<E> extends ImmutableAbstractList<E>
An immutable wrapper for a Vector.
Copyright (c) 2016-2020 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information https://djutils.org. The DJUTILS project is distributed under a three-clause BSD-style license, which can be found at https://djutils.org/docs/license.html.
- Author:
- Alexander Verbraeck, Peter Knoppers, Wouter Schakel
- See Also:
- Serialized Form
-
Field Summary
-
Constructor Summary
Constructors Constructor Description ImmutableVector(Collection<? extends E> collection)
ImmutableVector(Vector<E> vector, Immutable copyOrWrap)
ImmutableVector(ImmutableAbstractCollection<? extends E> collection)
ImmutableVector(ImmutableVector<E> vector, Immutable copyOrWrap)
-
Method Summary
Modifier and Type Method Description int
capacity()
Returns the current capacity of this immutable vector.void
copyInto(Object[] anArray)
Copies the components of this immutable vector into the specified array.E
elementAt(int index)
Returns the component at the specified index.Enumeration<E>
elements()
Returns an enumeration of the components of this vector.E
firstElement()
Returns the first component (the item at index0
) of this immutable vector.protected Vector<E>
getUnderlyingCollection()
Returns the underlying collection of this immutable collection.int
indexOf(Object o, int index)
Returns the index of the first occurrence of the specified element in this immutable vector, searching forwards fromindex
, or returns -1 if the element is not found.E
lastElement()
Returns the last component of the immutable vector.int
lastIndexOf(Object o, int index)
Returns the index of the last occurrence of the specified element in this immutable vector, searching backwards fromindex
, or returns -1 if the element is not found.ImmutableList<E>
subList(int fromIndex, int toIndex)
Returns a safe, immutable copy of the portion of this immutable list between the specifiedfromIndex
, inclusive, andtoIndex
, exclusive.List<E>
toList()
Returns a modifiable copy of this immutable list.String
toString()
Force to redefine toString.Vector<E>
toVector()
Returns a modifiable copy of this immutable vector.Methods inherited from class org.djutils.immutablecollections.ImmutableAbstractList
contains, containsAll, containsAll, equals, forEach, get, hashCode, indexOf, isEmpty, isWrap, iterator, lastIndexOf, parallelStream, size, spliterator, stream, toArray, toArray, toCollection
-
Constructor Details
-
ImmutableVector
- Parameters:
collection
- Collection<? extends E>; the collection to use for the immutable vector.
-
ImmutableVector
- Parameters:
vector
- Vector<E>; the vector to use for the immutable vector.copyOrWrap
- COPY stores a safe, internal copy of the collection; WRAP stores a pointer to the original collection
-
ImmutableVector
- Parameters:
collection
- ImmutableAbstractCollection<? extends E>; the immutable collection to use for the immutable vector.
-
ImmutableVector
- Parameters:
vector
- ImmutableVector<E>; the vector to use for the immutable vector.copyOrWrap
- COPY stores a safe, internal copy of the collection; WRAP stores a pointer to the original collection
-
-
Method Details
-
toList
Returns a modifiable copy of this immutable list.- Returns:
- a modifiable copy of this immutable list.
-
toVector
Returns a modifiable copy of this immutable vector.- Returns:
- a modifiable copy of this immutable vector.
-
getUnderlyingCollection
Returns the underlying collection of this immutable collection. In case of Immutable.WRAP, this will be the original collection. In case of IMMUTABLE.COPY, this will be the internally stored (mutable) copy of the collection.- Overrides:
getUnderlyingCollection
in classImmutableAbstractList<E>
- Returns:
- the underlying collection of this immutable collection.
-
subList
Returns a safe, immutable copy of the portion of this immutable list between the specifiedfromIndex
, inclusive, andtoIndex
, exclusive. (IffromIndex
andtoIndex
are equal, the returned immutable list is empty).- Parameters:
fromIndex
- int; low endpoint (inclusive) of the subListtoIndex
- int; high endpoint (exclusive) of the subList- Returns:
- a view of the specified range within this immutable list
-
copyInto
Copies the components of this immutable vector into the specified array. The item at indexk
in this immutable vector is copied into componentk
ofanArray
.- Parameters:
anArray
- Object[]; the array into which the components get copied- Throws:
NullPointerException
- if the given array is nullIndexOutOfBoundsException
- if the specified array is not large enough to hold all the components of this immutable vectorArrayStoreException
- if a component of this immutable vector is not of a runtime type that can be stored in the specified array- See Also:
ImmutableAbstractList.toArray(Object[])
-
capacity
public final int capacity()Returns the current capacity of this immutable vector.- Returns:
- the current capacity of this immutable vector.
-
elements
Returns an enumeration of the components of this vector. The returnedEnumeration
object will generate all items in this vector. The first item generated is the item at index0
, then the item at index1
, and so on.- Returns:
- an enumeration of the components of this vector
- See Also:
Iterator
-
indexOf
Returns the index of the first occurrence of the specified element in this immutable vector, searching forwards fromindex
, or returns -1 if the element is not found. More formally, returns the lowest indexi
such that(i >= index && (o==null ? get(i)==null : o.equals(get(i))))
, or -1 if there is no such index.- Parameters:
o
- Object; element to search forindex
- int; index to start searching from- Returns:
- the index of the first occurrence of the element in this immutable vector at position
index
or later in the vector;-1
if the element is not found. - Throws:
IndexOutOfBoundsException
- if the specified index is negative- See Also:
Object.equals(Object)
-
lastIndexOf
Returns the index of the last occurrence of the specified element in this immutable vector, searching backwards fromindex
, or returns -1 if the element is not found. More formally, returns the highest indexi
such that(i <= index && (o==null ? get(i)==null : o.equals(get(i))))
, or -1 if there is no such index.- Parameters:
o
- Object; element to search forindex
- int; index to start searching backwards from- Returns:
- the index of the last occurrence of the element at position less than or equal to
index
in this immutable vector; -1 if the element is not found. - Throws:
IndexOutOfBoundsException
- if the specified index is greater than or equal to the current size of this immutable vector
-
elementAt
Returns the component at the specified index.This method is identical in functionality to the
ImmutableAbstractList.get(int)
method (which is part of theList
interface).- Parameters:
index
- int; an index into this immutable vector- Returns:
- the component at the specified index
- Throws:
ArrayIndexOutOfBoundsException
- if the index is out of range (index < 0 || index >= size()
)
-
firstElement
Returns the first component (the item at index0
) of this immutable vector.- Returns:
- the first component of this immutable vector
- Throws:
NoSuchElementException
- if this immutable vector has no components
-
lastElement
Returns the last component of the immutable vector.- Returns:
- the last component of the immutable vector, i.e., the component at index
size() - 1
. - Throws:
NoSuchElementException
- if this immutable vector is empty
-
toString
Force to redefine toString.- Specified by:
toString
in interfaceImmutableList<E>
- Overrides:
toString
in classObject
- Returns:
- String; a description of this immutable list
-