1 19 20 22 package org.netbeans.editor.ext.html; 23 24 import java.lang.ref.WeakReference ; 25 26 35 public class WeakHashSet { 36 37 Entry[] data; 38 int count = 0; 40 int treshold; 42 float loadFactor; 43 44 45 46 public WeakHashSet( int capacity, float loadFactor ) { 47 this.loadFactor = loadFactor; 48 treshold = (int)(capacity * loadFactor); 49 data = new Entry[capacity]; 50 } 51 52 53 public Object get( Object obj ) { 54 if( obj == null ) return null; 55 56 Entry[] tab = data; 57 Entry prev = null; 58 int hash = obj.hashCode(); 59 int index = (hash & 0x7FFFFFFF) % tab.length; 60 61 for( Entry e = tab[index]; e != null; prev = e, e = e.next ) 62 if( e.hash == hash ) { 63 Object value = e.value.get(); 64 if( value == null ) { 65 count--; 67 if( prev == null ) tab[index] = e.next; 68 else prev.next = e.next; 69 } else { 70 if( value.equals( obj ) ) return value; 71 } 72 } 73 return null; 74 } 75 76 public Object put( Object obj ) { 77 if( obj == null ) return null; 78 79 Entry[] tab = data; 80 Entry prev = null; 81 int hash = obj.hashCode(); 82 int index = (hash & 0x7FFFFFFF) % tab.length; 83 84 for( Entry e = tab[index] ; e != null ; prev = e, e = e.next ) 85 if( e.hash == hash ) { 86 Object value = e.value.get(); 87 if( value == null ) { 88 count--; 89 if( prev == null ) tab[index] = e.next; 90 else prev.next = e.next; 91 } else { 92 if( value.equals( obj ) ) return value; 93 } 94 } 95 96 97 if( count >= treshold ) { 98 rehash(); 99 tab = data; 100 index = (hash & 0x7FFFFFFF) % tab.length; 101 } 102 103 Entry e = new Entry( hash, obj, tab[index] ); 104 tab[index] = e; 105 count++; 106 107 return obj; 108 } 109 110 private void rehash() { 111 int oldCapacity = data.length; 112 Entry oldMap[] = data; 113 114 int newCapacity = oldCapacity * 2 + 1; 115 Entry newMap[] = new Entry[newCapacity]; 116 117 treshold = (int)(newCapacity * loadFactor); 118 data = newMap; 119 120 for( int i = oldCapacity ; i-- > 0 ; ) { 121 for( Entry old = oldMap[i] ; old != null ; ) { 122 Entry e = old; 123 old = old.next; 124 125 int index = (e.hash & 0x7FFFFFFF) % newCapacity; 126 e.next = newMap[index]; 127 newMap[index] = e; 128 } 129 } 130 } 131 132 133 134 135 138 private static class Entry { 139 int hash; 140 WeakReference value; 141 Entry next; 142 143 Entry(int hash, Object value, Entry next) { 144 this.hash = hash; 145 this.value = new WeakReference ( value ); 146 this.next = next; 147 } 148 } 149 150 } 151 | Popular Tags |