View Javadoc
1   package org.djutils.immutablecollections;
2   
3   import java.util.Collection;
4   import java.util.Enumeration;
5   import java.util.Iterator;
6   import java.util.List;
7   import java.util.NoSuchElementException;
8   import java.util.Vector;
9   
10  /**
11   * An immutable wrapper for a Vector.
12   * <p>
13   * Copyright (c) 2016-2025 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
14   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
15   * distributed under a three-clause BSD-style license, which can be found at
16   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>.
17   * </p>
18   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
19   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
20   * @param <E> the type of content of this ImmutableVector
21   */
22  public class ImmutableVector<E> extends ImmutableAbstractList<E>
23  {
24      /**
25       * @param collection the collection to use for the immutable vector.
26       */
27      public ImmutableVector(final Collection<? extends E> collection)
28      {
29          super(new Vector<E>(collection), Immutable.COPY);
30      }
31  
32      /**
33       * @param vector the vector to use for the immutable vector.
34       * @param copyOrWrap WRAP stores a pointer to the original collection
35       */
36      public ImmutableVector(final Vector<E> vector, final Immutable copyOrWrap)
37      {
38          super(copyOrWrap == Immutable.COPY ? new Vector<E>(vector) : vector, copyOrWrap);
39      }
40  
41      /**
42       * @param collection the immutable collection to use for the immutable
43       *            vector.
44       */
45      public ImmutableVector(final ImmutableAbstractCollection<? extends E> collection)
46      {
47          super(new Vector<E>(collection.getUnderlyingCollection()), Immutable.COPY);
48      }
49  
50      /**
51       * @param vector the vector to use for the immutable vector.
52       * @param copyOrWrap WRAP stores a pointer to the original collection
53       */
54      public ImmutableVector(final ImmutableVector<E> vector, final Immutable copyOrWrap)
55      {
56          this(copyOrWrap == Immutable.COPY ? new Vector<E>(vector.getUnderlyingCollection()) : vector.getUnderlyingCollection(),
57                  copyOrWrap);
58      }
59  
60      @Override
61      public final List<E> toList()
62      {
63          return new Vector<E>(getUnderlyingCollection());
64      }
65  
66      /**
67       * Returns a modifiable copy of this immutable vector.
68       * @return a modifiable copy of this immutable vector.
69       */
70      public final Vector<E> toVector()
71      {
72          return new Vector<E>(getUnderlyingCollection());
73      }
74  
75      @Override
76      protected Vector<E> getUnderlyingCollection()
77      {
78          return (Vector<E>) super.getUnderlyingCollection();
79      }
80  
81      @Override
82      public final ImmutableList<E> subList(final int fromIndex, final int toIndex)
83      {
84          return new ImmutableVector<E>(getUnderlyingCollection().subList(fromIndex, toIndex));
85      }
86  
87      /**
88       * Copies the components of this immutable vector into the specified array. The item at index {@code k} in this immutable
89       * vector is copied into component {@code k} of {@code anArray}.
90       * @param anArray the array into which the components get copied
91       * @throws NullPointerException if the given array is null
92       * @throws IndexOutOfBoundsException if the specified array is not large enough to hold all the components of this immutable
93       *             vector
94       * @throws ArrayStoreException if a component of this immutable vector is not of a runtime type that can be stored in the
95       *             specified array
96       * @see #toArray(Object[])
97       */
98      public final void copyInto(final Object[] anArray)
99      {
100         getUnderlyingCollection().copyInto(anArray);
101     }
102 
103     /**
104      * Returns the current capacity of this immutable vector.
105      * @return the current capacity of this immutable vector.
106      */
107     public final int capacity()
108     {
109         return getUnderlyingCollection().capacity();
110     }
111 
112     /**
113      * Returns an enumeration of the components of this vector. The returned {@code Enumeration} object will generate all items
114      * in this vector. The first item generated is the item at index {@code 0}, then the item at index {@code 1}, and so on.
115      * @return an enumeration of the components of this vector
116      * @see Iterator
117      */
118     public final Enumeration<E> elements()
119     {
120         return getUnderlyingCollection().elements();
121     }
122 
123     /**
124      * Returns the index of the first occurrence of the specified element in this immutable vector, searching forwards from
125      * {@code index}, or returns -1 if the element is not found. More formally, returns the lowest index {@code i} such that
126      * <code>(i&nbsp;&gt;=&nbsp;index&nbsp;&amp;&amp;&nbsp;(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i))))</code>,
127      * or -1 if there is no such index.
128      * @param o element to search for
129      * @param index index to start searching from
130      * @return the index of the first occurrence of the element in this immutable vector at position {@code index} or later in
131      *         the vector; {@code -1} if the element is not found.
132      * @throws IndexOutOfBoundsException if the specified index is negative
133      * @see Object#equals(Object)
134      */
135     public final int indexOf(final Object o, final int index)
136     {
137         return getUnderlyingCollection().indexOf(o, index);
138     }
139 
140     /**
141      * Returns the index of the last occurrence of the specified element in this immutable vector, searching backwards from
142      * {@code index}, or returns -1 if the element is not found. More formally, returns the highest index {@code i} such that
143      * <code>(i&nbsp;&lt;=&nbsp;index&nbsp;&amp;&amp;&nbsp;(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i))))</code>,
144      * or -1 if there is no such index.
145      * @param o element to search for
146      * @param index index to start searching backwards from
147      * @return the index of the last occurrence of the element at position less than or equal to {@code index} in this immutable
148      *         vector; -1 if the element is not found.
149      * @throws IndexOutOfBoundsException if the specified index is greater than or equal to the current size of this immutable
150      *             vector
151      */
152     public final int lastIndexOf(final Object o, final int index)
153     {
154         return getUnderlyingCollection().lastIndexOf(o, index);
155     }
156 
157     /**
158      * Returns the component at the specified index.
159      * <p>
160      * This method is identical in functionality to the {@link #get(int)} method (which is part of the {@link List} interface).
161      * @param index an index into this immutable vector
162      * @return the component at the specified index
163      * @throws ArrayIndexOutOfBoundsException if the index is out of range ({@code index < 0 || index >= size()})
164      */
165     public final E elementAt(final int index)
166     {
167         return getUnderlyingCollection().elementAt(index);
168     }
169 
170     /**
171      * Returns the first component (the item at index {@code 0}) of this immutable vector.
172      * @return the first component of this immutable vector
173      * @throws NoSuchElementException if this immutable vector has no components
174      */
175     public final E firstElement()
176     {
177         return getUnderlyingCollection().firstElement();
178     }
179 
180     /**
181      * Returns the last component of the immutable vector.
182      * @return the last component of the immutable vector, i.e., the component at index <code>size()&nbsp;-&nbsp;1</code>.
183      * @throws NoSuchElementException if this immutable vector is empty
184      */
185     public final E lastElement()
186     {
187         return getUnderlyingCollection().lastElement();
188     }
189 
190     @Override
191     public final String toString()
192     {
193         List<E> list = getUnderlyingCollection();
194         if (null == list)
195         {
196             return "ImmutableVector []";
197         }
198         return "ImmutableVector [" + list.toString() + "]";
199     }
200 
201 }