1 package fr.dyade.aaa.agent; 2 3 import java.util.Enumeration ; 4 import java.util.NoSuchElementException ; 5 6 9 public class ServersHT { 10 11 private transient ServerDescEntry table[]; 12 13 private transient int count; 14 15 16 private static final int initialCapacity = 13; 17 18 private static final float loadFactor = 0.75f; 19 20 private int threshold; 21 22 23 private transient int modCount = 0; 24 25 29 public ServersHT() { 30 table = new ServerDescEntry[initialCapacity]; 31 threshold = (int)(initialCapacity * loadFactor); 32 } 33 34 39 public synchronized int size() { 40 return count; 41 } 42 43 49 public synchronized Enumeration keys() { 50 return new Enumerator(KEYS); 51 } 52 53 61 public synchronized Enumeration elements() { 62 return new Enumerator(VALUES); 63 } 64 65 71 public synchronized ServerDesc get(short sid) { 72 ServerDescEntry tab[] = table; 73 int index = (sid & 0x7FFF) % tab.length; 74 for (ServerDescEntry e = tab[index] ; e != null ; e = e.next) { 75 if (e.desc.sid == sid) return e.desc; 76 } 77 return null; 78 } 79 80 87 protected void rehash() { 88 int oldCapacity = table.length; 89 ServerDescEntry oldMap[] = table; 90 91 int newCapacity = oldCapacity * 2 + 1; 92 ServerDescEntry newMap[] = new ServerDescEntry[newCapacity]; 93 94 modCount++; 95 threshold = (int)(newCapacity * loadFactor); 96 table = newMap; 97 98 for (int i = oldCapacity ; i-- > 0 ;) { 99 for (ServerDescEntry old = oldMap[i] ; old != null ; ) { 100 ServerDescEntry e = old; 101 old = old.next; 102 103 int index = (e.desc.sid & 0x7FFFFFFF) % newCapacity; 104 e.next = newMap[index]; 105 newMap[index] = e; 106 } 107 } 108 } 109 110 120 public synchronized ServerDesc put(ServerDesc desc) { 121 if (desc == null) throw new NullPointerException (); 123 124 ServerDescEntry tab[] = table; 126 int index = (desc.sid & 0x7FFF) % tab.length; 127 for (ServerDescEntry e = tab[index] ; e != null ; e = e.next) { 128 if (e.desc.sid == desc.sid) { 129 ServerDesc old = e.desc; 130 e.desc = desc; 131 return old; 132 } 133 } 134 135 modCount++; 136 if (count >= threshold) { 137 rehash(); 139 140 tab = table; 141 index = (desc.sid & 0x7FFF) % tab.length; 142 } 143 144 ServerDescEntry e = new ServerDescEntry(desc, tab[index]); 146 tab[index] = e; 147 count++; 148 return null; 149 } 150 151 159 public synchronized ServerDesc remove(short sid) { 160 ServerDescEntry tab[] = table; 161 int index = (sid & 0x7FFF) % tab.length; 162 for (ServerDescEntry e = tab[index], prev = null ; e != null ; prev = e, e = e.next) { 163 if (e.desc.sid == sid) { 164 modCount++; 165 if (prev != null) { 166 prev.next = e.next; 167 } else { 168 tab[index] = e.next; 169 } 170 count--; 171 ServerDesc oldDesc = e.desc; 172 e.desc = null; 173 return oldDesc; 174 } 175 } 176 return null; 177 } 178 179 182 public synchronized void clear() { 183 ServerDescEntry tab[] = table; 184 modCount++; 185 for (int index = tab.length; --index >= 0; ) 186 tab[index] = null; 187 count = 0; 188 } 189 190 195 public synchronized String toString() { 196 int max = size() - 1; 197 StringBuffer buf = new StringBuffer (); 198 199 buf.append("(").append(super.toString()); 200 207 buf.append(")"); 211 return buf.toString(); 212 } 213 214 217 private static final class ServerDescEntry { 218 ServerDesc desc; 219 ServerDescEntry next; 220 221 protected ServerDescEntry(ServerDesc desc, ServerDescEntry next) { 222 this.desc = desc; 223 this.next = next; 224 } 225 226 public String toString() { 227 return desc.toString(); 228 } 229 } 230 231 private static final int KEYS = 0; 233 private static final int VALUES = 1; 234 235 242 private class Enumerator implements Enumeration { 243 ServerDescEntry[] table = ServersHT.this.table; 244 int index = table.length; 245 ServerDescEntry entry = null; 246 ServerDescEntry lastReturned = null; 247 int type; 248 249 254 protected int expectedModCount = modCount; 255 256 Enumerator(int type) { 257 this.type = type; 258 } 259 260 public boolean hasMoreElements() { 261 ServerDescEntry e = entry; 262 int i = index; 263 ServerDescEntry t[] = table; 264 265 while (e == null && i > 0) { 266 e = t[--i]; 267 } 268 entry = e; 269 index = i; 270 return e != null; 271 } 272 273 public Object nextElement() { 274 ServerDescEntry et = entry; 275 int i = index; 276 ServerDescEntry t[] = table; 277 278 while (et == null && i > 0) { 279 et = t[--i]; 280 } 281 entry = et; 282 index = i; 283 if (et != null) { 284 ServerDescEntry e = lastReturned = entry; 285 entry = e.next; 286 return (type == KEYS)?((Object ) new Short (e.desc.sid)):((Object ) e.desc); 287 } 288 throw new NoSuchElementException ("ServersHT Enumerator"); 289 } 290 } 291 292 } 293 | Popular Tags |