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