1 16 17 package org.apache.xerces.util; 18 19 import java.util.Hashtable ; 20 import java.util.Enumeration ; 21 22 import org.apache.xerces.xni.Augmentations; 23 24 35 public class AugmentationsImpl implements Augmentations{ 36 37 private AugmentationsItemsContainer fAugmentationsContainer = 38 new SmallContainer(); 39 40 49 public Object putItem (String key, Object item){ 50 Object oldValue = fAugmentationsContainer.putItem(key, item); 51 52 if (oldValue == null && fAugmentationsContainer.isFull()) { 53 fAugmentationsContainer = fAugmentationsContainer.expand(); 54 } 55 56 return oldValue; 57 } 58 59 60 68 public Object getItem(String key){ 69 return fAugmentationsContainer.getItem(key); 70 } 71 72 73 78 public Object removeItem (String key){ 79 return fAugmentationsContainer.removeItem(key); 80 } 81 82 86 public Enumeration keys (){ 87 return fAugmentationsContainer.keys(); 88 } 89 90 93 public void removeAllItems() { 94 fAugmentationsContainer.clear(); 95 } 96 97 public String toString() { 98 return fAugmentationsContainer.toString(); 99 } 100 101 abstract class AugmentationsItemsContainer { 102 abstract public Object putItem(Object key, Object item); 103 abstract public Object getItem(Object key); 104 abstract public Object removeItem(Object key); 105 abstract public Enumeration keys(); 106 abstract public void clear(); 107 abstract public boolean isFull(); 108 abstract public AugmentationsItemsContainer expand(); 109 } 110 111 class SmallContainer extends AugmentationsItemsContainer { 112 final static int SIZE_LIMIT = 10; 113 final Object [] fAugmentations = new Object [SIZE_LIMIT*2]; 114 int fNumEntries = 0; 115 116 public Enumeration keys() { 117 return new SmallContainerKeyEnumeration(); 118 } 119 120 public Object getItem(Object key) { 121 for (int i = 0; i < fNumEntries*2; i = i + 2) { 122 if (fAugmentations[i].equals(key)) { 123 return fAugmentations[i+1]; 124 } 125 } 126 127 return null; 128 } 129 130 public Object putItem(Object key, Object item) { 131 for (int i = 0; i < fNumEntries*2; i = i + 2) { 132 if (fAugmentations[i].equals(key)) { 133 Object oldValue = fAugmentations[i+1]; 134 fAugmentations[i+1] = item; 135 136 return oldValue; 137 } 138 } 139 140 fAugmentations[fNumEntries*2] = key; 141 fAugmentations[fNumEntries*2+1] = item; 142 fNumEntries++; 143 144 return null; 145 } 146 147 148 public Object removeItem(Object key) { 149 for (int i = 0; i < fNumEntries*2; i = i + 2) { 150 if (fAugmentations[i].equals(key)) { 151 Object oldValue = fAugmentations[i+1]; 152 153 for (int j = i; j < fNumEntries*2 - 2; j = j + 2) { 154 fAugmentations[j] = fAugmentations[j+2]; 155 fAugmentations[j+1] = fAugmentations[j+3]; 156 } 157 158 fAugmentations[fNumEntries*2-2] = null; 159 fAugmentations[fNumEntries*2-1] = null; 160 fNumEntries--; 161 162 return oldValue; 163 } 164 } 165 166 return null; 167 } 168 169 public void clear() { 170 for (int i = 0; i < fNumEntries*2; i = i + 2) { 171 fAugmentations[i] = null; 172 fAugmentations[i+1] = null; 173 } 174 175 fNumEntries = 0; 176 } 177 178 public boolean isFull() { 179 return (fNumEntries == SIZE_LIMIT); 180 } 181 182 public AugmentationsItemsContainer expand() { 183 LargeContainer expandedContainer = new LargeContainer(); 184 185 for (int i = 0; i < fNumEntries*2; i = i + 2) { 186 expandedContainer.putItem(fAugmentations[i], 187 fAugmentations[i+1]); 188 } 189 190 return expandedContainer; 191 } 192 193 public String toString() { 194 StringBuffer buff = new StringBuffer (); 195 buff.append("SmallContainer - fNumEntries == " + fNumEntries); 196 197 for (int i = 0; i < SIZE_LIMIT*2; i=i+2) { 198 buff.append("\nfAugmentations["); 199 buff.append(i); 200 buff.append("] == "); 201 buff.append(fAugmentations[i]); 202 buff.append("; fAugmentations["); 203 buff.append(i+1); 204 buff.append("] == "); 205 buff.append(fAugmentations[i+1]); 206 } 207 208 return buff.toString(); 209 } 210 211 class SmallContainerKeyEnumeration implements Enumeration { 212 Object [] enumArray = new Object [fNumEntries]; 213 int next = 0; 214 215 SmallContainerKeyEnumeration() { 216 for (int i = 0; i < fNumEntries; i++) { 217 enumArray[i] = fAugmentations[i*2]; 218 } 219 } 220 221 public boolean hasMoreElements() { 222 return next < enumArray.length; 223 } 224 225 public Object nextElement() { 226 if (next >= enumArray.length) { 227 throw new java.util.NoSuchElementException (); 228 } 229 230 Object nextVal = enumArray[next]; 231 enumArray[next] = null; 232 next++; 233 234 return nextVal; 235 } 236 } 237 } 238 239 class LargeContainer extends AugmentationsItemsContainer { 240 final Hashtable fAugmentations = new Hashtable (); 241 242 public Object getItem(Object key) { 243 return fAugmentations.get(key); 244 } 245 246 public Object putItem(Object key, Object item) { 247 return fAugmentations.put(key, item); 248 } 249 250 public Object removeItem(Object key) { 251 return fAugmentations.remove(key); 252 } 253 254 public Enumeration keys() { 255 return fAugmentations.keys(); 256 } 257 258 public void clear() { 259 fAugmentations.clear(); 260 } 261 262 public boolean isFull() { 263 return false; 264 } 265 266 public AugmentationsItemsContainer expand() { 267 return this; 268 } 269 270 public String toString() { 271 StringBuffer buff = new StringBuffer (); 272 buff.append("LargeContainer"); 273 Enumeration keys = fAugmentations.keys(); 274 275 while (keys.hasMoreElements()) { 276 Object key = keys.nextElement(); 277 buff.append("\nkey == "); 278 buff.append(key); 279 buff.append("; value == "); 280 buff.append(fAugmentations.get(key)); 281 } 282 283 return buff.toString(); 284 } 285 } 286 } 287 | Popular Tags |