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