package ru.agolovin;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * @author agolovin (agolovin@list.ru)
 * @version $Id$
 * @since 0.1
 */

public class PrimeNumberIterator implements Iterator<Integer> {

    /**
     * Private class array.
     */
    private int[] array;

    /**
     * Index in private array.
     */
    private int index = 0;

    /**
     * Index primeNumber.
     */
    private int indexPrimeNumber;

    /**
     * Constructor.
     * @param array int[]
     */
    PrimeNumberIterator(int[] array) {
        this.array = array;
    }

    @Override
    public boolean hasNext() {
        boolean result = false;
        int i = indexInArray();
        this.indexPrimeNumber = i;
        if (i != -1) {
            this.indexPrimeNumber = i;
            result = true;
        }

        return result;
    }

    @Override
    public Integer next() {
        int result;
        if (this.indexPrimeNumber != -1) {
            result = this.array[this.indexPrimeNumber];
            this.index = this.indexPrimeNumber + 1;
        } else {
            throw new NoSuchElementException();
        }
        return result;
    }

    /**
     * Index prime number in array.
     * @return index int
     */
    private int indexInArray() {
        int result = -1;
        boolean flag = false;
        for (int i = index; i < this.array.length; i++) {
            int element = this.array[i];
            if (element > 1) {
                if (element == 2 || element == 3) {
                    result = i;
                    break;
                } else {
                    for (int j = 2; j * j < element; j++) {
                        if (element % j == 0) {
                            break;
                        } else {
                            result = i;
                            flag = true;
                        }
                    }
                }
                if (flag) {
                    break;
                }
            }
        }
        return result;
    }
}

@@ -478,16 +478,23 @@
 ructor.%0A
+     *%0A
      * @
@@ -650,220 +650,35 @@
     
-boolean result = false;%0A        int i = indexInArray();%0A        this.indexPrimeNumber = i;%0A        if (i != -1) %7B%0A            this.indexPrimeNumber = i;%0A            result = true;%0A        %7D%0A%0A        return result
+return indexInArray() != -1
 ;%0A  
@@ -756,42 +756,56 @@
     
-if (this.indexPrimeNumber != -1) %7B
+try %7B%0A            int position = indexInArray();
 %0A   
@@ -833,37 +833,24 @@
 s.array%5B
-this.indexPrimeNumber
+position
 %5D;%0A     
@@ -873,49 +873,72 @@
 x = 
-this.indexPrimeNumber + 1;%0A        %7D else
+position + 1;%0A        %7D catch (ArrayIndexOutOfBoundsException e)
  %7B%0A 
@@ -1064,24 +1064,31 @@
 r in array.%0A
+     *%0A
      * @retu
@@ -1217,16 +1217,21 @@
 int i = 
+this.
 index; i

fix task3 in part1 chapter5 package2