package ru.job4j.simpleContainer;
import java.util.Arrays;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class Container<E> implements SimpleContainer<E>, Iterable<E> {

    private int index = 0;
    private int modCount = 0;
    private int expectedModCount = 0;
    private int indexNext = 0;
    private Object[] container = new Object[10];

    public Object[] ensureCapacity(Object[] source, int capacity) {
        return Arrays.copyOf(source, capacity);
    }

    @Override
    public void add(E value) {
        if (index > 9) {
            container = ensureCapacity(container, index + 1);
            container[index] = value;
        }
        container[index] = value;

        index++;
        modCount++;
    }

    @Override
    public E get(int index) {
        return (E) container[index];
    }

    @Override
    public Iterator<E> iterator() {
        return new SimpleIterator<E>();
    }

    public class SimpleIterator<E> implements Iterator {

        public void exceptions() throws ConcurrentModificationException, NoSuchElementException {
            if (expectedModCount == 0) {
                expectedModCount = modCount;
            }

            if (expectedModCount != modCount) {
                throw new ConcurrentModificationException();
            }

            if (!hasNext()) {
                throw new NoSuchElementException();
            }
        }

        @Override
        public boolean hasNext() {
            return indexNext < index;
        }

        @Override
        public E next() {
            exceptions();
            return (E)container[indexNext++];
        }
    }

    public static void main(String[] args) {
        Container<Integer> container = new Container<>();
        container.add(0);
        container.add(1);
        container.add(2);
        container.add(3);
        container.add(4);
        container.add(5);
        container.add(6);
        container.add(7);
        container.add(8);
        container.add(9);
        container.add(10);
        container.add(11);

    }
}

@@ -304,46 +304,8 @@
  0;%0A
-    private int expectedModCount = 0;%0A
     
@@ -327,24 +327,25 @@
 exNext = 0;%0A
+%0A
     private 
@@ -570,17 +570,36 @@
 index %3E 
-9
+container.length - 1
 ) %7B%0A    
@@ -657,47 +657,10 @@
  + 1
+0
 )
-;%0A            container%5Bindex%5D = value
 ;%0A  
@@ -701,17 +701,16 @@
  value;%0A
-%0A
         
@@ -922,32 +922,34 @@
 or%3CE%3E();%0A    %7D%0A%0A
+%0A%0A
     public class
@@ -1003,29 +1003,100 @@
    p
-ublic void excep
+rivate int expectedModCount = modCount;%0A%0A%0A        public void checkForConcurrentModifica
 tion
-s
 () t
@@ -1163,109 +1163,8 @@
 n %7B%0A
-            if (expectedModCount == 0) %7B%0A                expectedModCount = modCount;%0A            %7D%0A%0A
     
@@ -1285,105 +1285,8 @@
    %7D
-%0A%0A            if (!hasNext()) %7B%0A                throw new NoSuchElementException();%0A            %7D
 %0A   
@@ -1455,502 +1455,193 @@
     
-exceptions();%0A            return (E)container%5BindexNext++%5D;%0A        %7D
+if (!hasNext()) %7B
 %0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Container%3CInteger%3E container = new Container%3C%3E();%0A        container.add(0);%0A
+        throw new NoSuchElementException();%0A        
     
+%7D%0A
     
-container.add(1);%0A        container.add(2);%0A        container.add(3);%0A        container.add(4);%0A        container.add(5);%0A        container.add(6);%0A        container.add(7);%0A        container.add(8);%0A        container.add(9);%0A        container.add(10);%0A        container.add(11);%0A
+        checkForConcurrentModification();%0A            return (E)container%5BindexNext++%5D;%0A        %7D
 %0A   

task #158