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-2019 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="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
22   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
23   * @param <E> the type of content of this List
24   */
25  public abstract class ImmutableAbstractList<E> extends ImmutableAbstractCollection<E>
26          implements ImmutableList<E>, RandomAccess
27  {
28      /** */
29      private static final long serialVersionUID = 20160507L;
30  
31      /** the list that is wrapped, without giving access to methods that can change it. */
32      private final List<E> list;
33  
34      /**
35       * Construct an abstract immutable list. Make sure that the argument is a safe copy of the list or pointer to the
36       * list of the right type! Copying does not take place in the Abstract class!
37       * @param list List&lt;E&gt;; a safe copy of the list, or pointer to the list to use for the immutable list
38       * @param copyOrWrap Immutable; indicate whether the immutable is a copy or a wrap
39       */
40      protected ImmutableAbstractList(final List<E> list, final Immutable copyOrWrap)
41      {
42          super(copyOrWrap);
43          Throw.whenNull(list, "the list argument cannot be null");
44          this.list = list;
45      }
46  
47      /** {@inheritDoc} */
48      @Override
49      public final Collection<E> toCollection()
50      {
51          return toList();
52      }
53  
54      /** {@inheritDoc} */
55      @Override
56      protected List<E> getCollection()
57      {
58          return this.list;
59      }
60  
61      /** {@inheritDoc} */
62      @Override
63      public final int size()
64      {
65          return this.list.size();
66      }
67  
68      /** {@inheritDoc} */
69      @Override
70      public final boolean isEmpty()
71      {
72          return this.list.isEmpty();
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      public final boolean contains(final Object o)
78      {
79          return this.list.contains(o);
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      public final int indexOf(final Object o)
85      {
86          return this.list.indexOf(o);
87      }
88  
89      /** {@inheritDoc} */
90      @Override
91      public final int lastIndexOf(final Object o)
92      {
93          return this.list.lastIndexOf(o);
94      }
95  
96      /** {@inheritDoc} */
97      @Override
98      public final Object[] toArray()
99      {
100         return this.list.toArray();
101     }
102 
103     /** {@inheritDoc} */
104     @Override
105     public final <T> T[] toArray(final T[] a)
106     {
107         return this.list.toArray(a);
108     }
109 
110     /** {@inheritDoc} */
111     @Override
112     public final E get(final int index)
113     {
114         return this.list.get(index);
115     }
116 
117     /** {@inheritDoc} */
118     @Override
119     public final ImmutableIterator<E> iterator()
120     {
121         return new ImmutableIterator<E>(this.list.iterator());
122     }
123 
124     /** {@inheritDoc} */
125     @Override
126     public final void forEach(final Consumer<? super E> action)
127     {
128         this.list.forEach(action);
129     }
130 
131     /** {@inheritDoc} */
132     @Override
133     public final Spliterator<E> spliterator()
134     {
135         return this.list.spliterator();
136     }
137 
138     /** {@inheritDoc} */
139     @Override
140     public final boolean containsAll(final Collection<?> c)
141     {
142         return this.list.containsAll(c);
143     }
144 
145     /** {@inheritDoc} */
146     @Override
147     public final boolean containsAll(final ImmutableCollection<?> c)
148     {
149         return this.list.containsAll(c.toCollection());
150     }
151 
152     /** {@inheritDoc} */
153     @Override
154     public final Stream<E> stream()
155     {
156         return this.list.stream();
157     }
158 
159     /** {@inheritDoc} */
160     @Override
161     public final Stream<E> parallelStream()
162     {
163         return this.list.parallelStream();
164     }
165 
166     /** {@inheritDoc} */
167     @Override
168     public final boolean isWrap()
169     {
170         return this.copyOrWrap.isWrap();
171     }
172 
173     /** {@inheritDoc} */
174     @Override
175     @SuppressWarnings("checkstyle:designforextension")
176     public int hashCode()
177     {
178         final int prime = 31;
179         int result = 1;
180         result = prime * result + ((this.list == null) ? 0 : this.list.hashCode());
181         return result;
182     }
183 
184     /** {@inheritDoc} */
185     @Override
186     @SuppressWarnings({"checkstyle:designforextension", "checkstyle:needbraces"})
187     public boolean equals(final Object obj)
188     {
189         if (this == obj)
190             return true;
191         if (obj == null)
192             return false;
193         if (getClass() != obj.getClass())
194             return false;
195         ImmutableAbstractList<?> other = (ImmutableAbstractList<?>) obj;
196         if (this.list == null)
197         {
198             if (other.list != null)
199                 return false;
200         }
201         else if (!this.list.equals(other.list))
202             return false;
203         return true;
204     }
205 
206     /** {@inheritDoc} */
207     @Override
208     @SuppressWarnings("checkstyle:designforextension")
209     public String toString()
210     {
211         return "Immutable[" + this.list.toString() + "]";
212     }
213 }