package uebung09.collection;

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

class SingeltonSet<T> implements Set<T> {

    private T item;

    public SingeltonSet(T item) {
        this.item = item;
    }

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

            boolean returned = false;

            @Override
            public boolean hasNext() {
                return !returned;
            }

            @Override
            public T next() {
                if (!returned) {
                    returned = true;
                    return item;
                } else {
                    throw new NoSuchElementException();
                }
            }
        };
    }

    @Override
    public int size() {
        return 1;
    }

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

    @Override
    public boolean contains(Object el) {
        return el == item;
    }

    @Override
    public Set<T> union(Set<T> other) {
        //TODO
        return null;
    }

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

    @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) {
        throw new UnsupportedOperationException();
    }
}
