001 /*
002 * Java GPX Library (jpx-3.1.0).
003 * Copyright (c) 2016-2023 Franz Wilhelmstötter
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 * Author:
018 * Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019 */
020 package io.jenetics.jpx;
021
022 import static java.util.Objects.requireNonNull;
023
024 import java.io.Serial;
025 import java.io.Serializable;
026 import java.util.Collection;
027 import java.util.Comparator;
028 import java.util.Iterator;
029 import java.util.List;
030 import java.util.ListIterator;
031 import java.util.Objects;
032 import java.util.Spliterator;
033 import java.util.function.Consumer;
034 import java.util.function.Predicate;
035 import java.util.function.UnaryOperator;
036 import java.util.stream.Stream;
037
038 /**
039 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
040 * @version 1.1
041 * @since 1.1
042 */
043 final class NonNullList<E> implements List<E>, Serializable {
044
045 @Serial
046 private static final long serialVersionUID = 1L;
047
048 private final List<E> _adoptee;
049
050 NonNullList(final List<E> adoptee) {
051 _adoptee = requireNonNull(adoptee);
052 }
053
054 @Override
055 public int size() {
056 return _adoptee.size();
057 }
058
059 @Override
060 public boolean isEmpty() {
061 return _adoptee.isEmpty();
062 }
063
064 @Override
065 public boolean contains(final Object o) {
066 return _adoptee.contains(o);
067 }
068
069 @Override
070 public Object[] toArray() {
071 return _adoptee.toArray();
072 }
073
074 @Override
075 public <T> T[] toArray(final T[] a) {
076 return _adoptee.toArray(a);
077 }
078
079 @Override
080 public String toString() {
081 return _adoptee.toString();
082 }
083
084 @Override
085 public Iterator<E> iterator() {
086 return new Iterator<E>() {
087 private final Iterator<E> _it = _adoptee.iterator();
088
089 @Override
090 public boolean hasNext() {
091 return _it.hasNext();
092 }
093
094 @Override
095 public E next() {
096 return _it.next();
097 }
098
099 @Override
100 public void remove() {
101 _it.remove();
102 }
103
104 @Override
105 public void forEachRemaining(final Consumer<? super E> action) {
106 _it.forEachRemaining(action);
107 }
108 };
109 }
110
111 @Override
112 public boolean add(final E e) {
113 return _adoptee.add(requireNonNull(e));
114 }
115
116 @Override
117 public boolean remove(final Object o) {
118 return _adoptee.remove(requireNonNull(o));
119 }
120
121 @Override
122 public boolean containsAll(final Collection<?> coll) {
123 return _adoptee.containsAll(coll);
124 }
125
126 @Override
127 public boolean addAll(final Collection<? extends E> coll) {
128 coll.forEach(Objects::requireNonNull);
129 return _adoptee.addAll(coll);
130 }
131
132 @Override
133 public boolean removeAll(final Collection<?> coll) {
134 return _adoptee.removeAll(coll);
135 }
136
137 @Override
138 public boolean retainAll(final Collection<?> coll) {
139 coll.forEach(Objects::requireNonNull);
140 return _adoptee.retainAll(coll);
141 }
142
143 @Override
144 public void clear() {
145 _adoptee.clear();
146 }
147
148 @Override
149 public void forEach(final Consumer<? super E> action) {
150 _adoptee.forEach(action);
151 }
152
153 @Override
154 public boolean removeIf(final Predicate<? super E> filter) {
155 return _adoptee.removeIf(filter);
156 }
157
158 @Override
159 public Spliterator<E> spliterator() {
160 return _adoptee.spliterator();
161 }
162
163 @Override
164 public Stream<E> stream() {
165 return _adoptee.stream();
166 }
167
168 @Override
169 public Stream<E> parallelStream() {
170 return _adoptee.parallelStream();
171 }
172
173 @Override
174 public E get(final int index) {
175 return _adoptee.get(index);
176 }
177
178 @Override
179 public E set(final int index, final E element) {
180 return _adoptee.set(index, requireNonNull(element));
181 }
182
183 @Override
184 public void add(final int index, final E element) {
185 _adoptee.add(index, requireNonNull(element));
186 }
187
188 @Override
189 public E remove(int index) {
190 return _adoptee.remove(index);
191 }
192
193 @Override
194 public int indexOf(final Object o) {
195 return _adoptee.indexOf(requireNonNull(o));
196 }
197
198 @Override
199 public int lastIndexOf(final Object o) {
200 return _adoptee.lastIndexOf(requireNonNull(o));
201 }
202
203 @Override
204 public boolean addAll(final int index, final Collection<? extends E> coll) {
205 coll.forEach(Objects::requireNonNull);
206 return _adoptee.addAll(coll);
207 }
208
209 @Override
210 public void replaceAll(final UnaryOperator<E> operator) {
211 _adoptee.replaceAll(operator);
212 }
213
214 @Override
215 public void sort(final Comparator<? super E> comparator) {
216 _adoptee.sort(comparator);
217 }
218
219 @Override
220 public ListIterator<E> listIterator() {
221 return listIterator(0);
222 }
223
224 @Override
225 public ListIterator<E> listIterator(final int index) {
226 return new ListIterator<E>() {
227 private final ListIterator<E> _it =
228 _adoptee.listIterator(index);
229
230 @Override
231 public boolean hasNext() {
232 return _it.hasNext();
233 }
234
235 @Override
236 public E next() {
237 return _it.next();
238 }
239
240 @Override
241 public boolean hasPrevious() {
242 return _it.hasPrevious();
243 }
244
245 @Override
246 public E previous() {
247 return _it.previous();
248 }
249
250 @Override
251 public int nextIndex() {
252 return _it.nextIndex();
253 }
254
255 @Override
256 public int previousIndex() {
257 return _it.previousIndex();
258 }
259
260 @Override
261 public void remove() {
262 _it.remove();
263 }
264
265 @Override
266 public void set(final E e) {
267 _it.set(requireNonNull(e));
268 }
269
270 @Override
271 public void add(final E e) {
272 _it.add(requireNonNull(e));
273 }
274
275 @Override
276 public void forEachRemaining(final Consumer<? super E> action) {
277 _it.forEachRemaining(action);
278 }
279
280 };
281 }
282
283 @Override
284 public List<E> subList(final int from, final int to) {
285 return new NonNullList<>(_adoptee.subList(from, to));
286 }
287
288 @Override
289 public int hashCode() {
290 return _adoptee.hashCode();
291 }
292
293 @Override
294 public boolean equals(final Object o) {
295 return o == this || _adoptee.equals(o);
296 }
297
298 }
|