1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/compact/src/main/java/java/util/AbstractSet.java Mon Feb 25 19:00:08 2013 +0100
1.3 @@ -0,0 +1,185 @@
1.4 +/*
1.5 + * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +package java.util;
1.30 +
1.31 +/**
1.32 + * This class provides a skeletal implementation of the <tt>Set</tt>
1.33 + * interface to minimize the effort required to implement this
1.34 + * interface. <p>
1.35 + *
1.36 + * The process of implementing a set by extending this class is identical
1.37 + * to that of implementing a Collection by extending AbstractCollection,
1.38 + * except that all of the methods and constructors in subclasses of this
1.39 + * class must obey the additional constraints imposed by the <tt>Set</tt>
1.40 + * interface (for instance, the add method must not permit addition of
1.41 + * multiple instances of an object to a set).<p>
1.42 + *
1.43 + * Note that this class does not override any of the implementations from
1.44 + * the <tt>AbstractCollection</tt> class. It merely adds implementations
1.45 + * for <tt>equals</tt> and <tt>hashCode</tt>.<p>
1.46 + *
1.47 + * This class is a member of the
1.48 + * <a href="{@docRoot}/../technotes/guides/collections/index.html">
1.49 + * Java Collections Framework</a>.
1.50 + *
1.51 + * @param <E> the type of elements maintained by this set
1.52 + *
1.53 + * @author Josh Bloch
1.54 + * @author Neal Gafter
1.55 + * @see Collection
1.56 + * @see AbstractCollection
1.57 + * @see Set
1.58 + * @since 1.2
1.59 + */
1.60 +
1.61 +public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E> {
1.62 + /**
1.63 + * Sole constructor. (For invocation by subclass constructors, typically
1.64 + * implicit.)
1.65 + */
1.66 + protected AbstractSet() {
1.67 + }
1.68 +
1.69 + // Comparison and hashing
1.70 +
1.71 + /**
1.72 + * Compares the specified object with this set for equality. Returns
1.73 + * <tt>true</tt> if the given object is also a set, the two sets have
1.74 + * the same size, and every member of the given set is contained in
1.75 + * this set. This ensures that the <tt>equals</tt> method works
1.76 + * properly across different implementations of the <tt>Set</tt>
1.77 + * interface.<p>
1.78 + *
1.79 + * This implementation first checks if the specified object is this
1.80 + * set; if so it returns <tt>true</tt>. Then, it checks if the
1.81 + * specified object is a set whose size is identical to the size of
1.82 + * this set; if not, it returns false. If so, it returns
1.83 + * <tt>containsAll((Collection) o)</tt>.
1.84 + *
1.85 + * @param o object to be compared for equality with this set
1.86 + * @return <tt>true</tt> if the specified object is equal to this set
1.87 + */
1.88 + public boolean equals(Object o) {
1.89 + if (o == this)
1.90 + return true;
1.91 +
1.92 + if (!(o instanceof Set))
1.93 + return false;
1.94 + Collection c = (Collection) o;
1.95 + if (c.size() != size())
1.96 + return false;
1.97 + try {
1.98 + return containsAll(c);
1.99 + } catch (ClassCastException unused) {
1.100 + return false;
1.101 + } catch (NullPointerException unused) {
1.102 + return false;
1.103 + }
1.104 + }
1.105 +
1.106 + /**
1.107 + * Returns the hash code value for this set. The hash code of a set is
1.108 + * defined to be the sum of the hash codes of the elements in the set,
1.109 + * where the hash code of a <tt>null</tt> element is defined to be zero.
1.110 + * This ensures that <tt>s1.equals(s2)</tt> implies that
1.111 + * <tt>s1.hashCode()==s2.hashCode()</tt> for any two sets <tt>s1</tt>
1.112 + * and <tt>s2</tt>, as required by the general contract of
1.113 + * {@link Object#hashCode}.
1.114 + *
1.115 + * <p>This implementation iterates over the set, calling the
1.116 + * <tt>hashCode</tt> method on each element in the set, and adding up
1.117 + * the results.
1.118 + *
1.119 + * @return the hash code value for this set
1.120 + * @see Object#equals(Object)
1.121 + * @see Set#equals(Object)
1.122 + */
1.123 + public int hashCode() {
1.124 + int h = 0;
1.125 + Iterator<E> i = iterator();
1.126 + while (i.hasNext()) {
1.127 + E obj = i.next();
1.128 + if (obj != null)
1.129 + h += obj.hashCode();
1.130 + }
1.131 + return h;
1.132 + }
1.133 +
1.134 + /**
1.135 + * Removes from this set all of its elements that are contained in the
1.136 + * specified collection (optional operation). If the specified
1.137 + * collection is also a set, this operation effectively modifies this
1.138 + * set so that its value is the <i>asymmetric set difference</i> of
1.139 + * the two sets.
1.140 + *
1.141 + * <p>This implementation determines which is the smaller of this set
1.142 + * and the specified collection, by invoking the <tt>size</tt>
1.143 + * method on each. If this set has fewer elements, then the
1.144 + * implementation iterates over this set, checking each element
1.145 + * returned by the iterator in turn to see if it is contained in
1.146 + * the specified collection. If it is so contained, it is removed
1.147 + * from this set with the iterator's <tt>remove</tt> method. If
1.148 + * the specified collection has fewer elements, then the
1.149 + * implementation iterates over the specified collection, removing
1.150 + * from this set each element returned by the iterator, using this
1.151 + * set's <tt>remove</tt> method.
1.152 + *
1.153 + * <p>Note that this implementation will throw an
1.154 + * <tt>UnsupportedOperationException</tt> if the iterator returned by the
1.155 + * <tt>iterator</tt> method does not implement the <tt>remove</tt> method.
1.156 + *
1.157 + * @param c collection containing elements to be removed from this set
1.158 + * @return <tt>true</tt> if this set changed as a result of the call
1.159 + * @throws UnsupportedOperationException if the <tt>removeAll</tt> operation
1.160 + * is not supported by this set
1.161 + * @throws ClassCastException if the class of an element of this set
1.162 + * is incompatible with the specified collection
1.163 + * (<a href="Collection.html#optional-restrictions">optional</a>)
1.164 + * @throws NullPointerException if this set contains a null element and the
1.165 + * specified collection does not permit null elements
1.166 + * (<a href="Collection.html#optional-restrictions">optional</a>),
1.167 + * or if the specified collection is null
1.168 + * @see #remove(Object)
1.169 + * @see #contains(Object)
1.170 + */
1.171 + public boolean removeAll(Collection<?> c) {
1.172 + boolean modified = false;
1.173 +
1.174 + if (size() > c.size()) {
1.175 + for (Iterator<?> i = c.iterator(); i.hasNext(); )
1.176 + modified |= remove(i.next());
1.177 + } else {
1.178 + for (Iterator<?> i = iterator(); i.hasNext(); ) {
1.179 + if (c.contains(i.next())) {
1.180 + i.remove();
1.181 + modified = true;
1.182 + }
1.183 + }
1.184 + }
1.185 + return modified;
1.186 + }
1.187 +
1.188 +}