1 package org.djutils.immutablecollections;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.List;
6
7 /**
8 * An immutable wrapper for an ArrayList.
9 * <p>
10 * Copyright (c) 2016-2019 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
11 * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
12 * distributed under a three-clause BSD-style license, which can be found at
13 * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>.
14 * </p>
15 * $, initial version May 7, 2016 <br>
16 * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
17 * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
18 * @author <a href="https://www.transport.citg.tudelft.nl">Wouter Schakel</a>
19 * @param <E> the type of content of this List
20 */
21 public class ImmutableArrayList<E> extends ImmutableAbstractList<E>
22 {
23 /** */
24 private static final long serialVersionUID = 20160507L;
25
26 /**
27 * @param collection Collection<? extends E>; the collection to use for the immutable list.
28 */
29 public ImmutableArrayList(final Collection<? extends E> collection)
30 {
31 super(new ArrayList<E>(collection), Immutable.COPY);
32 }
33
34 /**
35 * @param list List<E>; the list to use for the immutable list.
36 * @param copyOrWrap COPY stores a safe, internal copy of the collection; WRAP stores a pointer to the original collection
37 */
38 public ImmutableArrayList(final List<E> list, final Immutable copyOrWrap)
39 {
40 super(copyOrWrap == Immutable.COPY ? new ArrayList<E>(list) : list, copyOrWrap);
41 }
42
43 /**
44 * @param collection ImmutableAbstractCollection<? extends E>; the collection to use for the immutable list.
45 */
46 public ImmutableArrayList(final ImmutableAbstractCollection<? extends E> collection)
47 {
48 super(new ArrayList<E>(collection.getCollection()), Immutable.COPY);
49 }
50
51 /**
52 * @param list ImmutableAbstractList<E>; the list to use for the immutable list.
53 * @param copyOrWrap COPY stores a safe, internal copy of the collection; WRAP stores a pointer to the original collection
54 */
55 public ImmutableArrayList(final ImmutableAbstractList<E> list, final Immutable copyOrWrap)
56 {
57 super(copyOrWrap == Immutable.COPY ? new ArrayList<E>(list.getCollection()) : list.getCollection(), copyOrWrap);
58 }
59
60 /** {@inheritDoc} */
61 @Override
62 public final ArrayList<E> toList()
63 {
64 return new ArrayList<E>(getCollection());
65 }
66
67 /** {@inheritDoc} */
68 @Override
69 protected List<E> getCollection()
70 {
71 return super.getCollection();
72 }
73
74 /** {@inheritDoc} */
75 @Override
76 public final ImmutableList<E> subList(final int fromIndex, final int toIndex)
77 {
78 return new ImmutableArrayList<>(getCollection().subList(fromIndex, toIndex));
79 }
80
81 /** {@inheritDoc} */
82 @Override
83 public final String toString()
84 {
85 List<E> list = getCollection();
86 if (null == list)
87 {
88 return "ImmutableArrayList []";
89 }
90 return "ImmutableArrayList [" + list.toString() + "]";
91 }
92
93 }