ArrayProxyIterator.java
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.internal.util;
021 
022 import java.util.ListIterator;
023 import java.util.NoSuchElementException;
024 
025 
026 /**
027  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
028  @since 1.4
029  @version 1.5 &mdash; <em>$Date: 2013-12-04 $</em>
030  */
031 public class ArrayProxyIterator<T> implements ListIterator<T> {
032 
033     protected final ArrayProxy<T> _proxy;
034 
035     protected int _cursor = 0;
036     protected int _lastElement = -1;
037 
038     public ArrayProxyIterator(final ArrayProxy<T> proxy) {
039         _proxy = proxy;
040     }
041 
042     @Override
043     public boolean hasNext() {
044         return _cursor != _proxy._length;
045     }
046 
047     @Override
048     public T next() {
049         final int i = _cursor;
050         if (_cursor >= _proxy._length) {
051             throw new NoSuchElementException();
052         }
053 
054         _cursor = i + 1;
055         return _proxy.uncheckedGet(_lastElement = i);
056     }
057 
058     @Override
059     public int nextIndex() {
060         return _cursor;
061     }
062 
063     @Override
064     public boolean hasPrevious() {
065         return _cursor != 0;
066     }
067 
068     @Override
069     public T previous() {
070         final int i = _cursor - 1;
071         if (i < 0) {
072             throw new NoSuchElementException();
073         }
074 
075         _cursor = i;
076         return _proxy.uncheckedGet(_lastElement = i);
077     }
078 
079     @Override
080     public int previousIndex() {
081         return _cursor - 1;
082     }
083 
084     @Override
085     public void set(final T value) {
086         throw new UnsupportedOperationException(
087             "Iterator is immutable."
088         );
089     }
090 
091     @Override
092     public void add(final T value) {
093         throw new UnsupportedOperationException(
094             "Can't change Iterator size."
095         );
096     }
097 
098     @Override
099     public void remove() {
100         throw new UnsupportedOperationException(
101             "Can't change Iterator size."
102         );
103     }
104 
105 }