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