KickJava   Java API By Example, From Geeks To Geeks.

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


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  * @author fredt@users
40  * @version 1.7.2
41  * @since 1.7.2
42  */

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