001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb.lib;
032:
033: import org.hsqldb.store.BaseHashMap;
034:
035: /**
036: * This class does not store null keys.
037: *
038: * @author fredt@users
039: * @version 1.7.2
040: * @since 1.7.2
041: */
042: public class HashSet extends BaseHashMap implements Set {
043:
044: public HashSet() {
045: this (16, 0.75f);
046: }
047:
048: public HashSet(int initialCapacity) throws IllegalArgumentException {
049: this (initialCapacity, 0.75f);
050: }
051:
052: public HashSet(int initialCapacity, float loadFactor)
053: throws IllegalArgumentException {
054: super (initialCapacity, loadFactor,
055: BaseHashMap.objectKeyOrValue, BaseHashMap.noKeyOrValue,
056: false);
057: }
058:
059: public boolean contains(Object key) {
060: return super .containsKey(key);
061: }
062:
063: public Object get(Object key) {
064:
065: int lookup = getLookup(key, key.hashCode());
066:
067: if (lookup < 0) {
068: return null;
069: } else {
070: return objectKeyTable[lookup];
071: }
072: }
073:
074: public boolean add(Object key) {
075:
076: int oldSize = size();
077:
078: super .addOrRemove(0, 0, key, null, false);
079:
080: return oldSize != size();
081: }
082:
083: public boolean addAll(Collection c) {
084:
085: int oldSize = size();
086: Iterator it = c.iterator();
087:
088: while (it.hasNext()) {
089: add(it.next());
090: }
091:
092: return oldSize != size();
093: }
094:
095: public boolean addAll(Object[] keys) {
096:
097: boolean changed = false;
098:
099: for (int i = 0; i < keys.length; i++) {
100: if (add(keys[i])) {
101: changed = true;
102: }
103: }
104:
105: return changed;
106: }
107:
108: public boolean remove(Object key) {
109:
110: int oldSize = size();
111:
112: super .removeObject(key);
113:
114: return oldSize != size();
115: }
116:
117: public Object[] toArray(Object[] a) {
118:
119: if (a == null || a.length < size()) {
120: a = new Object[size()];
121: }
122:
123: Iterator it = iterator();
124:
125: for (int i = 0; it.hasNext(); i++) {
126: a[i] = it.next();
127: }
128:
129: return a;
130: }
131:
132: public Iterator iterator() {
133: return new BaseHashIterator(true);
134: }
135:
136: /**
137: * Returns a String like "[Drei, zwei, Eins]", exactly like
138: * java.util.HashSet.
139: */
140: public String toString() {
141:
142: Iterator it = iterator();
143: StringBuffer sb = new StringBuffer();
144:
145: while (it.hasNext()) {
146: if (sb.length() > 0) {
147: sb.append(", ");
148: } else {
149: sb.append('[');
150: }
151:
152: sb.append(it.next());
153: }
154:
155: return sb.toString() + ']';
156: }
157: }
|