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