KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > lib > HashSet


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.lib;
33
34 import org.hsqldb.store.BaseHashMap;
35
36 /**
37  * This class does not store null keys.
38  *
39  * @author fredt@users
40  * @version 1.7.2
41  * @since 1.7.2
42  */

43 public class HashSet extends BaseHashMap implements Set {
44
45     public HashSet() {
46         this(16, 0.75f);
47     }
48
49     public HashSet(int initialCapacity) throws IllegalArgumentException JavaDoc {
50         this(initialCapacity, 0.75f);
51     }
52
53     public HashSet(int initialCapacity,
54                    float loadFactor) throws IllegalArgumentException JavaDoc {
55         super(initialCapacity, loadFactor, BaseHashMap.objectKeyOrValue,
56               BaseHashMap.noKeyOrValue, false);
57     }
58
59     public boolean contains(Object JavaDoc key) {
60         return super.containsKey(key);
61     }
62
63     public Object JavaDoc get(Object JavaDoc key) {
64
65         int lookup = getLookup(key, key.hashCode());
66
67         if (lookup < 0) {
68             return null;
69         } else {
70             return objectKeyTable[lookup];
71         }
72     }
73
74     public boolean add(Object JavaDoc key) {
75
76         int oldSize = size();
77
78         super.addOrRemove(0, 0, key, null, false);
79
80         return oldSize != size();
81     }
82
83     public boolean addAll(Collection c) {
84
85         int oldSize = size();
86         Iterator it = c.iterator();
87
88         while (it.hasNext()) {
89             add(it.next());
90         }
91
92         return oldSize != size();
93     }
94
95     public boolean addAll(Object JavaDoc[] keys) {
96
97         boolean changed = false;
98
99         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 JavaDoc key) {
109
110         int oldSize = size();
111
112         super.removeObject(key);
113
114         return oldSize != size();
115     }
116
117     public Object JavaDoc[] toArray(Object JavaDoc[] a) {
118
119         if (a == null || a.length < size()) {
120             a = new Object JavaDoc[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 JavaDoc toString() {
141
142         Iterator it = iterator();
143         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 }
158
Popular Tags