1 30 31 32 package org.hsqldb.lib; 33 34 45 public class HashMappedList extends HashMap { 46 47 public HashMappedList() { 48 this(16, 0.75f); 49 } 50 51 public HashMappedList(int initialCapacity) 52 throws IllegalArgumentException { 53 this(initialCapacity, 0.75f); 54 } 55 56 public HashMappedList(int initialCapacity, 57 float loadFactor) throws IllegalArgumentException { 58 super(initialCapacity, loadFactor); 59 } 60 61 public Object get(int index) throws IndexOutOfBoundsException { 62 63 checkRange(index); 64 65 return objectValueTable[index]; 66 } 67 68 public Object remove(Object key) { 69 70 int lookup = getLookup(key, key.hashCode()); 71 72 if (lookup < 0) { 73 return null; 74 } 75 76 Object returnValue = super.remove(key); 77 78 removeRow(lookup); 79 80 return returnValue; 81 } 82 83 public Object remove(int index) throws IndexOutOfBoundsException { 84 85 checkRange(index); 86 87 return remove(objectKeyTable[index]); 88 } 89 90 public boolean add(Object key, Object value) { 91 92 if (keySet().contains(key)) { 93 return false; 94 } 95 96 super.put(key, value); 97 98 return true; 99 } 100 101 public Object put(Object key, Object value) { 102 return super.put(key, value); 103 } 104 105 public Object set(int index, 106 Object value) throws IndexOutOfBoundsException { 107 108 checkRange(index); 109 110 Object returnValue = objectKeyTable[index]; 111 112 objectKeyTable[index] = value; 113 114 return returnValue; 115 } 116 117 public boolean insert(int index, Object key, 118 Object value) throws IndexOutOfBoundsException { 119 120 if (index < 0 || index > size()) { 121 throw new IndexOutOfBoundsException (); 122 } 123 124 if (keySet().contains(key)) { 125 return false; 126 } 127 128 if (index == size()) { 129 return add(key, value); 130 } 131 132 HashMappedList hm = new HashMappedList(size()); 133 134 for (int i = index; i < size(); i++) { 135 hm.add(getKey(i), get(i)); 136 } 137 138 for (int i = size() - 1; i >= index; i--) { 139 remove(i); 140 } 141 142 for (int i = 0; i < hm.size(); i++) { 143 add(hm.getKey(i), hm.get(i)); 144 } 145 146 return true; 147 } 148 149 public boolean set(int index, Object key, 150 Object value) throws IndexOutOfBoundsException { 151 152 checkRange(index); 153 154 if (keySet().contains(key) && getIndex(key) != index) { 155 return false; 156 } 157 158 super.remove(objectKeyTable[index]); 159 super.put(key, value); 160 161 return true; 162 } 163 164 public boolean setKey(int index, 165 Object key) throws IndexOutOfBoundsException { 166 167 checkRange(index); 168 169 Object value = objectValueTable[index]; 170 171 return set(index, key, value); 172 } 173 174 public Object getKey(int index) throws IndexOutOfBoundsException { 175 176 checkRange(index); 177 178 return objectKeyTable[index]; 179 } 180 181 public int getIndex(Object key) { 182 return getLookup(key, key.hashCode()); 183 } 184 185 private void checkRange(int i) { 186 187 if (i < 0 || i >= size()) { 188 throw new IndexOutOfBoundsException (); 189 } 190 } 191 } 192 | Popular Tags |