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