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