package ru.job4j.taskIterator;

import java.util.*;

/**
 * Class for iterator for prime number in array.
 * @author atrifonov.
 * @since 19.08.2017.
 * @version 1.
 */
public class Converter {
    /**
     * Convert iterator of iterators integers to iterator of integers.
     * @param it iterator of iterators integers
     * @return iterator of integers
     */
    public Iterator<Integer> convert(Iterator<Iterator<Integer>> it){
        return new Iterator<Integer>() {
            private Iterator<Integer> iterator;
            @Override
            public boolean hasNext() {

                boolean has = false;
                if(iterator == null) {
                    iterator = it.next();
                }
                if(iterator.hasNext()) {
                    has = true;
                } else if(it.hasNext()) {
                    iterator = it.next();
                    has = true;
                }
                return has;
            }

            @Override
            public Integer next() {
                int item;

                if(iterator == null) {
                    iterator = it.next();
                }

                try {
                    item = iterator.next();
                } catch (NoSuchElementException e) {
                    iterator = it.next();
                    item = iterator.next();
                }

                return item;
            }
        };
    }

}


@@ -1027,35 +1027,8 @@
 ) %7B%0A
-                int item;%0A%0A
     
@@ -1054,32 +1054,32 @@
 ator == null) %7B%0A
+
                 
@@ -1143,11 +1143,21 @@
     
-try
+if(hasNext())
  %7B%0A 
@@ -1167,38 +1167,38 @@
                 
-item =
+return
  iterator.next()
@@ -1221,176 +1221,88 @@
   %7D 
-catch (NoSuchElementException e) %7B%0A                    iterator = it.next();%0A                    item = iterator.next();%0A                %7D%0A%0A                return item;
+else %7B%0A                    throw new NoSuchElementException();%0A                %7D
 %0A   

edit task 152