001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb.lib;
032:
033: import org.hsqldb.store.BaseHashMap;
034:
035: /**
036: * This class does not store null keys.
037: *
038: * @author fredt@users
039: * @version 1.7.2
040: * @since 1.7.2
041: */
042: public class HashMap extends BaseHashMap {
043:
044: Set keySet;
045: Collection values;
046:
047: public HashMap() {
048: this (16, 0.75f);
049: }
050:
051: public HashMap(int initialCapacity) throws IllegalArgumentException {
052: this (initialCapacity, 0.75f);
053: }
054:
055: public HashMap(int initialCapacity, float loadFactor)
056: throws IllegalArgumentException {
057: super (initialCapacity, loadFactor,
058: BaseHashMap.objectKeyOrValue,
059: BaseHashMap.objectKeyOrValue, false);
060: }
061:
062: public Object get(Object key) {
063:
064: int hash = key.hashCode();
065: int lookup = getLookup(key, hash);
066:
067: if (lookup != -1) {
068: return objectValueTable[lookup];
069: }
070:
071: return null;
072: }
073:
074: public Object put(Object key, Object value) {
075: return super .addOrRemove(0, 0, key, value, false);
076: }
077:
078: public Object remove(Object key) {
079: return super .removeObject(key);
080: }
081:
082: public boolean containsKey(Object key) {
083: return super .containsKey(key);
084: }
085:
086: public boolean containsValue(Object value) {
087: return super .containsValue(value);
088: }
089:
090: public void putAll(HashMap t) {
091:
092: Iterator it = t.keySet.iterator();
093:
094: while (it.hasNext()) {
095: Object key = it.next();
096:
097: put(key, t.get(key));
098: }
099: }
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 o) {
130: return containsKey(o);
131: }
132:
133: public Object get(Object 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 value) {
145: throw new RuntimeException();
146: }
147:
148: public boolean addAll(Collection c) {
149: throw new RuntimeException();
150: }
151:
152: public boolean remove(Object 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 o) {
181: throw new RuntimeException();
182: }
183:
184: public boolean add(Object value) {
185: throw new RuntimeException();
186: }
187:
188: public boolean addAll(Collection c) {
189: throw new RuntimeException();
190: }
191:
192: public boolean remove(Object o) {
193: throw new RuntimeException();
194: }
195:
196: public boolean isEmpty() {
197: return size() == 0;
198: }
199:
200: public void clear() {
201: HashMap.this.clear();
202: }
203: }
204: }
|