rt/emul/compact/src/main/java/java/util/AbstractSet.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 557 emul/compact/src/main/java/java/util/AbstractSet.java@5be31d9fa455
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
jaroslav@557
     1
/*
jaroslav@557
     2
 * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
jaroslav@557
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@557
     4
 *
jaroslav@557
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@557
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@557
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@557
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@557
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@557
    10
 *
jaroslav@557
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@557
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@557
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@557
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@557
    15
 * accompanied this code).
jaroslav@557
    16
 *
jaroslav@557
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@557
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@557
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@557
    20
 *
jaroslav@557
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@557
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@557
    23
 * questions.
jaroslav@557
    24
 */
jaroslav@557
    25
jaroslav@557
    26
package java.util;
jaroslav@557
    27
jaroslav@557
    28
/**
jaroslav@557
    29
 * This class provides a skeletal implementation of the <tt>Set</tt>
jaroslav@557
    30
 * interface to minimize the effort required to implement this
jaroslav@557
    31
 * interface. <p>
jaroslav@557
    32
 *
jaroslav@557
    33
 * The process of implementing a set by extending this class is identical
jaroslav@557
    34
 * to that of implementing a Collection by extending AbstractCollection,
jaroslav@557
    35
 * except that all of the methods and constructors in subclasses of this
jaroslav@557
    36
 * class must obey the additional constraints imposed by the <tt>Set</tt>
jaroslav@557
    37
 * interface (for instance, the add method must not permit addition of
jaroslav@557
    38
 * multiple instances of an object to a set).<p>
jaroslav@557
    39
 *
jaroslav@557
    40
 * Note that this class does not override any of the implementations from
jaroslav@557
    41
 * the <tt>AbstractCollection</tt> class.  It merely adds implementations
jaroslav@557
    42
 * for <tt>equals</tt> and <tt>hashCode</tt>.<p>
jaroslav@557
    43
 *
jaroslav@557
    44
 * This class is a member of the
jaroslav@557
    45
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
jaroslav@557
    46
 * Java Collections Framework</a>.
jaroslav@557
    47
 *
jaroslav@557
    48
 * @param <E> the type of elements maintained by this set
jaroslav@557
    49
 *
jaroslav@557
    50
 * @author  Josh Bloch
jaroslav@557
    51
 * @author  Neal Gafter
jaroslav@557
    52
 * @see Collection
jaroslav@557
    53
 * @see AbstractCollection
jaroslav@557
    54
 * @see Set
jaroslav@557
    55
 * @since 1.2
jaroslav@557
    56
 */
jaroslav@557
    57
jaroslav@557
    58
