001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.util;
019:
020: /**
021: * AbstractSet is an abstract implementation of the Set interface. This
022: * Implementation does not support adding. A subclass must implement the
023: * abstract methods iterator() and size().
024: *
025: * @since 1.2
026: */
027: public abstract class AbstractSet<E> extends AbstractCollection<E>
028: implements Set<E> {
029:
030: /**
031: * Constructs a new instance of this AbstractSet.
032: */
033: protected AbstractSet() {
034: super ();
035: }
036:
037: /**
038: * Compares the specified object to this Set and answer if they are equal.
039: * The object must be an instance of Set and contain the same objects.
040: *
041: * @param object
042: * the object to compare with this object
043: * @return true if the specified object is equal to this Set, false
044: * otherwise
045: *
046: * @see #hashCode
047: */
048: @Override
049: public boolean equals(Object object) {
050: if (this == object) {
051: return true;
052: }
053: if (object instanceof Set) {
054: Set<?> s = (Set<?>) object;
055:
056: try {
057: return size() == s.size() && containsAll(s);
058: } catch (ClassCastException cce) {
059: return false;
060: }
061: }
062: return false;
063: }
064:
065: /**
066: * Answers an integer hash code for the receiver. Objects which are equal
067: * answer the same value for this method.
068: *
069: * @return the receiver's hash
070: *
071: * @see #equals
072: */
073: @Override
074: public int hashCode() {
075: int result = 0;
076: Iterator<?> it = iterator();
077: while (it.hasNext()) {
078: Object next = it.next();
079: result += next == null ? 0 : next.hashCode();
080: }
081: return result;
082: }
083:
084: /**
085: * Removes all occurrences in this Collection of each object in the
086: * specified Collection.
087: *
088: * @param collection
089: * the Collection of objects to remove
090: * @return true if this Collection is modified, false otherwise
091: *
092: * @exception UnsupportedOperationException
093: * when removing from this Collection is not supported
094: */
095: @Override
096: public boolean removeAll(Collection<?> collection) {
097: boolean result = false;
098: if (size() <= collection.size()) {
099: Iterator<?> it = iterator();
100: while (it.hasNext()) {
101: if (collection.contains(it.next())) {
102: it.remove();
103: result = true;
104: }
105: }
106: } else {
107: Iterator<?> it = collection.iterator();
108: while (it.hasNext()) {
109: result = remove(it.next()) || result;
110: }
111: }
112: return result;
113: }
114: }
|