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
14
15
16
17
18
19
20
21
22
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
30 private final List<E> list;
31
32
33
34
35
36
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
46 @Override
47 public final Collection<E> toCollection()
48 {
49 return toList();
50 }
51
52
53 @Override
54 protected List<E> getUnderlyingCollection()
55 {
56 return this.list;
57 }
58
59
60 @Override
61 public final int size()
62 {
63 return this.list.size();
64 }
65
66
67 @Override
68 public final boolean isEmpty()
69 {
70 return this.list.isEmpty();
71 }
72
73
74 @Override
75 public final boolean contains(final Object o)
76 {
77 return this.list.contains(o);
78 }
79
80
81 @Override
82 public final int indexOf(final Object o)
83 {
84 return this.list.indexOf(o);
85 }
86
87
88 @Override
89 public final int lastIndexOf(final Object o)
90 {
91 return this.list.lastIndexOf(o);
92 }
93
94
95 @Override
96 public final Object[] toArray()
97 {
98 return this.list.toArray();
99 }
100
101
102 @Override
103 public final <T> T[] toArray(final T[] a)
104 {
105 return this.list.toArray(a);
106 }
107
108
109 @Override
110 public final E get(final int index)
111 {
112 return this.list.get(index);
113 }
114
115
116 @Override
117 public final ImmutableIterator<E> iterator()
118 {
119 return new ImmutableIterator<E>(this.list.iterator());
120 }
121
122
123 @Override
124 public final void forEach(final Consumer<? super E> action)
125 {
126 this.list.forEach(action);
127 }
128
129
130 @Override
131 public final Spliterator<E> spliterator()
132 {
133 return this.list.spliterator();
134 }
135
136
137 @Override
138 public final boolean containsAll(final Collection<?> c)
139 {
140 return this.list.containsAll(c);
141 }
142
143
144 @Override
145 public final boolean containsAll(final ImmutableCollection<?> c)
146 {
147 return this.list.containsAll(c.toCollection());
148 }
149
150
151 @Override
152 public final Stream<E> stream()
153 {
154 return this.list.stream();
155 }
156
157
158 @Override
159 public final Stream<E> parallelStream()
160 {
161 return this.list.parallelStream();
162 }
163
164
165 @Override
166 public final boolean isWrap()
167 {
168 return this.copyOrWrap.isWrap();
169 }
170
171
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
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 }