public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E> {
jaroslav@557
    59
    /**
jaroslav@557
    60
     * Sole constructor.  (For invocation by subclass constructors, typically
jaroslav@557
    61
     * implicit.)
jaroslav@557
    62
     */
jaroslav@557
    63
    protected AbstractSet() {
jaroslav@557
    64
    }
jaroslav@557
    65
jaroslav@557
    66
    // Comparison and hashing
jaroslav@557
    67
jaroslav@557
    68
    /**
jaroslav@557
    69
     * Compares the specified object with this set for equality.  Returns
jaroslav@557
    70
     * <tt>true</tt> if the given object is also a set, the two sets have
jaroslav@557
    71
     * the same size, and every member of the given set is contained in
jaroslav@557
    72
     * this set.  This ensures that the <tt>equals</tt> method works
jaroslav@557
    73
     * properly across different implementations of the <tt>Set</tt>
jaroslav@557
    74
     * interface.<p>
jaroslav@557
    75
     *
jaroslav@557
    76
     * This implementation first checks if the specified object is this
jaroslav@557
    77
     * set; if so it returns <tt>true</tt>.  Then, it checks if the
jaroslav@557
    78
     * specified object is a set whose size is identical to the size of
jaroslav@557
    79
     * this set; if not, it returns false.  If so, it returns
jaroslav@557
    80
     * <tt>containsAll((Collection) o)</tt>.
jaroslav@557
    81
     *
jaroslav@557
    82
     * @param o object to be compared for equality with this set
jaroslav@557
    83
     * @return <tt>true</tt> if the specified object is equal to this set
jaroslav@557
    84
     */
jaroslav@557
    85
    public boolean equals(Object o) {
jaroslav@557
    86
        if (o == this)
jaroslav@557
    87
            return true;
jaroslav@557
    88
jaroslav@557
    89
        if (!(o instanceof Set))
jaroslav@557
    90
            return false;
jaroslav@557
    91
        Collection c = (Collection) o;
jaroslav@557
    92
        if (c.size() != size())
jaroslav@557
    93
            return false;
jaroslav@557
    94
        try {
jaroslav@557
    95
            return containsAll(c);
jaroslav@557
    96
        } catch (ClassCastException unused)   {
jaroslav@557
    97
            return false;
jaroslav@557
    98
        } catch (NullPointerException unused) {
jaroslav@557
    99
            return false;
jaroslav@557
   100
        }
jaroslav@557
   101
    }
jaroslav@557
   102
jaroslav@557
   103
    /**
jaroslav@557
   104
     * Returns the hash code value for this set.  The hash code of a set is
jaroslav@557
   105
     * defined to be the sum of the hash codes of the elements in the set,
jaroslav@557
   106
     * where the hash code of a <tt>null</tt> element is defined to be zero.
jaroslav@557
   107
     * This ensures that <tt>s1.equals(s2)</tt> implies that
jaroslav@557
   108
     * <tt>s1.hashCode()==s2.hashCode()</tt> for any two sets <tt>s1</tt>
jaroslav@557
   109
     * and <tt>s2</tt>, as required by the general contract of
jaroslav@557
   110
     * {@link Object#hashCode}.
jaroslav@557
   111
     *
jaroslav@557
   112
     * <p>This implementation iterates over the set, calling the
jaroslav@557
   113
     * <tt>hashCode</tt> method on each element in the set, and adding up
jaroslav@557
   114
     * the results.
jaroslav@557
   115
     *
jaroslav@557
   116
     * @return the hash code value for this set
jaroslav@557
   117
     * @see Object#equals(Object)
jaroslav@557
   118
     * @see Set#equals(Object)
jaroslav@557
   119
     */
jaroslav@557
   120
    public int hashCode() {
jaroslav@557
   121
        int h = 0;
jaroslav@557
   122
        Iterator<E> i = iterator();
jaroslav@557
   123
        while (i.hasNext()) {
jaroslav@557
   124
            E obj = i.next();
jaroslav@557
   125
            if (obj != null)
jaroslav@557
   126
                h += obj.hashCode();
jaroslav@557
   127
        }
jaroslav@557
   128
        return h;
jaroslav@557
   129
    }
jaroslav@557
   130
jaroslav@557
   131
    /**
jaroslav@557
   132
     * Removes from this set all of its elements that are contained in the
jaroslav@557
   133
     * specified collection (optional operation).  If the specified
jaroslav@557
   134
     * collection is also a set, this operation effectively modifies this
jaroslav@557
   135
     * set so that its value is the <i>asymmetric set difference</i> of
jaroslav@557
   136
     * the two sets.
jaroslav@557
   137
     *
jaroslav@557
   138
     * <p>This implementation determines which is the smaller of this set
jaroslav@557
   139
     * and the specified collection, by invoking the <tt>size</tt>
jaroslav@557
   140
     * method on each.  If this set has fewer elements, then the
jaroslav@557
   141
     * implementation iterates over this set, checking each element
jaroslav@557
   142
     * returned by the iterator in turn to see if it is contained in
jaroslav@557
   143
     * the specified collection.  If it is so contained, it is removed
jaroslav@557
   144
     * from this set with the iterator's <tt>remove</tt> method.  If
jaroslav@557
   145
     * the specified collection has fewer elements, then the
jaroslav@557
   146
     * implementation iterates over the specified collection, removing
jaroslav@557
   147
     * from this set each element returned by the iterator, using this
jaroslav@557
   148
     * set's <tt>remove</tt> method.
jaroslav@557
   149
     *
jaroslav@557
   150
     * <p>Note that this implementation will throw an
jaroslav@557
   151
     * <tt>UnsupportedOperationException</tt> if the iterator returned by the
jaroslav@557
   152
     * <tt>iterator</tt> method does not implement the <tt>remove</tt> method.
jaroslav@557
   153
     *
jaroslav@557
   154
     * @param  c collection containing elements to be removed from this set
jaroslav@557
   155
     * @return <tt>true</tt> if this set changed as a result of the call
jaroslav@557
   156
     * @throws UnsupportedOperationException if the <tt>removeAll</tt> operation
jaroslav@557
   157
     *         is not supported by this set
jaroslav@557
   158
     * @throws ClassCastException if the class of an element of this set
jaroslav@557
   159
     *         is incompatible with the specified collection
jaroslav@557
   160
     * (<a href="Collection.html#optional-restrictions">optional</a>)
jaroslav@557
   161
     * @throws NullPointerException if this set contains a null element and the
jaroslav@557
   162
     *         specified collection does not permit null elements
jaroslav@557
   163
     * (<a href="Collection.html#optional-restrictions">optional</a>),
jaroslav@557
   164
     *         or if the specified collection is null
jaroslav@557
   165
     * @see #remove(Object)
jaroslav@557
   166
     * @see #contains(Object)
jaroslav@557
   167
     */
jaroslav@557
   168
    public boolean removeAll(Collection<?> c) {
jaroslav@557
   169
        boolean modified = false;
jaroslav@557
   170
jaroslav@557
   171
        if (size() > c.size()) {
jaroslav@557
   172
            for (Iterator<?> i = c.iterator(); i.hasNext(); )
jaroslav@557
   173
                modified |= remove(i.next());
jaroslav@557
   174
        } else {
jaroslav@557
   175
            for (Iterator<?> i = iterator(); i.hasNext(); ) {
jaroslav@557
   176
                if (c.contains(i.next())) {
jaroslav@557
   177
                    i.remove();
jaroslav@557
   178
                    modified = true;
jaroslav@557
   179
                }
jaroslav@557
   180
            }
jaroslav@557
   181
        }
jaroslav@557
   182
        return modified;
jaroslav@557
   183
    }
jaroslav@557
   184
jaroslav@557
   185
}