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-2024 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      @Override
46      public final Collection<E> toCollection()
47      {
48          return toList();
49      }
50  
51      @Override
52      protected List<E> getUnderlyingCollection()
53      {
54          return this.list;
55      }
56  
57      @Override
58      public final int size()
59      {
60          return this.list.size();
61      }
62  
63      @Override
64      public final boolean isEmpty()
65      {
66          return this.list.isEmpty();
67      }
68  
69      @Override
70      public final boolean contains(final Object o)
71      {
72          return this.list.contains(o);
73      }
74  
75      @Override
76      public final int indexOf(final Object o)
77      {
78          return this.list.indexOf(o);
79      }
80  
81      @Override
82      public final int lastIndexOf(final Object o)
83      {
84          return this.list.lastIndexOf(o);
85      }
86  
87      @Override
88      public final Object[] toArray()
89      {
90          return this.list.toArray();
91      }
92  
93      @Override
94      public final <T> T[] toArray(final T[] a)
95      {
96          return this.list.toArray(a);
97      }
98  
99      @Override
100     public final E get(final int index)
101     {
102         return this.list.get(index);
103     }
104 
105     @Override
106     public final ImmutableIterator<E> iterator()
107     {
108         return new ImmutableIterator<E>(this.list.iterator());
109     }
110 
111     @Override
112     public final void forEach(final Consumer<? super E> action)
113     {
114         this.list.forEach(action);
115     }
116 
117     @Override
118     public final Spliterator<E> spliterator()
119     {
120         return this.list.spliterator();
121     }
122 
123     @Override
124     public final boolean containsAll(final Collection<?> c)
125     {
126         return this.list.containsAll(c);
127     }
128 
129     @Override
130     public final boolean containsAll(final ImmutableCollection<?> c)
131     {
132         return this.list.containsAll(c.toCollection());
133     }
134 
135     @Override
136     public final Stream<E> stream()
137     {
138         return this.list.stream();
139     }
140 
141     @Override
142     public final Stream<E> parallelStream()
143     {
144         return this.list.parallelStream();
145     }
146 
147     @Override
148     public final boolean isWrap()
149     {
150         return this.copyOrWrap.isWrap();
151     }
152 
153     @Override
154     @SuppressWarnings("checkstyle:designforextension")
155     public int hashCode()
156     {
157         final int prime = 31;
158         int result = 1;
159         result = prime * result + ((this.list == null) ? 0 : this.list.hashCode());
160         return result;
161     }
162 
163     @Override
164     @SuppressWarnings({"checkstyle:designforextension", "checkstyle:needbraces"})
165     public boolean equals(final Object obj)
166     {
167         if (this == obj)
168             return true;
169         if (obj == null)
170             return false;
171         if (getClass() != obj.getClass())
172             return false;
173         ImmutableAbstractList<?> other = (ImmutableAbstractList<?>) obj;
174         if (this.list == null)
175         {
176             if (other.list != null)
177                 return false;
178         }
179         else if (!this.list.equals(other.list))
180             return false;
181         return true;
182     }
183 
184 }