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 java.util.NoSuchElementException;
034:
035: import org.hsqldb.store.BaseHashMap;
036:
037: /**
038: * This class does not store null keys.
039: *
040: * @author fredt@users
041: * @version 1.7.2
042: * @since 1.7.2
043: */
044: public class IntValueHashMap extends BaseHashMap {
045:
046: Set keySet;
047:
048: public IntValueHashMap() {
049: this (16, 0.75f);
050: }
051:
052: public IntValueHashMap(int initialCapacity)
053: throws IllegalArgumentException {
054: this (initialCapacity, 0.75f);
055: }
056:
057: public IntValueHashMap(int initialCapacity, float loadFactor)
058: throws IllegalArgumentException {
059: super (initialCapacity, loadFactor,
060: BaseHashMap.objectKeyOrValue,
061: BaseHashMap.intKeyOrValue, false);
062: }
063:
064: public int get(Object key) throws NoSuchElementException {
065:
066: if (key == null) {
067: throw new NoSuchElementException();
068: }
069:
070: int hash = key.hashCode();
071: int lookup = getLookup(key, hash);
072:
073: if (lookup != -1) {
074: return intValueTable[lookup];
075: }
076:
077: throw new NoSuchElementException();
078: }
079:
080: public int get(Object key, int defaultValue) {
081:
082: if (key == null) {
083: throw new NoSuchElementException();
084: }
085:
086: int hash = key.hashCode();
087: int lookup = getLookup(key, hash);
088:
089: if (lookup != -1) {
090: return intValueTable[lookup];
091: }
092:
093: return defaultValue;
094: }
095:
096: public boolean get(Object key, int[] value) {
097:
098: if (key == null) {
099: throw new NoSuchElementException();
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 key, int value) {
115:
116: if (key == null) {
117: throw new NoSuchElementException();
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 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 key) {
137: return super .containsKey(key);
138: }
139:
140: public boolean containsValue(int value) {
141: throw new RuntimeException();
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 o) {
164: return containsKey(o);
165: }
166:
167: public Object get(Object key) {
168:
169: int lookup = IntValueHashMap.this .getLookup(key, key
170: .hashCode());
171:
172: if (lookup < 0) {
173: return null;
174: } else {
175: return IntValueHashMap.this .objectKeyTable[lookup];
176: }
177: }
178:
179: public boolean add(Object value) {
180: throw new RuntimeException();
181: }
182:
183: public boolean addAll(Collection c) {
184: throw new RuntimeException();
185: }
186:
187: public boolean remove(Object o) {
188:
189: int oldSize = size();
190:
191: IntValueHashMap.this .remove(o);
192:
193: return size() != oldSize;
194: }
195:
196: public boolean isEmpty() {
197: return size() == 0;
198: }
199:
200: public void clear() {
201: IntValueHashMap.this .clear();
202: }
203: }
204:
205: public void putAll(IntValueHashMap t) {
206:
207: Iterator it = t.keySet().iterator();
208:
209: while (it.hasNext()) {
210: Object key = it.next();
211:
212: put(key, t.get(key));
213: }
214: }
215: }
|