package ru.pravvich.set.arraySimpleSet;

import java.util.Iterator;
import java.util.Objects;

public class ArraySimpleSet<E> implements SetArray<E> {

    private volatile E[] values;

    public ArraySimpleSet() {
        values = (E[]) new Object[0];
    }

    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {
            private int index = 0;

            @Override
            public boolean hasNext() {
                return index < values.length;
            }

            @Override
            public E next() {
                return values[index++];
            }
        };
    }

    @Override
    public synchronized boolean add(E e) {
        Objects.requireNonNull(e);
        if (notExistDuplicate(e)) {
            try {
                E[] temp = values;
                values = (E[]) new Object[temp.length + 1]; // ??? ClassCastException ???
                System.arraycopy(temp, 0, values, 0, temp.length);
                values[values.length - 1] = e;
                sortByHashCode();
                return true;
            } catch (ClassCastException ex) {
                ex.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public int size() {
        return values.length;
    }

    private boolean notExistDuplicate(E elem) {
        for (E e : values) {
            if (e.equals(elem))
                return false;
        }
        return true;
    }

    private void sortByHashCode() {
        quickSort(values, 0, values.length - 1);
    }

    private void quickSort(E[] array, int left, int right) {
        if (right > left) {
            int i = left, j = right;
            E tmp;

            int v = array[right].hashCode(); //pivot

            do {
                while (array[i].hashCode() < v)
                    i++;
                while (array[j].hashCode() > v)
                    j--;

                if (i <= j) {
                    tmp = array[i];
                    array[i] = array[j];
                    array[j] = tmp;
                    i++;
                    j--;
                }
            } while (i <= j);

            if (left < j) quickSort(array, left, j);
            if (i < right) quickSort(array, i, right);
        }
    }
}

@@ -161,15 +161,59 @@
 ate 
-volatil
+static final Object lock = new Object();%0A    privat
 e E%5B
@@ -344,24 +344,58 @@
 terator() %7B%0A
+        synchronized (lock) %7B%0A    
         retu
@@ -425,24 +425,28 @@
             
+   
+ 
 private int 
@@ -461,32 +461,36 @@
 0;%0A%0A            
+    
 @Override%0A      
@@ -491,24 +491,28 @@
             
+ 
+   
 public boole
@@ -538,24 +538,28 @@
             
+   
+ 
 return index
@@ -584,27 +584,35 @@
             
-%7D%0A%0A
+    %7D%0A%0A    
             
@@ -629,24 +629,28 @@
             
+   
+ 
 public E nex
@@ -647,32 +647,36 @@
 blic E next() %7B%0A
+    
                 
@@ -711,26 +711,33 @@
         
-%7D%0A
+    %7D%0A   
         
 %7D;%0A    %7D
@@ -732,11 +732,22 @@
     
+ 
 %7D;%0A
+        %7D%0A
     
@@ -774,29 +774,16 @@
  public 
-synchronized 
 boolean 
@@ -789,24 +789,58 @@
  add(E e) %7B%0A
+        synchronized (lock) %7B%0A    
         Obje
@@ -866,24 +866,28 @@
 e);%0A        
+   
+ 
 if (notExist
@@ -914,16 +914,20 @@
         
+    
 try %7B%0A  
@@ -940,16 +940,20 @@
         
+ 
+   
 E%5B%5D temp
@@ -971,32 +971,36 @@
                 
+   
+ 
 values = (E%5B%5D) n
@@ -1073,16 +1073,20 @@
         
+    
 System.a
@@ -1140,24 +1140,28 @@
             
+ 
+   
 values%5Bvalue
@@ -1195,16 +1195,20 @@
         
+   
+ 
 sortByHa
@@ -1225,32 +1225,36 @@
                 
+    
 return true;%0A   
@@ -1262,16 +1262,20 @@
         
+ 
+   
 %7D catch 
@@ -1316,16 +1316,20 @@
         
+   
+ 
 ex.print
@@ -1354,26 +1354,32 @@
         
-%7D%0A
+    %7D%0A  
         
 %7D%0A      
@@ -1366,34 +1366,39 @@
 %7D%0A          
-%7D%0A
+  %7D%0A   
         
 return false
@@ -1377,32 +1377,33 @@
    %7D%0A           
+ 
 return false;%0A  
@@ -1396,24 +1396,35 @@
 turn false;%0A
+        %7D%0A%0A
     %7D%0A%0A    @

add synchronize in ArraySimpleSet