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 @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 }