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

* Copyright (c) 2013-2016 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 list the list to use as the immutable list. */ public ImmutableVector(final List list) { this(list, Immutable.COPY); } /** * @param list the list to use as 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 List list, final Immutable copyOrWrap) { super(copyOrWrap == Immutable.COPY ? new Vector(list) : list, copyOrWrap == Immutable.COPY); Throw.whenNull(copyOrWrap, "the copyOrWrap argument should be Immutable.COPY or Immutable.WRAP"); } /** * @param list the list to use as the immutable list. */ public ImmutableVector(final ImmutableList list) { this(list, Immutable.COPY); } /** * @param list the list to use as the immutable list. * @param copyOrWrap COPY stores a safe, internal copy of the collection; WRAP stores a pointer to the original collection */ public ImmutableVector(final ImmutableList list, final Immutable copyOrWrap) { this(((ImmutableAbstractList) list).getList(), copyOrWrap); } /** {@inheritDoc} */ @Override protected final Vector getList() { return (Vector) super.getList(); } /** {@inheritDoc} */ @Override public final List toList() { return new Vector(getList()); } /** {@inheritDoc} */ @Override public final ImmutableList subList(final int fromIndex, final int toIndex) { return new ImmutableVector(getList().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) { getList().copyInto(anArray); } /** * Returns the current capacity of this immutable vector. * @return the current capacity of this immutable vector. */ public final int capacity() { return getList().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 getList().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 getList().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 getList().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 getList().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 getList().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 getList().lastElement(); } /** {@inheritDoc} */ @Override public final String toString() { List list = getList(); if (null == list) { return "ImmutableVector []"; } return "ImmutableVector [" + list.toString() + "]"; } }