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 private static final long serialVersionUID = 20160507L;
26
27 /**
28 * @param collection the collection to use for the immutable vector.
29 */
30 public ImmutableVector(final Collection<? extends E> collection)
31 {
32 super(new Vector<E>(collection), Immutable.COPY);
33 }
34
35 /**
36 * @param vector the vector to use for the immutable vector.
37 * @param copyOrWrap WRAP stores a pointer to the original collection
38 */
39 public ImmutableVector(final Vector<E> vector, final Immutable copyOrWrap)
40 {
41 super(copyOrWrap == Immutable.COPY ? new Vector<E>(vector) : vector, copyOrWrap);
42 }
43
44 /**
45 * @param collection the immutable collection to use for the immutable
46 * vector.
47 */
48 public ImmutableVector(final ImmutableAbstractCollection<? extends E> collection)
49 {
50 super(new Vector<E>(collection.getUnderlyingCollection()), Immutable.COPY);
51 }
52
53 /**
54 * @param vector the vector to use for the immutable vector.
55 * @param copyOrWrap WRAP stores a pointer to the original collection
56 */
57 public ImmutableVector(final ImmutableVector<E> vector, final Immutable copyOrWrap)
58 {
59 this(copyOrWrap == Immutable.COPY ? new Vector<E>(vector.getUnderlyingCollection()) : vector.getUnderlyingCollection(),
60 copyOrWrap);
61 }
62
63 @Override
64 public final List<E> toList()
65 {
66 return new Vector<E>(getUnderlyingCollection());
67 }
68
69 /**
70 * Returns a modifiable copy of this immutable vector.
71 * @return a modifiable copy of this immutable vector.
72 */
73 public final Vector<E> toVector()
74 {
75 return new Vector<E>(getUnderlyingCollection());
76 }
77
78 @Override
79 protected Vector<E> getUnderlyingCollection()
80 {
81 return (Vector<E>) super.getUnderlyingCollection();
82 }
83
84 @Override
85 public final ImmutableList<E> subList(final int fromIndex, final int toIndex)
86 {
87 return new ImmutableVector<E>(getUnderlyingCollection().subList(fromIndex, toIndex));
88 }
89
90 /**
91 * Copies the components of this immutable vector into the specified array. The item at index {@code k} in this immutable
92 * vector is copied into component {@code k} of {@code anArray}.
93 * @param anArray the array into which the components get copied
94 * @throws NullPointerException if the given array is null
95 * @throws IndexOutOfBoundsException if the specified array is not large enough to hold all the components of this immutable
96 * vector
97 * @throws ArrayStoreException if a component of this immutable vector is not of a runtime type that can be stored in the
98 * specified array
99 * @see #toArray(Object[])
100 */
101 public final void copyInto(final Object[] anArray)
102 {
103 getUnderlyingCollection().copyInto(anArray);
104 }
105
106 /**
107 * Returns the current capacity of this immutable vector.
108 * @return the current capacity of this immutable vector.
109 */
110 public final int capacity()
111 {
112 return getUnderlyingCollection().capacity();
113 }
114
115 /**
116 * Returns an enumeration of the components of this vector. The returned {@code Enumeration} object will generate all items
117 * in this vector. The first item generated is the item at index {@code 0}, then the item at index {@code 1}, and so on.
118 * @return an enumeration of the components of this vector
119 * @see Iterator
120 */
121 public final Enumeration<E> elements()
122 {
123 return getUnderlyingCollection().elements();
124 }
125
126 /**
127 * Returns the index of the first occurrence of the specified element in this immutable vector, searching forwards from
128 * {@code index}, or returns -1 if the element is not found. More formally, returns the lowest index {@code i} such that
129 * <code>(i >= index && (o==null ? get(i)==null : o.equals(get(i))))</code>,
130 * or -1 if there is no such index.
131 * @param o element to search for
132 * @param index index to start searching from
133 * @return the index of the first occurrence of the element in this immutable vector at position {@code index} or later in
134 * the vector; {@code -1} if the element is not found.
135 * @throws IndexOutOfBoundsException if the specified index is negative
136 * @see Object#equals(Object)
137 */
138 public final int indexOf(final Object o, final int index)
139 {
140 return getUnderlyingCollection().indexOf(o, index);
141 }
142
143 /**
144 * Returns the index of the last occurrence of the specified element in this immutable vector, searching backwards from
145 * {@code index}, or returns -1 if the element is not found. More formally, returns the highest index {@code i} such that
146 * <code>(i <= index && (o==null ? get(i)==null : o.equals(get(i))))</code>,
147 * or -1 if there is no such index.
148 * @param o element to search for
149 * @param index index to start searching backwards from
150 * @return the index of the last occurrence of the element at position less than or equal to {@code index} in this immutable
151 * vector; -1 if the element is not found.
152 * @throws IndexOutOfBoundsException if the specified index is greater than or equal to the current size of this immutable
153 * vector
154 */
155 public final int lastIndexOf(final Object o, final int index)
156 {
157 return getUnderlyingCollection().lastIndexOf(o, index);
158 }
159
160 /**
161 * Returns the component at the specified index.
162 * <p>
163 * This method is identical in functionality to the {@link #get(int)} method (which is part of the {@link List} interface).
164 * @param index an index into this immutable vector
165 * @return the component at the specified index
166 * @throws ArrayIndexOutOfBoundsException if the index is out of range ({@code index < 0 || index >= size()})
167 */
168 public final E elementAt(final int index)
169 {
170 return getUnderlyingCollection().elementAt(index);
171 }
172
173 /**
174 * Returns the first component (the item at index {@code 0}) of this immutable vector.
175 * @return the first component of this immutable vector
176 * @throws NoSuchElementException if this immutable vector has no components
177 */
178 public final E firstElement()
179 {
180 return getUnderlyingCollection().firstElement();
181 }
182
183 /**
184 * Returns the last component of the immutable vector.
185 * @return the last component of the immutable vector, i.e., the component at index <code>size() - 1</code>.
186 * @throws NoSuchElementException if this immutable vector is empty
187 */
188 public final E lastElement()
189 {
190 return getUnderlyingCollection().lastElement();
191 }
192
193 @Override
194 public final String toString()
195 {
196 List<E> list = getUnderlyingCollection();
197 if (null == list)
198 {
199 return "ImmutableVector []";
200 }
201 return "ImmutableVector [" + list.toString() + "]";
202 }
203
204 }