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