package ru.job4j.set;

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

/**
 * @author Vitaly Zubov (mailto:Zubov.VP@yandex.ru).
 * @version $Id$.
 * @since 0.1.
 */
public class MyHashSet<E> implements Iterable<E> {
    private E[] table;
    private int count = 0;

    public MyHashSet(int initialCapacity) {
        this.table = (E[]) new Object[initialCapacity];
    }

    public MyHashSet() {
        this.table = (E[]) new Object[16];
    }

    /**
     *  Check element in MyHashSet.
     *
     * @param e - element.
     * @return - result.
     */
    public boolean contains(E e) {
        return this.table[calculatePosition(e)] != null;
    }

    /**
     * Add element in MyHashSet.
     *
     * @param e - element.
     * @return - result.
     */
    public boolean add(E e) {
        checkcapacity();
        boolean result = false;
        if (!contains(e)) {
            this.table[calculatePosition(e)] = e;
            result = true;
            count++;
        }
        return result;
    }

    /**
     * Remove element from MyHashSet.
     *
     * @param e - element.
     * @return - result.
     */
    public boolean remove(E e) {
        boolean result = false;
        if (contains(e)) {
            this.table[calculatePosition(e)] = null;
            result = true;
            count--;
        }
        return result;
    }

    /**
     * Check capacity of MyHashSet.
     */
    private void checkcapacity() {
        if ((double) count / this.table.length >= 0.75) {
            E[] newTable = (E[]) new Object[this.table.length * 2];
            for (E count : this.table) {
                newTable[calculatePosition(count)] = count;
            }
            this.table = newTable;
        }
    }

    /**
     * Calculate number position for element.
     *
     * @param e - element.
     * @return - number position.
     */
    private int calculatePosition(E e) {
        return Math.abs(e.hashCode() % this.table.length);
    }

    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {
            int step = 0;
            int expectedModCount = count;

            @Override
            public boolean hasNext() {
                boolean result = false;
                if (expectedModCount != count) {
                    throw new ConcurrentModificationException();
                }
                for (int x = step; x < table.length; x++) {
                    if (table[x] != null) {
                        result = true;
                    }
                }
                return result;
            }

            @Override
            public E next() {
                E result = null;
                if (expectedModCount != count) {
                    throw new ConcurrentModificationException();
                }
                for (int x = step; x < table.length; x++) {
                    if (table[x] != null) {
                        result = table[x];
                        step++;
                        break;
                    }
                    step++;
                }
                    if (result == null) {
                        throw new NoSuchElementException();
                    }
                return result;
            }
        };
    }
}

@@ -704,16 +704,35 @@
 sition(e
+, this.table.length
 )%5D != nu
@@ -1003,24 +1003,43 @@
 tePosition(e
+, this.table.length
 )%5D = e;%0A    
@@ -1369,16 +1369,35 @@
 sition(e
+, this.table.length
 )%5D = nul
@@ -1789,16 +1789,33 @@
 on(count
+, newTable.length
 )%5D = cou
@@ -2047,24 +2047,34 @@
 Position(E e
+, int size
 ) %7B%0A        
@@ -2242,16 +2242,51 @@
 ep = 0;%0A
+            int returnElement = 0;%0A
         
@@ -2328,33 +2328,27 @@
             
-@Override
+/**
 %0A           
@@ -2352,74 +2352,203 @@
     
-public boolean hasNext() %7B%0A                boolean result = false;
+ * check correct of the table.%0A             *%0A             * @param expectedModCount - count.%0A             */%0A            private void checkConcurrentModificationException(int expectedModCount) %7B
 %0A   
@@ -2692,64 +2692,75 @@
     
+%7D%0A%0A
     
-for (int x = step; x %3C table.length; x++) %7B%0A
+        /**%0A             * Check next element.%0A     
         
     
@@ -2747,32 +2747,34 @@
 t.%0A             
+*%0A
             if (
@@ -2773,69 +2773,222 @@
     
-if (table%5Bx%5D != null) %7B%0A                        result = true
+ * @return - result.%0A             */%0A            @Override%0A            public boolean hasNext() %7B%0A                checkConcurrentModificationException(expectedModCount);%0A                return count %3E returnElement
 ;%0A  
@@ -2988,32 +2988,36 @@
 ent;%0A           
+ %7D%0A%0A
          %7D%0A     
@@ -3001,33 +3001,38 @@
     %7D%0A%0A         
-%7D
+   /**
 %0A               
@@ -3021,36 +3021,52 @@
 **%0A             
-   %7D
+* Return next count.
 %0A               
@@ -3060,36 +3060,52 @@
         
+   *%0A
       
-return result;
+       * @return - count.
 %0A       
@@ -3101,34 +3101,35 @@
 nt.%0A            
-%7D%0A
+ */
 %0A            @Ov
@@ -3218,37 +3218,94 @@
     
-if (expectedModCount != count
+checkConcurrentModificationException(expectedModCount);%0A                if (!hasNext()
 ) %7B%0A
@@ -3334,38 +3334,29 @@
 row new 
-ConcurrentModification
+NoSuchElement
 Exceptio
@@ -3676,115 +3676,24 @@
     
-    if (result == null) %7B%0A                        throw new NoSuchElementException();%0A                    %7D
+returnElement++;
 %0A   

Correct MyHashSet