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: * @author fredt@users
039: * @version 1.7.2
040: * @since 1.7.2
041: */
042: public class IntKeyIntValueHashMap extends BaseHashMap {
043:
044: private Set keySet;
045: private Collection values;
046:
047: public IntKeyIntValueHashMap() {
048: this (16, 0.75f);
049: }
050:
051: public IntKeyIntValueHashMap(int initialCapacity)
052: throws IllegalArgumentException {
053: this (initialCapacity, 0.75f);
054: }
055:
056: public IntKeyIntValueHashMap(int initialCapacity, float loadFactor)
057: throws IllegalArgumentException {
058: super (initialCapacity, loadFactor, BaseHashMap.intKeyOrValue,
059: BaseHashMap.intKeyOrValue, false);
060: }
061:
062: public int get(int key) throws NoSuchElementException {
063:
064: int lookup = getLookup(key);
065:
066: if (lookup != -1) {
067: return intValueTable[lookup];
068: }
069:
070: throw new NoSuchElementException();
071: }
072:
073: public int get(int key, int defaultValue) {
074:
075: int lookup = getLookup(key);
076:
077: if (lookup != -1) {
078: return intValueTable[lookup];
079: }
080:
081: return defaultValue;
082: }
083:
084: public boolean get(int key, int[] value) {
085:
086: int lookup = getLookup(key);
087:
088: if (lookup != -1) {
089: value[0] = intValueTable[lookup];
090:
091: return true;
092: }
093:
094: return false;
095: }
096:
097: public boolean put(int key, int value) {
098:
099: int oldSize = size();
100:
101: super .addOrRemove(key, value, null, null, false);
102:
103: return oldSize != size();
104: }
105:
106: public boolean remove(int key) {
107:
108: int oldSize = size();
109:
110: super .addOrRemove(key, 0, null, null, true);
111:
112: return oldSize != size();
113: }
114:
115: public Set keySet() {
116:
117: if (keySet == null) {
118: keySet = new KeySet();
119: }
120:
121: return keySet;
122: }
123:
124: public Collection values() {
125:
126: if (values == null) {
127: values = new Values();
128: }
129:
130: return values;
131: }
132:
133: class KeySet implements Set {
134:
135: public Iterator iterator() {
136: return IntKeyIntValueHashMap.this .new BaseHashIterator(true);
137: }
138:
139: public int size() {
140: return IntKeyIntValueHashMap.this .size();
141: }
142:
143: public boolean contains(Object o) {
144: throw new RuntimeException();
145: }
146:
147: public Object get(Object key) {
148: throw new RuntimeException();
149: }
150:
151: public boolean add(Object value) {
152: throw new RuntimeException();
153: }
154:
155: public boolean addAll(Collection c) {
156: throw new RuntimeException();
157: }
158:
159: public boolean remove(Object o) {
160: throw new RuntimeException();
161: }
162:
163: public boolean isEmpty() {
164: return size() == 0;
165: }
166:
167: public void clear() {
168: IntKeyIntValueHashMap.this .clear();
169: }
170: }
171:
172: class Values implements Collection {
173:
174: public Iterator iterator() {
175: return IntKeyIntValueHashMap.this .new BaseHashIterator(
176: false);
177: }
178:
179: public int size() {
180: return IntKeyIntValueHashMap.this .size();
181: }
182:
183: public boolean contains(Object o) {
184: throw new RuntimeException();
185: }
186:
187: public boolean add(Object value) {
188: throw new RuntimeException();
189: }
190:
191: public boolean addAll(Collection c) {
192: throw new RuntimeException();
193: }
194:
195: public boolean remove(Object o) {
196: throw new RuntimeException();
197: }
198:
199: public boolean isEmpty() {
200: return size() == 0;
201: }
202:
203: public void clear() {
204: IntKeyIntValueHashMap.this.clear();
205: }
206: }
207: }
|