1 30 31 32 package org.hsqldb.lib; 33 34 import java.util.NoSuchElementException ; 35 36 import org.hsqldb.store.BaseHashMap; 37 38 45 public class IntValueHashMap extends BaseHashMap { 46 47 Set keySet; 48 49 public IntValueHashMap() { 50 this(16, 0.75f); 51 } 52 53 public IntValueHashMap(int initialCapacity) 54 throws IllegalArgumentException { 55 this(initialCapacity, 0.75f); 56 } 57 58 public IntValueHashMap(int initialCapacity, 59 float loadFactor) throws IllegalArgumentException { 60 super(initialCapacity, loadFactor, BaseHashMap.objectKeyOrValue, 61 BaseHashMap.intKeyOrValue, false); 62 } 63 64 public int get(Object key) throws NoSuchElementException { 65 66 if (key == null) { 67 throw new NoSuchElementException (); 68 } 69 70 int hash = key.hashCode(); 71 int lookup = getLookup(key, hash); 72 73 if (lookup != -1) { 74 return intValueTable[lookup]; 75 } 76 77 throw new NoSuchElementException (); 78 } 79 80 public int get(Object key, int defaultValue) { 81 82 if (key == null) { 83 throw new NoSuchElementException (); 84 } 85 86 int hash = key.hashCode(); 87 int lookup = getLookup(key, hash); 88 89 if (lookup != -1) { 90 return intValueTable[lookup]; 91 } 92 93 return defaultValue; 94 } 95 96 public boolean get(Object key, int[] value) { 97 98 if (key == null) { 99 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.hashCode()); 170 171 if (lookup < 0) { 172 return null; 173 } else { 174 return IntValueHashMap.this.objectKeyTable[lookup]; 175 } 176 } 177 178 public boolean add(Object value) { 179 throw new RuntimeException (); 180 } 181 182 public boolean addAll(Collection c) { 183 throw new RuntimeException (); 184 } 185 186 public boolean remove(Object o) { 187 188 int oldSize = size(); 189 190 IntValueHashMap.this.remove(o); 191 192 return size() != oldSize; 193 } 194 195 public boolean isEmpty() { 196 return size() == 0; 197 } 198 199 public void clear() { 200 IntValueHashMap.this.clear(); 201 } 202 } 203 204 public void putAll(IntValueHashMap t) { 205 206 Iterator it = t.keySet().iterator(); 207 208 while (it.hasNext()) { 209 Object key = it.next(); 210 211 put(key, t.get(key)); 212 } 213 } 214 } 215 | Popular Tags |