001    /*
002     * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
003     * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
004     *
005     *
006     *
007     *
008     *
009     *
010     *
011     *
012     *
013     *
014     *
015     *
016     *
017     *
018     *
019     *
020     *
021     *
022     *
023     *
024     */
025    
026    package railo.commons.util.mod;
027    
028    import java.util.Collection;
029    import java.util.Iterator;
030    import java.util.Set;
031    
032    public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E> {
033        /**
034         * Sole constructor.  (For invocation by subclass constructors, typically
035         * implicit.)
036         */
037        protected AbstractSet() {
038        }
039    
040        // Comparison and hashing
041    
042        /**
043         * Compares the specified object with this set for equality.  Returns
044         * <tt>true</tt> if the given object is also a set, the two sets have
045         * the same size, and every member of the given set is contained in
046         * this set.  This ensures that the <tt>equals</tt> method works
047         * properly across different implementations of the <tt>Set</tt>
048         * interface.<p>
049         *
050         * This implementation first checks if the specified object is this
051         * set; if so it returns <tt>true</tt>.  Then, it checks if the
052         * specified object is a set whose size is identical to the size of
053         * this set; if not, it returns false.  If so, it returns
054         * <tt>containsAll((Collection) o)</tt>.
055         *
056         * @param o object to be compared for equality with this set
057         * @return <tt>true</tt> if the specified object is equal to this set
058         */
059        public boolean equals(Object o) {
060            if (o == this)
061                return true;
062    
063            if (!(o instanceof Set))
064                return false;
065            Collection c = (Collection) o;
066            if (c.size() != size())
067                return false;
068            try {
069                return containsAll(c);
070            } catch (ClassCastException unused)   {
071                return false;
072            } catch (NullPointerException unused) {
073                return false;
074            }
075        }
076    
077        @Override
078        public int hashCode() {
079            int h = 0;
080            Iterator<E> i = iterator();
081            while (i.hasNext()) {
082                E obj = i.next();
083                if (obj != null)
084                    h += obj.hashCode();
085            }
086            return h;
087        }
088    
089        @Override
090        public boolean removeAll(Collection<?> c) {
091            boolean modified = false;
092    
093            if (size() > c.size()) {
094                for (Iterator<?> i = c.iterator(); i.hasNext(); )
095                    modified |= remove(i.next());
096            } else {
097                for (Iterator<?> i = iterator(); i.hasNext(); ) {
098                    if (c.contains(i.next())) {
099                        i.remove();
100                        modified = true;
101                    }
102                }
103            }
104            return modified;
105        }
106    
107    }