KickJava   Java API By Example, From Geeks To Geeks.

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


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