KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > AbstractSet


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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  * Abstract base class for set implementations.
20  */

21 public abstract class AbstractSet extends AbstractCollection JavaDoc implements Set JavaDoc {
22
23   public boolean equals(Object JavaDoc o) {
24     if (o == this) {
25       return true;
26     }
27
28     if (!(o instanceof Set JavaDoc)) {
29       return false;
30     }
31
32     Set JavaDoc other = (Set JavaDoc) o;
33
34     if (other.size() != size()) {
35       return false;
36     }
37
38     for (Iterator JavaDoc iter = other.iterator(); iter.hasNext();) {
39       Object JavaDoc otherItem = iter.next();
40       if (!contains(otherItem)) {
41         return false;
42       }
43     }
44     return true;
45   }
46
47   public int hashCode() {
48     int hashCode = 0;
49     for (Iterator JavaDoc iter = iterator(); iter.hasNext();) {
50       // Sets can have null members
51
Object JavaDoc next = iter.next();
52       if (next != null) {
53         hashCode += next.hashCode();
54       }
55     }
56     return hashCode;
57   }
58
59   public boolean removeAll(Collection JavaDoc c) {
60     int size = size();
61     if (size < c.size()) {
62       // If the member of 'this' is in 'c', remove it from 'this'.
63
//
64
for (Iterator JavaDoc iter = iterator(); iter.hasNext();) {
65         Object JavaDoc o = iter.next();
66         if (c.contains(o)) {
67           iter.remove();
68         }
69       }
70     } else {
71       // Remove every member of 'c' from 'this'.
72
//
73
for (Iterator JavaDoc iter = c.iterator(); iter.hasNext();) {
74         Object JavaDoc o = iter.next();
75         remove(o);
76       }
77     }
78     return (size != size());
79   }
80
81 }
82
Popular Tags