1 /* Copyright (c) 2001-2014, 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.map.BaseHashMap;
35 
36 /**
37  *
38  * @author Fred Toussi (fredt@users dot sourceforge.net)
39  * @version 2.3.3
40  * @since 1.7.2
41  */
42 public class IntKeyHashMap extends BaseHashMap {
43 
44     Set        keySet;
45     Collection values;
46 
IntKeyHashMap()47     public IntKeyHashMap() {
48         this(8);
49     }
50 
IntKeyHashMap(int initialCapacity)51     public IntKeyHashMap(int initialCapacity) throws IllegalArgumentException {
52         super(initialCapacity, BaseHashMap.intKeyOrValue,
53               BaseHashMap.objectKeyOrValue, false);
54     }
55 
get(int key)56     public Object get(int key) {
57 
58         int lookup = getLookup(key);
59 
60         if (lookup != -1) {
61             return objectValueTable[lookup];
62         }
63 
64         return null;
65     }
66 
put(int key, Object value)67     public Object put(int key, Object value) {
68         return super.addOrRemove(key, value, null, false);
69     }
70 
containsValue(Object value)71     public boolean containsValue(Object value) {
72         return super.containsValue(value);
73     }
74 
remove(int key)75     public Object remove(int key) {
76         return super.addOrRemove(key, null, null, true);
77     }
78 
containsKey(int key)79     public boolean containsKey(int key) {
80         return super.containsKey(key);
81     }
82 
putAll(IntKeyHashMap other)83     public void putAll(IntKeyHashMap other) {
84 
85         Iterator it = other.keySet().iterator();
86 
87         while (it.hasNext()) {
88             int key = it.nextInt();
89 
90             put(key, other.get(key));
91         }
92     }
93 
94 
valuesToArray(Object[] array)95     public void valuesToArray(Object[] array) {
96 
97         Iterator it = values().iterator();
98         int      i  = 0;
99 
100         while (it.hasNext()) {
101             array[i] = it.next();
102 
103             i++;
104         }
105     }
106 
keySet()107     public Set keySet() {
108 
109         if (keySet == null) {
110             keySet = new KeySet();
111         }
112 
113         return keySet;
114     }
115 
values()116     public Collection values() {
117 
118         if (values == null) {
119             values = new Values();
120         }
121 
122         return values;
123     }
124 
125     class KeySet implements Set {
126 
iterator()127         public Iterator iterator() {
128             return IntKeyHashMap.this.new BaseHashIterator(true);
129         }
130 
size()131         public int size() {
132             return IntKeyHashMap.this.size();
133         }
134 
contains(Object o)135         public boolean contains(Object o) {
136             throw new UnsupportedOperationException();
137         }
138 
get(Object key)139         public Object get(Object key) {
140             throw new UnsupportedOperationException();
141         }
142 
add(Object value)143         public boolean add(Object value) {
144             throw new UnsupportedOperationException();
145         }
146 
addAll(Collection c)147         public boolean addAll(Collection c) {
148             throw new UnsupportedOperationException();
149         }
150 
remove(Object o)151         public boolean remove(Object o) {
152             throw new UnsupportedOperationException();
153         }
154 
isEmpty()155         public boolean isEmpty() {
156             return size() == 0;
157         }
158 
clear()159         public void clear() {
160             IntKeyHashMap.this.clear();
161         }
162     }
163 
164     class Values implements Collection {
165 
iterator()166         public Iterator iterator() {
167             return IntKeyHashMap.this.new BaseHashIterator(false);
168         }
169 
size()170         public int size() {
171             return IntKeyHashMap.this.size();
172         }
173 
contains(Object o)174         public boolean contains(Object o) {
175             throw new UnsupportedOperationException();
176         }
177 
add(Object value)178         public boolean add(Object value) {
179             throw new UnsupportedOperationException();
180         }
181 
addAll(Collection c)182         public boolean addAll(Collection c) {
183             throw new UnsupportedOperationException();
184         }
185 
remove(Object o)186         public boolean remove(Object o) {
187             throw new UnsupportedOperationException();
188         }
189 
isEmpty()190         public boolean isEmpty() {
191             return size() == 0;
192         }
193 
clear()194         public void clear() {
195             IntKeyHashMap.this.clear();
196         }
197     }
198 }
199