1 23 24 package com.sun.ejb.containers.util; 25 26 import java.io.*; 27 import java.util.*; 28 import java.util.ArrayList ; 29 import java.security.*; 30 31 32 public class LongHashMap { 33 34 protected static final int DEFAULT_CAPACITY = 128; 35 protected static final float DEFAULT_LOAD_FACTOR = 0.75f; 36 protected static final int MAXIMUM_CAPACITY = 1 << 30; 37 38 protected static boolean debug = false; 39 40 protected transient Entry[] table; 41 protected float loadFactor; 42 protected transient int size; 43 protected int bucketmask; 44 protected int capacity; 45 protected int threshold; 46 47 public LongHashMap() { 48 this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR); 49 } 50 51 public LongHashMap(int initialCapacity) { 52 this(initialCapacity, DEFAULT_LOAD_FACTOR); 53 } 54 55 public LongHashMap(int initialCapacity, float loadFactor) { 56 if (initialCapacity > MAXIMUM_CAPACITY) { 57 initialCapacity = MAXIMUM_CAPACITY; 58 } 59 if (loadFactor <= 0 || Float.isNaN(loadFactor)) { 60 throw new IllegalArgumentException ("Illegal load factor: " 61 + loadFactor); 62 } 63 64 capacity = 1; 66 67 while (capacity < initialCapacity) { 68 capacity <<= 1; 69 } 70 this.loadFactor = loadFactor; 71 this.threshold = (int)(capacity * loadFactor); 72 this.table = new Entry[capacity]; 73 this.bucketmask = capacity - 1; 74 } 75 76 77 public boolean contains(long key) { 78 int index = ((int) key) & bucketmask; 79 for (Entry e = table[index]; e != null; e = e.next) { 80 if (e.key == key) { 81 return true; 82 } 83 } 84 return false; 85 } 86 87 public Object get(long key) { 88 int index = ((int) key) & bucketmask; 89 for (Entry e = table[index]; e != null; e = e.next) { 90 if (e.key == key) { 91 return e.value; 92 } 93 } 94 return null; 95 } 96 97 public Object put(long key, Object value) { 98 int index = ((int) key) & bucketmask; 99 100 for (Entry e = table[index]; e != null; e = e.next) { 101 if (e.key == key) { 102 Object oldValue = e.value; 103 e.value = value; 104 return oldValue; 105 } 106 } 107 108 table[index] = new Entry(key, value, table[index]); 109 if (size++ >= threshold) { 110 int newCapacity = 2 * capacity; 111 Entry[] newTable = new Entry[newCapacity]; 112 transfer(newTable); 113 table = newTable; 114 capacity = newCapacity; 115 threshold = (int)(newCapacity * loadFactor); 116 bucketmask = capacity-1; 117 } 118 return null; 119 } 120 121 124 private void transfer(Entry[] newTable) { 125 Entry[] src = table; 126 int newCapacity = newTable.length; 127 int bucketmask = newCapacity-1; 128 for (int j = 0; j < src.length; j++) { 129 Entry e = src[j]; 130 if (e != null) { 131 src[j] = null; 132 do { 133 Entry next = e.next; 134 int index = ((int) e.key) & bucketmask; 135 e.next = newTable[index]; 136 newTable[index] = e; 137 e = next; 138 } while (e != null); 139 } 140 } 141 } 142 143 public Object remove(long key) { 144 int index = ((int) key) & bucketmask; 145 Entry prev = table[index]; 146 Entry e = prev; 147 148 while (e != null) { 149 Entry next = e.next; 150 if (e.key == key) { 151 size--; 152 if (prev == e) { 153 table[index] = next; 154 } else { 155 prev.next = next; 156 } 157 return e.value; 158 } 159 prev = e; 160 e = next; 161 } 162 163 return null; 164 } 165 166 public Enumeration elements() { 167 Vector keyList = new Vector(); 168 for (int index=0; index<capacity; index++) { 169 for (Entry e = table[index]; e != null; e = e.next) { 170 keyList.addElement(e.value); 171 } 172 } 173 174 return keyList.elements(); 175 } 176 177 public Iterator values() { 178 ArrayList keyList = new ArrayList (); 179 for (int index=0; index<capacity; index++) { 180 for (Entry e = table[index]; e != null; e = e.next) { 181 keyList.add(e.value); 182 } 183 } 184 185 return keyList.iterator(); 186 } 187 188 public Iterator keys() { 189 ArrayList keyList = new ArrayList (); 190 for (int index=0; index<capacity; index++) { 191 for (Entry e = table[index]; e != null; e = e.next) { 192 keyList.add(new Long (e.key)); 193 } 194 } 195 196 return keyList.iterator(); 197 } 198 199 static class Entry { 200 final long key; 201 Object value; 202 Entry next; 203 204 207 Entry(long k, Object v, Entry n) { 208 key = k; 209 value = v; 210 next = n; 211 } 212 213 public int hashCode() { 214 return ((int) key); 215 } 216 217 public String toString() { 218 return key + "=" + value; 219 } 220 } 221 } 222 | Popular Tags |