KickJava   Java API By Example, From Geeks To Geeks.

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


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 HashMap extends BaseHashMap {
44
45     Set keySet;
46     Collection values;
47
48     public HashMap() {
49         this(16, 0.75f);
50     }
51
52     public HashMap(int initialCapacity) throws IllegalArgumentException JavaDoc {
53         this(initialCapacity, 0.75f);
54     }
55
56     public HashMap(int initialCapacity,
57                    float loadFactor) throws IllegalArgumentException JavaDoc {
58         super(initialCapacity, loadFactor, BaseHashMap.objectKeyOrValue,
59               BaseHashMap.objectKeyOrValue, false);
60     }
61
62     public Object JavaDoc get(Object JavaDoc key) {
63
64         int hash = key.hashCode();
65         int lookup = getLookup(key, hash);
66
67         if (lookup != -1) {
68             return objectValueTable[lookup];
69         }
70
71         return null;
72     }
73
74     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
75         return super.addOrRemove(0, 0, key, value, false);
76     }
77
78     public Object JavaDoc remove(Object JavaDoc key) {
79         return super.removeObject(key);
80     }
81
82     public boolean containsKey(Object JavaDoc key) {
83         return super.containsKey(key);
84     }
85
86     public boolean containsValue(Object JavaDoc value) {
87         return super.containsValue(value);
88     }
89
90     public void putAll(HashMap t) {
91
92         Iterator it = t.keySet.iterator();
93
94         while (it.hasNext()) {
95             Object JavaDoc key = it.next();
96
97             put(key, t.get(key));
98         }
99     }
100
101     public Set keySet() {
102
103         if (keySet == null) {
104             keySet = new KeySet();
105         }
106
107         return keySet;
108     }
109
110     public Collection values() {
111
112         if (values == null) {
113             values = new Values();
114         }
115
116         return values;
117     }
118
119     class KeySet implements Set {
120
121         public Iterator iterator() {
122             return HashMap.this.new BaseHashIterator(true);
123         }
124
125         public int size() {
126             return HashMap.this.size();
127         }
128
129         public boolean contains(Object JavaDoc o) {
130             return containsKey(o);
131         }
132
133         public Object JavaDoc get(Object JavaDoc key) {
134
135             int lookup = HashMap.this.getLookup(key, key.hashCode());
136
137             if (lookup < 0) {
138                 return null;
139             } else {
140                 return HashMap.this.objectKeyTable[lookup];
141             }
142         }
143
144         public boolean add(Object JavaDoc value) {
145             throw new RuntimeException JavaDoc();
146         }
147
148         public boolean addAll(Collection c) {
149             throw new RuntimeException JavaDoc();
150         }
151
152         public boolean remove(Object JavaDoc o) {
153
154             int oldSize = size();
155
156             HashMap.this.remove(o);
157
158             return size() != oldSize;
159         }
160
161         public boolean isEmpty() {
162             return size() == 0;
163         }
164
165         public void clear() {
166             HashMap.this.clear();
167         }
168     }
169
170     class Values implements Collection {
171
172         public Iterator iterator() {
173             return HashMap.this.new BaseHashIterator(false);
174         }
175
176         public int size() {
177             return HashMap.this.size();
178         }
179
180         public boolean contains(Object JavaDoc o) {
181             throw new RuntimeException JavaDoc();
182         }
183
184         public boolean add(Object JavaDoc value) {
185             throw new RuntimeException JavaDoc();
186         }
187
188         public boolean addAll(Collection c) {
189             throw new RuntimeException JavaDoc();
190         }
191
192         public boolean remove(Object JavaDoc o) {
193             throw new RuntimeException JavaDoc();
194         }
195
196         public boolean isEmpty() {
197             return size() == 0;
198         }
199
200         public void clear() {
201             HashMap.this.clear();
202         }
203     }
204 }
205
Popular Tags