1 57 58 package com.sun.org.apache.xerces.internal.util; 59 60 import java.util.Hashtable ; 61 import java.util.Enumeration ; 62 63 import com.sun.org.apache.xerces.internal.xni.Augmentations; 64 65 76 public class AugmentationsImpl implements Augmentations{ 77 78 private AugmentationsItemsContainer fAugmentationsContainer = 79 new SmallContainer(); 80 81 90 public Object putItem (String key, Object item){ 91 Object oldValue = fAugmentationsContainer.putItem(key, item); 92 93 if (oldValue == null && fAugmentationsContainer.isFull()) { 94 fAugmentationsContainer = fAugmentationsContainer.expand(); 95 } 96 97 return oldValue; 98 } 99 100 101 109 public Object getItem(String key){ 110 return fAugmentationsContainer.getItem(key); 111 } 112 113 114 119 public Object removeItem (String key){ 120 return fAugmentationsContainer.removeItem(key); 121 } 122 123 127 public Enumeration keys (){ 128 return fAugmentationsContainer.keys(); 129 } 130 131 134 public void removeAllItems() { 135 fAugmentationsContainer.clear(); 136 } 137 138 public String toString() { 139 return fAugmentationsContainer.toString(); 140 } 141 142 abstract class AugmentationsItemsContainer { 143 abstract public Object putItem(Object key, Object item); 144 abstract public Object getItem(Object key); 145 abstract public Object removeItem(Object key); 146 abstract public Enumeration keys(); 147 abstract public void clear(); 148 abstract public boolean isFull(); 149 abstract public AugmentationsItemsContainer expand(); 150 } 151 152 class SmallContainer extends AugmentationsItemsContainer { 153 final static int SIZE_LIMIT = 10; 154 final Object [] fAugmentations = new Object [SIZE_LIMIT*2]; 155 int fNumEntries = 0; 156 157 public Enumeration keys() { 158 return new SmallContainerKeyEnumeration(); 159 } 160 161 public Object getItem(Object key) { 162 for (int i = 0; i < fNumEntries*2; i = i + 2) { 163 if (fAugmentations[i].equals(key)) { 164 return fAugmentations[i+1]; 165 } 166 } 167 168 return null; 169 } 170 171 public Object putItem(Object key, Object item) { 172 for (int i = 0; i < fNumEntries*2; i = i + 2) { 173 if (fAugmentations[i].equals(key)) { 174 Object oldValue = fAugmentations[i+1]; 175 fAugmentations[i+1] = item; 176 177 return oldValue; 178 } 179 } 180 181 fAugmentations[fNumEntries*2] = key; 182 fAugmentations[fNumEntries*2+1] = item; 183 fNumEntries++; 184 185 return null; 186 } 187 188 189 public Object removeItem(Object key) { 190 for (int i = 0; i < fNumEntries*2; i = i + 2) { 191 if (fAugmentations[i].equals(key)) { 192 Object oldValue = fAugmentations[i+1]; 193 194 for (int j = i; j < fNumEntries*2 - 2; j = j + 2) { 195 fAugmentations[j] = fAugmentations[j+2]; 196 fAugmentations[j+1] = fAugmentations[j+3]; 197 } 198 199 fAugmentations[fNumEntries*2-2] = null; 200 fAugmentations[fNumEntries*2-1] = null; 201 fNumEntries--; 202 203 return oldValue; 204 } 205 } 206 207 return null; 208 } 209 210 public void clear() { 211 for (int i = 0; i < fNumEntries*2; i = i + 2) { 212 fAugmentations[i] = null; 213 fAugmentations[i+1] = null; 214 } 215 216 fNumEntries = 0; 217 } 218 219 public boolean isFull() { 220 return (fNumEntries == SIZE_LIMIT); 221 } 222 223 public AugmentationsItemsContainer expand() { 224 LargeContainer expandedContainer = new LargeContainer(); 225 226 for (int i = 0; i < fNumEntries*2; i = i + 2) { 227 expandedContainer.putItem(fAugmentations[i], 228 fAugmentations[i+1]); 229 } 230 231 return expandedContainer; 232 } 233 234 public String toString() { 235 StringBuffer buff = new StringBuffer (); 236 buff.append("SmallContainer - fNumEntries == " + fNumEntries); 237 238 for (int i = 0; i < SIZE_LIMIT*2; i=i+2) { 239 buff.append("\nfAugmentations["); 240 buff.append(i); 241 buff.append("] == "); 242 buff.append(fAugmentations[i]); 243 buff.append("; fAugmentations["); 244 buff.append(i+1); 245 buff.append("] == "); 246 buff.append(fAugmentations[i+1]); 247 } 248 249 return buff.toString(); 250 } 251 252 class SmallContainerKeyEnumeration implements Enumeration { 253 Object [] enumArray = new Object [fNumEntries]; 254 int next = 0; 255 256 SmallContainerKeyEnumeration() { 257 for (int i = 0; i < fNumEntries; i++) { 258 enumArray[i] = fAugmentations[i*2]; 259 } 260 } 261 262 public boolean hasMoreElements() { 263 return next < enumArray.length; 264 } 265 266 public Object nextElement() { 267 if (next >= enumArray.length) { 268 throw new java.util.NoSuchElementException (); 269 } 270 271 Object nextVal = enumArray[next]; 272 enumArray[next] = null; 273 next++; 274 275 return nextVal; 276 } 277 } 278 } 279 280 class LargeContainer extends AugmentationsItemsContainer { 281 final Hashtable fAugmentations = new Hashtable (); 282 283 public Object getItem(Object key) { 284 return fAugmentations.get(key); 285 } 286 287 public Object putItem(Object key, Object item) { 288 return fAugmentations.put(key, item); 289 } 290 291 public Object removeItem(Object key) { 292 return fAugmentations.remove(key); 293 } 294 295 public Enumeration keys() { 296 return fAugmentations.keys(); 297 } 298 299 public void clear() { 300 fAugmentations.clear(); 301 } 302 303 public boolean isFull() { 304 return false; 305 } 306 307 public AugmentationsItemsContainer expand() { 308 return this; 309 } 310 311 public String toString() { 312 StringBuffer buff = new StringBuffer (); 313 buff.append("LargeContainer"); 314 Enumeration keys = fAugmentations.keys(); 315 316 while (keys.hasMoreElements()) { 317 Object key = keys.nextElement(); 318 buff.append("\nkey == "); 319 buff.append(key); 320 buff.append("; value == "); 321 buff.append(fAugmentations.get(key)); 322 } 323 324 return buff.toString(); 325 } 326 } 327 } 328 | Popular Tags |