001 /*
002 * Java Genetic Algorithm Library (jenetics-1.6.0).
003 * Copyright (c) 2007-2014 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@gmx.at)
019 */
020 package org.jenetics.util;
021
022 import java.util.ListIterator;
023 import java.util.NoSuchElementException;
024
025 /**
026 * Helper class which iterates over an given array.
027 *
028 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
029 * @since 1.0
030 * @version 1.0 — <em>$Date: 2013-09-18 $</em>
031 */
032 class ArraySeqIterator<T> implements ListIterator<T> {
033 final ArraySeq<T> _array;
034
035 int _cursor;
036 int _lastElement = -1;
037
038 public ArraySeqIterator(final ArraySeq<T> array) {
039 _array = array;
040 _cursor = array._start;
041 }
042
043 @Override
044 public boolean hasNext() {
045 return _cursor != _array._end;
046 }
047
048 @Override
049 @SuppressWarnings("unchecked")
050 public T next() {
051 final int i = _cursor;
052 if (_cursor >= _array._end) {
053 throw new NoSuchElementException();
054 }
055
056 _cursor = i + 1;
057 return (T)_array._array.data[_lastElement = i];
058 }
059
060 @Override
061 public int nextIndex() {
062 return _cursor;
063 }
064
065 @Override
066 public boolean hasPrevious() {
067 return _cursor != _array._start;
068 }
069
070 @SuppressWarnings("unchecked")
071 @Override
072 public T previous() {
073 final int i = _cursor - 1;
074 if (i < _array._start) {
075 throw new NoSuchElementException();
076 }
077
078 _cursor = i;
079 return (T)_array._array.data[_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("Array is immutable.");
090 }
091
092 @Override
093 public void add(final T o) {
094 throw new UnsupportedOperationException("Can't change array size.");
095 }
096
097 @Override
098 public void remove() {
099 throw new UnsupportedOperationException("Can't change array size.");
100 }
101
102 }
|