package nl.tudelft.simulation.immutablecollections; import java.util.Collection; import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; import java.util.Vector; /** * An immutable wrapper for a Vector. *

* Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights * reserved.
* BSD-style license. See DSOL License. *

* $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck * $, initial version May 7, 2016
* @author Alexander Verbraeck * @author Peter Knoppers * @author Wouter Schakel * @param the type of content of this List */ public class ImmutableVector extends ImmutableAbstractList { /** */ private static final long serialVersionUID = 20160507L; /** * @param collection the collection to use for the immutable vector. */ public ImmutableVector(final Collection collection) { super(new Vector(collection), Immutable.COPY); } /** * @param vector the vector to use for the immutable vector. * @param copyOrWrap COPY stores a safe, internal copy of the collection; WRAP stores a pointer to the original * collection */ public ImmutableVector(final Vector vector, final Immutable copyOrWrap) { super(copyOrWrap == Immutable.COPY ? new Vector(vector) : vector, copyOrWrap); } /** * @param collection the immutable collection to use for the immutable vector. */ public ImmutableVector(final ImmutableAbstractCollection collection) { super(new Vector(collection.getCollection()), Immutable.COPY); } /** * @param vector the vector to use for the immutable vector. * @param copyOrWrap COPY stores a safe, internal copy of the collection; WRAP stores a pointer to the original * collection */ public ImmutableVector(final ImmutableVector vector, final Immutable copyOrWrap) { this(copyOrWrap == Immutable.COPY ? new Vector(vector.getCollection()) : vector.getCollection(), copyOrWrap); } /** {@inheritDoc} */ @Override public final List toList() { return new Vector(getCollection()); } /** * Returns a modifiable copy of this immutable vector. * @return a modifiable copy of this immutable vector. */ public final Vector toVector() { return new Vector(getCollection()); } /** {@inheritDoc} */ @Override protected Vector getCollection() { return (Vector) super.getCollection(); } /** {@inheritDoc} */ @Override public final ImmutableList subList(final int fromIndex, final int toIndex) { return new ImmutableVector(getCollection().subList(fromIndex, toIndex)); } /** * Copies the components of this immutable vector into the specified array. The item at index {@code k} in this * immutable vector is copied into component {@code k} of {@code anArray}. * @param anArray the array into which the components get copied * @throws NullPointerException if the given array is null * @throws IndexOutOfBoundsException if the specified array is not large enough to hold all the components of this * immutable vector * @throws ArrayStoreException if a component of this immutable vector is not of a runtime type that can be stored * in the specified array * @see #toArray(Object[]) */ public final void copyInto(final Object[] anArray) { getCollection().copyInto(anArray); } /** * Returns the current capacity of this immutable vector. * @return the current capacity of this immutable vector. */ public final int capacity() { return getCollection().capacity(); } /** * Returns an enumeration of the components of this vector. The returned {@code Enumeration} object will generate * all items in this vector. The first item generated is the item at index {@code 0}, then the item at index * {@code 1}, and so on. * @return an enumeration of the components of this vector * @see Iterator */ public final Enumeration elements() { return getCollection().elements(); } /** * Returns the index of the first occurrence of the specified element in this immutable vector, searching forwards * from {@code index}, or returns -1 if the element is not found. More formally, returns the lowest index {@code i} * such that * (i >= index && (o==null ? get(i)==null : o.equals(get(i)))), * or -1 if there is no such index. * @param o element to search for * @param index index to start searching from * @return the index of the first occurrence of the element in this immutable vector at position {@code index} or * later in the vector; {@code -1} if the element is not found. * @throws IndexOutOfBoundsException if the specified index is negative * @see Object#equals(Object) */ public final int indexOf(final Object o, final int index) { return getCollection().indexOf(o, index); } /** * Returns the index of the last occurrence of the specified element in this immutable vector, searching backwards * from {@code index}, or returns -1 if the element is not found. More formally, returns the highest index {@code i} * such that * (i <= index && (o==null ? get(i)==null : o.equals(get(i)))), * or -1 if there is no such index. * @param o element to search for * @param index index to start searching backwards from * @return the index of the last occurrence of the element at position less than or equal to {@code 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 */ public final int lastIndexOf(final Object o, final int index) { return getCollection().lastIndexOf(o, index); } /** * Returns the component at the specified index. *

* This method is identical in functionality to the {@link #get(int)} method (which is part of the {@link List} * interface). * @param index an index into this immutable vector * @return the component at the specified index * @throws ArrayIndexOutOfBoundsException if the index is out of range ({@code index < 0 || index >= size()}) */ public final E elementAt(final int index) { return getCollection().elementAt(index); } /** * Returns the first component (the item at index {@code 0}) of this immutable vector. * @return the first component of this immutable vector * @throws NoSuchElementException if this immutable vector has no components */ public final E firstElement() { return getCollection().firstElement(); } /** * Returns the last component of the immutable vector. * @return the last component of the immutable vector, i.e., the component at index * size() - 1. * @throws NoSuchElementException if this immutable vector is empty */ public final E lastElement() { return getCollection().lastElement(); } /** {@inheritDoc} */ @Override public final String toString() { List list = getCollection(); if (null == list) { return "ImmutableVector []"; } return "ImmutableVector [" + list.toString() + "]"; } }