package uebung09.collection;

import java.util.Iterator;

class BigSet<T> implements Set<T>{

    private T item;
    private Set<T> rest;

    public BigSet(T item, Set<T> rest){
        //TODO
        this.item = item;
        this.rest = rest;
    }

    @Override
    public Iterator<T> iterator() {
        return new Iterator<T>() {

            private boolean returned = false;

            private Iterator<T> restI = rest.iterator();
            
            @Override
            public boolean hasNext() {
                return restI.hasNext();
            }

            @Override
            public T next() {
                if (!returned) {
                    returned = true;
                    return item;
                } else {
                    return restI.next();
                }
            }
        };
    }

    @Override
    public int size() {
        int count = 1;
        count += rest.size();

        return count;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public boolean contains(Object el) {
        if(el == item) {
            return true;
        } else {
            return rest.contains(el);
        }
    }

    @Override
    public Set<T> union(Set<T> other) {
        if (other == EmptySet.getInstance()) {
            return this;
        } else if (other instanceof SingeltonSet<T> && !contains(other.iterator().next())) {
            return new BigSet<T>(other.iterator().next(), this);
        } else {
            return new BigSet<T>(item, rest.union(other));
        }
    }

    @Override
    public Set<T> add(T element) {
        if (contains(element)) {
            return this;
        } else {
            return new BigSet<T>(element, this);
        }
    }

    @Override
    public Set<T> intersection(Set<T> other) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'intersection'");
    }

    @Override
    public boolean subsetOf(Set<?> other) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'subsetOf'");
    }
}
