View Javadoc
1   package org.djutils.immutablecollections;
2   
3   import java.util.Collection;
4   import java.util.List;
5   import java.util.RandomAccess;
6   import java.util.Spliterator;
7   import java.util.function.Consumer;
8   import java.util.stream.Stream;
9   
10  import org.djutils.exceptions.Throw;
11  
12  /**
13   * An abstract base class for an immutable wrapper for a List.
14   * <p>
15   * Copyright (c) 2016-2022 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
16   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
17   * distributed under a three-clause BSD-style license, which can be found at
18   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>.
19   * </p>
20   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
21   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
22   * @param <E> the type of content of this List
23   */
24  public abstract class ImmutableAbstractList<E> extends ImmutableAbstractCollection<E> implements ImmutableList<E>, RandomAccess
25  {
26      /** */
27      private static final long serialVersionUID = 20160507L;
28  
29      /** the list that is wrapped, without giving access to methods that can change it. */
30      private final List<E> list;
31  
32      /**
33       * Construct an abstract immutable list. Make sure that the argument is a safe copy of the list or pointer to the list of
34       * the right type! Copying does not take place in the Abstract class!
35       * @param list List&lt;E&gt;; a safe copy of the list, or pointer to the list to use for the immutable list
36       * @param copyOrWrap Immutable; indicate whether the immutable is a copy or a wrap
37       */
38      protected ImmutableAbstractList(final List<E> list, final Immutable copyOrWrap)
39      {
40          super(copyOrWrap);
41          Throw.whenNull(list, "the list argument cannot be null");
42          this.list = list;
43      }
44  
45      /** {@inheritDoc} */
46      @Override
47      public final Collection<E> toCollection()
48      {
49          return toList();
50      }
51  
52      /** {@inheritDoc} */
53      @Override
54      protected List<E> getUnderlyingCollection()
55      {
56          return this.list;
57      }
58  
59      /** {@inheritDoc} */
60      @Override
61      public final int size()
62      {
63          return this.list.size();
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      public final boolean isEmpty()
69      {
70          return this.list.isEmpty();
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      public final boolean contains(final Object o)
76      {
77          return this.list.contains(o);
78      }
79  
80      /** {@inheritDoc} */
81      @Override
82      public final int indexOf(final Object o)
83      {
84          return this.list.indexOf(o);
85      }
86  
87      /** {@inheritDoc} */
88      @Override
89      public final int lastIndexOf(final Object o)
90      {
91          return this.list.lastIndexOf(o);
92      }
93  
94      /** {@inheritDoc} */
95      @Override
96      public final Object[] toArray()
97      {
98          return this.list.toArray();
99      }
100 
101     /** {@inheritDoc} */
102     @Override
103     public final <T> T[] toArray(final T[] a)
104     {
105         return this.list.toArray(a);
106     }
107 
108     /** {@inheritDoc} */
109     @Override
110     public final E get(final int index)
111     {
112         return this.list.get(index);
113     }
114 
115     /** {@inheritDoc} */
116     @Override
117     public final ImmutableIterator<E> iterator()
118     {
119         return new ImmutableIterator<E>(this.list.iterator());
120     }
121 
122     /** {@inheritDoc} */
123     @Override
124     public final void forEach(final Consumer<? super E> action)
125     {
126         this.list.forEach(action);
127     }
128 
129     /** {@inheritDoc} */
130     @Override
131     public final Spliterator<E> spliterator()
132     {
133         return this.list.spliterator();
134     }
135 
136     /** {@inheritDoc} */
137     @Override
138     public final boolean containsAll(final Collection<?> c)
139     {
140         return this.list.containsAll(c);
141     }
142 
143     /** {@inheritDoc} */
144     @Override
145     public final boolean containsAll(final ImmutableCollection<?> c)
146     {
147         return this.list.containsAll(c.toCollection());
148     }
149 
150     /** {@inheritDoc} */
151     @Override
152     public final Stream<E> stream()
153     {
154         return this.list.stream();
155     }
156 
157     /** {@inheritDoc} */
158     @Override
159     public final Stream<E> parallelStream()
160     {
161         return this.list.parallelStream();
162     }
163 
164     /** {@inheritDoc} */
165     @Override
166     public final boolean isWrap()
167     {
168         return this.copyOrWrap.isWrap();
169     }
170 
171     /** {@inheritDoc} */
172     @Override
173     @SuppressWarnings("checkstyle:designforextension")
174     public int hashCode()
175     {
176         final int prime = 31;
177         int result = 1;
178         result = prime * result + ((this.list == null) ? 0 : this.list.hashCode());
179         return result;
180     }
181 
182     /** {@inheritDoc} */
183     @Override
184     @SuppressWarnings({"checkstyle:designforextension", "checkstyle:needbraces"})
185     public boolean equals(final Object obj)
186     {
187         if (this == obj)
188             return true;
189         if (obj == null)
190             return false;
191         if (getClass() != obj.getClass())
192             return false;
193         ImmutableAbstractList<?> other = (ImmutableAbstractList<?>) obj;
194         if (this.list == null)
195         {
196             if (other.list != null)
197                 return false;
198         }
199         else if (!this.list.equals(other.list))
200             return false;
201         return true;
202     }
203 
204 }