GNU Classpath (0.95) | |
Frames | No Frames |
1: /* AbstractSet.java -- Abstract implementation of most of Set 2: Copyright (C) 1998, 2000, 2001, 2004, 2005 3: Free Software Foundation, Inc. 4: 5: This file is part of GNU Classpath. 6: 7: GNU Classpath is free software; you can redistribute it and/or modify 8: it under the terms of the GNU General Public License as published by 9: the Free Software Foundation; either version 2, or (at your option) 10: any later version. 11: 12: GNU Classpath is distributed in the hope that it will be useful, but 13: WITHOUT ANY WARRANTY; without even the implied warranty of 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15: General Public License for more details. 16: 17: You should have received a copy of the GNU General Public License 18: along with GNU Classpath; see the file COPYING. If not, write to the 19: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 20: 02110-1301 USA. 21: 22: Linking this library statically or dynamically with other modules is 23: making a combined work based on this library. Thus, the terms and 24: conditions of the GNU General Public License cover the whole 25: combination. 26: 27: As a special exception, the copyright holders of this library give you 28: permission to link this library with independent modules to produce an 29: executable, regardless of the license terms of these independent 30: modules, and to copy and distribute the resulting executable under 31: terms of your choice, provided that you also meet, for each linked 32: independent module, the terms and conditions of the license of that 33: module. An independent module is a module which is not derived from 34: or based on this library. If you modify this library, you may extend 35: this exception to your version of the library, but you are not 36: obligated to do so. If you do not wish to do so, delete this 37: exception statement from your version. */ 38: 39: 40: package java.util; 41: 42: /** 43: * An abstract implementation of Set to make it easier to create your own 44: * implementations. In order to create a Set, subclass AbstractSet and 45: * implement the same methods that are required for AbstractCollection 46: * (although these methods must of course meet the requirements that Set puts 47: * on them - specifically, no element may be in the set more than once). This 48: * class simply provides implementations of equals() and hashCode() to fulfil 49: * the requirements placed on them by the Set interface. 50: * 51: * @author Original author unknown 52: * @author Eric Blake (ebb9@email.byu.edu) 53: * @see Collection 54: * @see AbstractCollection 55: * @see Set 56: * @see HashSet 57: * @see TreeSet 58: * @see LinkedHashSet 59: * @since 1.2 60: * @status updated to 1.4 61: */ 62: public abstract class AbstractSet<E> 63: extends AbstractCollection<E> 64: implements Set<E> 65: { 66: /** 67: * The main constructor, for use by subclasses. 68: */ 69: protected AbstractSet() 70: { 71: } 72: 73: /** 74: * Tests whether the given object is equal to this Set. This implementation 75: * first checks whether this set <em>is</em> the given object, and returns 76: * true if so. Otherwise, if o is a Set and is the same size as this one, it 77: * returns the result of calling containsAll on the given Set. Otherwise, it 78: * returns false. 79: * 80: * @param o the Object to be tested for equality with this Set 81: * @return true if the given object is equal to this Set 82: */ 83: public boolean equals(Object o) 84: { 85: return (o == this 86: || (o instanceof Set && ((Set) o).size() == size() 87: && containsAll((Collection) o))); 88: } 89: 90: /** 91: * Returns a hash code for this Set. The hash code of a Set is the sum of the 92: * hash codes of all its elements, except that the hash code of null is 93: * defined to be zero. This implementation obtains an Iterator over the Set, 94: * and sums the results. 95: * 96: * @return a hash code for this Set 97: */ 98: public int hashCode() 99: { 100: Iterator<E> itr = iterator(); 101: int hash = 0; 102: int pos = size(); 103: while (--pos >= 0) 104: hash += hashCode(itr.next()); 105: return hash; 106: } 107: 108: /** 109: * Removes from this set all elements in the given collection (optional 110: * operation). This implementation uses <code>size()</code> to determine 111: * the smaller collection. Then, if this set is smaller, it iterates 112: * over the set, calling Iterator.remove if the collection contains 113: * the element. If this set is larger, it iterates over the collection, 114: * calling Set.remove for all elements in the collection. Note that 115: * this operation will fail if a remove methods is not supported. 116: * 117: * @param c the collection of elements to remove 118: * @return true if the set was modified as a result 119: * @throws UnsupportedOperationException if remove is not supported 120: * @throws NullPointerException if the collection is null 121: * @see AbstractCollection#remove(Object) 122: * @see Collection#contains(Object) 123: * @see Iterator#remove() 124: */ 125: public boolean removeAll(Collection<?> c) 126: { 127: int oldsize = size(); 128: int count = c.size(); 129: if (oldsize < count) 130: { 131: Iterator<E> i; 132: for (i = iterator(), count = oldsize; count > 0; count--) 133: { 134: if (c.contains(i.next())) 135: i.remove(); 136: } 137: } 138: else 139: { 140: Iterator<?> i; 141: for (i = c.iterator(); count > 0; count--) 142: remove(i.next()); 143: } 144: return oldsize != size(); 145: } 146: }
GNU Classpath (0.95) |