KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.NoSuchElementException JavaDoc;
35
36 import org.hsqldb.store.BaseHashMap;
37
38 /**
39  * This class does not store null keys.
40  *
41  * @author fredt@users
42  * @version 1.7.2
43  * @since 1.7.2
44  */

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