1 |
/* AbstractSet.java -- Abstract implementation of most of Set |
/* AbstractSet.java -- Abstract implementation of most of Set |
2 |
Copyright (C) 1998, 2000 Free Software Foundation, Inc. |
Copyright (C) 1998, 2000, 2001 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
35 |
* on them - specifically, no element may be in the set more than once). This |
* on them - specifically, no element may be in the set more than once). This |
36 |
* class simply provides implementations of equals() and hashCode() to fulfil |
* class simply provides implementations of equals() and hashCode() to fulfil |
37 |
* the requirements placed on them by the Set interface. |
* the requirements placed on them by the Set interface. |
38 |
|
* |
39 |
|
* @author Original author unknown |
40 |
|
* @author Eric Blake <ebb9@email.byu.edu> |
41 |
|
* @see Collection |
42 |
|
* @see AbstractCollection |
43 |
|
* @see Set |
44 |
|
* @see HashSet |
45 |
|
* @see TreeSet |
46 |
|
* @see LinkedHashSet |
47 |
|
* @since 1.2 |
48 |
|
* @status updated to 1.4 |
49 |
*/ |
*/ |
50 |
public abstract class AbstractSet extends AbstractCollection implements Set |
public abstract class AbstractSet extends AbstractCollection implements Set |
51 |
{ |
{ |
52 |
/** |
/** |
53 |
|
* The main constructor, for use by subclasses. |
54 |
|
*/ |
55 |
|
protected AbstractSet() |
56 |
|
{ |
57 |
|
} |
58 |
|
|
59 |
|
/** |
60 |
* Tests whether the given object is equal to this Set. This implementation |
* Tests whether the given object is equal to this Set. This implementation |
61 |
* first checks whether this set <em>is</em> the given object, and returns |
* first checks whether this set <em>is</em> the given object, and returns |
62 |
* true if so. Otherwise, if o is a Set and is the same size as this one, it |
* true if so. Otherwise, if o is a Set and is the same size as this one, it |
68 |
*/ |
*/ |
69 |
public boolean equals(Object o) |
public boolean equals(Object o) |
70 |
{ |
{ |
71 |
if (o == this) |
return (o == this || |
72 |
return true; |
(o instanceof Set && ((Set) o).size() == size() |
73 |
else if (o instanceof Set && ((Set) o).size() == size()) |
&& containsAll((Collection) o))); |
|
return containsAll((Collection) o); |
|
|
else |
|
|
return false; |
|
74 |
} |
} |
75 |
|
|
76 |
/** |
/** |
84 |
public int hashCode() |
public int hashCode() |
85 |
{ |
{ |
86 |
Iterator itr = iterator(); |
Iterator itr = iterator(); |
|
int size = size(); |
|
87 |
int hash = 0; |
int hash = 0; |
88 |
for (int pos = 0; pos < size; pos++) |
int pos = size(); |
89 |
{ |
while (--pos >= 0) |
90 |
Object obj = itr.next(); |
hash += hashCode(itr.next()); |
|
if (obj != null) |
|
|
hash += obj.hashCode(); |
|
|
} |
|
91 |
return hash; |
return hash; |
92 |
} |
} |
93 |
|
|
94 |
|
/** |
95 |
|
* Removes from this set all elements in the given collection (optional |
96 |
|
* operation). This implementation uses <code>size()</code> to determine |
97 |
|
* the smaller collection. Then, if this set is smaller, it iterates |
98 |
|
* over the set, calling Iterator.remove if the collection contains |
99 |
|
* the element. If this set is larger, it iterates over the collection, |
100 |
|
* calling Set.remove for all elements in the collection. Note that |
101 |
|
* this operation will fail if a remove methods is not supported. |
102 |
|
* |
103 |
|
* @param c the collection of elements to remove |
104 |
|
* @return true if the set was modified as a result |
105 |
|
* @throws UnsupportedOperationException if remove is not supported |
106 |
|
* @throws NullPointerException if the collection is null |
107 |
|
* @see AbstractCollection#remove(Object) |
108 |
|
* @see Collection#contains(Object) |
109 |
|
* @see Iterator#remove() |
110 |
|
*/ |
111 |
|
public boolean removeAll(Collection c) |
112 |
|
{ |
113 |
|
int oldsize = size(); |
114 |
|
int count = c.size(); |
115 |
|
Iterator i; |
116 |
|
if (oldsize < count) |
117 |
|
for (i = iterator(), count = oldsize; count > 0; count--) |
118 |
|
if (c.contains(i.next())) |
119 |
|
i.remove(); |
120 |
|
else |
121 |
|
for (i = c.iterator(); count > 0; count--) |
122 |
|
remove(i.next()); |
123 |
|
return oldsize != size(); |
124 |
|
} |
125 |
|
|
126 |
} |
} |