001package org.intellimate.izou.util;
002
003import org.intellimate.izou.identification.Identifiable;
004
005import java.util.AbstractSet;
006import java.util.HashSet;
007import java.util.Iterator;
008import java.util.Set;
009
010/**
011 * Use this class if you want to store a Set of AddOnModules permanently.
012 * <p>
013 * The reason for this class is that in the future it might be needed to unregister an AddOn and with this class it is
014 * easy to introduce this feature.
015 * </p>
016 * @author Leander Kurscheidt
017 * @version 1.0
018 */
019public class IdentifiableSet<X extends Identifiable> extends AbstractSet<X> implements Set<X>, Cloneable {
020    private Set<X> set = new HashSet<>();
021
022    public IdentifiableSet(Set<X> set) {
023        this.set = set;
024    }
025
026    public IdentifiableSet() {}
027
028    /**
029     * Returns the number of elements in this set (its cardinality).  If this
030     * set contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
031     * <tt>Integer.MAX_VALUE</tt>.
032     *
033     * @return the number of elements in this set (its cardinality)
034     */
035    @Override
036    public int size() {
037        return set.size();
038    }
039
040    /**
041     * Returns <tt>true</tt> if this set contains no elements.
042     *
043     * @return <tt>true</tt> if this set contains no elements
044     */
045    @Override
046    public boolean isEmpty() {
047        return set.isEmpty();
048    }
049
050    /**
051     * Returns <tt>true</tt> if this set contains the specified element.
052     * More formally, returns <tt>true</tt> if and only if this set
053     * contains an element <tt>e</tt> such that
054     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
055     *
056     * @param o element whose presence in this set is to be tested
057     * @return <tt>true</tt> if this set contains the specified element
058     * @throws ClassCastException   if the type of the specified element
059     *                              is incompatible with this set
060     *                              (<a href="Collection.html#optional-restrictions">optional</a>)
061     * @throws NullPointerException if the specified element is null and this
062     *                              set does not permit null elements
063     *                              (<a href="Collection.html#optional-restrictions">optional</a>)
064     */
065    @Override
066    public boolean contains(Object o) {
067        return set.contains(o);
068    }
069
070    /**
071     * Returns an iterator over the elements in this set. The elements are returned in no particular order.
072     * @return an Iterator over the elements in this set
073     */
074    public Iterator<X> iterator() {
075        return set.iterator();
076    }
077
078    /**
079     * Adds an Element to the Set
080     * @param x the Element
081     * @return true if this set did not already contain the specified element
082     * @throws java.lang.IllegalArgumentException if it is not allowed to put Elements without Identification in this
083     *                                            Set
084     */
085    @Override
086    public boolean add(X x) {
087        return set.add(x);
088    }
089
090    /**
091     * {@inheritDoc}
092     * <p>This implementation iterates over the collection looking for the
093     * specified element.  If it finds the element, it removes the element
094     * from the collection using the iterator's remove method.
095     * </p>
096     *
097     * @param o the object to remove
098     * @throws ClassCastException            {@inheritDoc}
099     * @throws NullPointerException          {@inheritDoc}
100     */
101    @Override
102    public boolean remove(Object o) {
103        return set.remove(o);
104    }
105
106    /**
107     * {@inheritDoc}
108     * <p>This implementation iterates over this collection, removing each
109     * element using the <tt>Iterator.remove</tt> operation.  Most
110     * implementations will probably choose to override this method for
111     * efficiency.
112     * </p>
113     */
114    @Override
115    public void clear() {
116        set.clear();
117    }
118
119    /**
120     * Returns a shallow copy of this HashSet instance: the elements themselves are not cloned.
121     *
122     * @return a shallow copy of this set
123     */
124    @Override
125    public Object clone() {
126        try {
127            IdentifiableSet<X> newSet = (IdentifiableSet<X>) super.clone();
128            newSet.set = (Set<X>) ((HashSet<X>) set).clone();
129            return newSet;
130        } catch (CloneNotSupportedException e) {
131            throw new InternalError();
132        }
133    }
134}
135
136