01: /*
02: * Copyright 2007 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13: * License for the specific language governing permissions and limitations under
14: * the License.
15: */
16: package java.util;
17:
18: /**
19: * Skeletal implementation of the Set interface. <a
20: * href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/AbstractSet.html">[Sun
21: * docs]</a>
22: *
23: * @param <E> the element type.
24: */
25: public abstract class AbstractSet<E> extends AbstractCollection<E>
26: implements Set<E> {
27:
28: @Override
29: public boolean equals(Object o) {
30: if (o == this ) {
31: return true;
32: }
33:
34: if (!(o instanceof Set)) {
35: return false;
36: }
37:
38: Set<?> other = (Set<?>) o;
39:
40: if (other.size() != size()) {
41: return false;
42: }
43:
44: for (Iterator<?> iter = other.iterator(); iter.hasNext();) {
45: Object otherItem = iter.next();
46: if (!contains(otherItem)) {
47: return false;
48: }
49: }
50: return true;
51: }
52:
53: @Override
54: public int hashCode() {
55: int hashCode = 0;
56: for (Iterator<E> iter = iterator(); iter.hasNext();) {
57: // Sets can have null members
58: E next = iter.next();
59: if (next != null) {
60: hashCode += next.hashCode();
61: }
62: }
63: return hashCode;
64: }
65:
66: @Override
67: public boolean removeAll(Collection<?> c) {
68: int size = size();
69: if (size < c.size()) {
70: // If the member of 'this' is in 'c', remove it from 'this'.
71: //
72: for (Iterator<E> iter = iterator(); iter.hasNext();) {
73: E o = iter.next();
74: if (c.contains(o)) {
75: iter.remove();
76: }
77: }
78: } else {
79: // Remove every member of 'c' from 'this'.
80: //
81: for (Iterator<?> iter = c.iterator(); iter.hasNext();) {
82: Object o = iter.next();
83: remove(o);
84: }
85: }
86: return (size != size());
87: }
88:
89: }
|