View Javadoc
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   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck
16   * $, initial version May 7, 2016 <br>
17   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
18   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
19   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
20   * @param <E> the type of content of this List
21   */
22  public class ImmutableArrayList<E> extends ImmutableAbstractList<E>
23  {
24      /** */
25      private static final long serialVersionUID = 20160507L;
26  
27      /**
28       * @param collection Collection&lt;? extends E&gt;; the collection to use for the immutable list.
29       */
30      public ImmutableArrayList(final Collection<? extends E> collection)
31      {
32          super(new ArrayList<E>(collection), Immutable.COPY);
33      }
34  
35      /**
36       * @param list List&lt;E&gt;; the list to use for the immutable list.
37       * @param copyOrWrap COPY stores a safe, internal copy of the collection; WRAP stores a pointer to the original
38       *            collection
39       */
40      public ImmutableArrayList(final List<E> list, final Immutable copyOrWrap)
41      {
42          super(copyOrWrap == Immutable.COPY ? new ArrayList<E>(list) : list, copyOrWrap);
43      }
44  
45      /**
46       * @param collection ImmutableAbstractCollection&lt;? extends E&gt;; the collection to use for the immutable list.
47       */
48      public ImmutableArrayList(final ImmutableAbstractCollection<? extends E> collection)
49      {
50          super(new ArrayList<E>(collection.getCollection()), Immutable.COPY);
51      }
52  
53      /**
54       * @param list ImmutableAbstractList&lt;E&gt;; the list to use for the immutable list.
55       * @param copyOrWrap COPY stores a safe, internal copy of the collection; WRAP stores a pointer to the original
56       *            collection
57       */
58      public ImmutableArrayList(final ImmutableAbstractList<E> list, final Immutable copyOrWrap)
59      {
60          super(copyOrWrap == Immutable.COPY ? new ArrayList<E>(list.getCollection()) : list.getCollection(), copyOrWrap);
61      }
62  
63      /** {@inheritDoc} */
64      @Override
65      public final ArrayList<E> toList()
66      {
67          return new ArrayList<E>(getCollection());
68      }
69  
70      /** {@inheritDoc} */
71      @Override
72      protected List<E> getCollection()
73      {
74          return super.getCollection();
75      }
76  
77      /** {@inheritDoc} */
78      @Override
79      public final ImmutableList<E> subList(final int fromIndex, final int toIndex)
80      {
81          return new ImmutableArrayList<>(getCollection().subList(fromIndex, toIndex));
82      }
83  
84      /** {@inheritDoc} */
85      @Override
86      public final String toString()
87      {
88          List<E> list = getCollection();
89          if (null == list)
90          {
91              return "ImmutableArrayList []";
92          }
93          return "ImmutableArrayList [" + list.toString() + "]";
94      }
95  
96  }