001/*
002 * Java Genetic Algorithm Library (jenetics-8.0.0).
003 * Copyright (c) 2007-2024 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 */
020package io.jenetics.internal.collection;
021
022import static java.util.Objects.requireNonNull;
023
024import java.util.ListIterator;
025import java.util.NoSuchElementException;
026
027import io.jenetics.util.BaseSeq;
028
029/**
030 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
031 * @since 1.4
032 * @version 5.2
033 */
034public class BaseSeqIterator<T, S extends BaseSeq<T>> implements ListIterator<T> {
035
036        public final S array;
037
038        private int cursor = 0;
039        int lastElement = -1;
040
041        public BaseSeqIterator(final S array) {
042                this.array = requireNonNull(array, "Array must not be null.");
043        }
044
045        @Override
046        public boolean hasNext() {
047                return cursor != array.length();
048        }
049
050        @Override
051        public T next() {
052                final int i = cursor;
053                if (cursor >= array.length()) {
054                        throw new NoSuchElementException();
055                }
056
057                cursor = i + 1;
058                return array.get(lastElement = i);
059        }
060
061        @Override
062        public int nextIndex() {
063                return cursor;
064        }
065
066        @Override
067        public boolean hasPrevious() {
068                return cursor != 0;
069        }
070
071        @Override
072        public T previous() {
073                final int i = cursor - 1;
074                if (i < 0) {
075                        throw new NoSuchElementException();
076                }
077
078                cursor = i;
079                return array.get(lastElement = i);
080        }
081
082        @Override
083        public int previousIndex() {
084                return cursor - 1;
085        }
086
087        @Override
088        public void set(final T value) {
089                throw new UnsupportedOperationException(
090                        "Iterator is immutable."
091                );
092        }
093
094        @Override
095        public void add(final T value) {
096                throw new UnsupportedOperationException(
097                        "Can't change Iterator size."
098                );
099        }
100
101        @Override
102        public void remove() {
103                throw new UnsupportedOperationException(
104                        "Can't change Iterator size."
105                );
106        }
107
108}