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