1 18 package org.apache.batik.dom.util; 19 20 import org.w3c.dom.Element ; 21 22 import org.apache.batik.util.CleanerThread; 23 24 30 public class DocumentDescriptor { 31 32 35 protected final static int INITIAL_CAPACITY = 101; 36 37 40 protected Entry[] table; 41 42 45 protected int count; 46 47 50 public DocumentDescriptor() { 51 table = new Entry[INITIAL_CAPACITY]; 52 } 53 54 57 public int getNumberOfElements() { 58 synchronized (this) { 59 return count; 60 } 61 } 62 63 67 public int getLocationLine(Element elt) { 68 synchronized (this) { 69 int hash = elt.hashCode() & 0x7FFFFFFF; 70 int index = hash % table.length; 71 72 for (Entry e = table[index]; e != null; e = e.next) { 73 if (e.hash != hash) 74 continue; 75 Object o = e.get(); 76 if (o == elt) 77 return e.locationLine; 78 } 79 } 80 return 0; 81 } 82 83 86 public void setLocationLine(Element elt, int line) { 87 synchronized (this) { 88 int hash = elt.hashCode() & 0x7FFFFFFF; 89 int index = hash % table.length; 90 91 for (Entry e = table[index]; e != null; e = e.next) { 92 if (e.hash != hash) 93 continue; 94 Object o = e.get(); 95 if (o == elt) 96 e.locationLine = line; 97 } 98 99 int len = table.length; 101 if (count++ >= (len * 3) >>> 2) { 102 rehash(); 103 index = hash % table.length; 104 } 105 106 Entry e = new Entry(hash, elt, line, table[index]); 107 table[index] = e; 108 } 109 } 110 111 114 protected void rehash () { 115 Entry[] oldTable = table; 116 117 table = new Entry[oldTable.length * 2 + 1]; 118 119 for (int i = oldTable.length-1; i >= 0; i--) { 120 for (Entry old = oldTable[i]; old != null;) { 121 Entry e = old; 122 old = old.next; 123 124 int index = e.hash % table.length; 125 e.next = table[index]; 126 table[index] = e; 127 } 128 } 129 } 130 131 protected void removeEntry(Entry e) { 132 synchronized (this) { 133 int hash = e.hash; 134 int index = hash % table.length; 135 Entry curr = table[index]; 136 Entry prev = null; 137 while (curr != e) { 138 prev = curr; 139 curr = curr.next; 140 } 141 if (curr == null) return; 143 if (prev == null) 144 table[index] = curr.next; 146 else 147 prev.next = curr.next; 148 count--; 149 } 150 } 151 152 155 protected class Entry extends CleanerThread.WeakReferenceCleared { 156 159 public int hash; 160 161 164 public int locationLine; 165 166 169 public Entry next; 170 171 174 public Entry(int hash, Element element, int locationLine, Entry next) { 175 super(element); 176 this.hash = hash; 177 this.locationLine = locationLine; 178 this.next = next; 179 } 180 181 public void cleared() { 182 removeEntry(this); 183 } 184 } 185 } 186 | Popular Tags |