package ru.pravvich.set.arraySimpleSet;

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

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

    private 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 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() {
        int len = (values.length - 1);
        for(int i = len; i >= 0; i--) {
            for (int j = 0; j < i; j++) {
                if (values[j].hashCode() < values[j + 1].hashCode()) {
                    E temp = values[j + 1];
                    values[j + 1] = values[j];
                    values[j] = temp;
                }
            }
        }
    }

    public static void main(String[] args) {
        SetArray<String> setArray = new ArraySimpleSet<>();
        setArray.add("dfgsdf");
        setArray.add("abc");
        setArray.add("dfg");
        setArray.add("dfg");

        for (String s : setArray) {
            System.out.println(s);
        }

    }
}

@@ -1845,321 +1845,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        SetArray%3CString%3E setArray = new ArraySimpleSet%3C%3E();%0A        setArray.add(%22dfgsdf%22);%0A        setArray.add(%22abc%22);%0A        setArray.add(%22dfg%22);%0A        setArray.add(%22dfg%22);%0A%0A        for (String s : setArray) %7B%0A            System.out.println(s);%0A        %7D%0A%0A    %7D
 %0A%7D%0A

update ArraySimpleSet