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