1 11 12 package org.eclipse.pde.internal.core.util; 13 14 import java.util.Collection ; 15 import java.util.Dictionary ; 16 import java.util.Enumeration ; 17 import java.util.Map ; 18 import java.util.Set ; 19 20 31 public class Headers extends Dictionary implements Map { 32 private boolean readOnly = false; 33 private Object [] headers; 34 private Object [] values; 35 private int size = 0; 36 37 42 public Headers(int initialCapacity) { 43 super(); 44 headers = new Object [initialCapacity]; 45 values = new Object [initialCapacity]; 46 } 47 48 55 public Headers(Dictionary values) { 56 this(values.size()); 57 58 Enumeration keys = values.keys(); 59 while (keys.hasMoreElements()) { 60 Object key = keys.nextElement(); 61 set(key, values.get(key)); 62 } 63 } 64 65 68 public synchronized Enumeration keys() { 69 return new ArrayEnumeration(headers, size); 70 } 71 72 75 public synchronized Enumeration elements() { 76 return new ArrayEnumeration(values, size); 77 } 78 79 private int getIndex(Object key) { 80 boolean stringKey = key instanceof String ; 81 for (int i = 0; i < size; i++) { 82 if (stringKey && (headers[i] instanceof String )) { 83 if (((String ) headers[i]).equalsIgnoreCase((String ) key)) 84 return i; 85 } else { 86 if (headers[i].equals(key)) 87 return i; 88 } 89 } 90 return -1; 91 } 92 93 private Object remove(int remove) { 94 Object removed = values[remove]; 95 for (int i = remove; i < size; i++) { 96 if (i == headers.length - 1) { 97 headers[i] = null; 98 values[i] = null; 99 } else { 100 headers[i] = headers[i + 1]; 101 values[i] = values[i + 1]; 102 } 103 } 104 if (remove < size) 105 size--; 106 return removed; 107 } 108 109 private void add(Object header, Object value) { 110 if (size == headers.length) { 111 Object [] newHeaders = new Object [headers.length + 10]; 113 Object [] newValues = new Object [values.length + 10]; 114 System.arraycopy(headers, 0, newHeaders, 0, headers.length); 115 System.arraycopy(values, 0, newValues, 0, values.length); 116 headers = newHeaders; 117 values = newValues; 118 } 119 headers[size] = header; 120 values[size] = value; 121 size++; 122 } 123 124 129 public synchronized Object get(Object key) { 130 int i = -1; 131 if ((i = getIndex(key)) != -1) 132 return values[i]; 133 return null; 134 } 135 136 152 public synchronized Object set(Object key, Object value, boolean replace) { 153 if (readOnly) 154 throw new UnsupportedOperationException (); 155 if (key instanceof String ) 156 key = ((String ) key).intern(); 157 int i = getIndex(key); 158 if (value == null) { 159 if (i != -1) 160 return remove(i); 161 } else { 162 if (i != -1) { 163 if (!replace) 164 throw new IllegalArgumentException (); 165 Object oldVal = values[i]; 166 values[i] = value; 167 return oldVal; 168 } 169 add(key, value); 170 } 171 return null; 172 } 173 174 185 public synchronized Object set(Object key, Object value) { 186 return set(key, value, false); 187 } 188 189 public synchronized void setReadOnly() { 190 readOnly = true; 191 } 192 193 198 public synchronized int size() { 199 return size; 200 } 201 202 210 public synchronized boolean isEmpty() { 211 return size == 0; 212 } 213 214 221 public synchronized Object put(Object key, Object value) { 222 if (readOnly) 223 throw new UnsupportedOperationException (); 224 return set(key, value, true); 225 } 226 227 233 public Object remove(Object key) { 234 throw new UnsupportedOperationException (); 235 } 236 237 public String toString() { 238 return (values.toString()); 239 } 240 241 class ArrayEnumeration implements Enumeration { 242 private Object [] array; 243 int cur = 0; 244 245 public ArrayEnumeration(Object [] array, int size) { 246 this.array = new Object [size]; 247 System.arraycopy(array, 0, this.array, 0, this.array.length); 248 } 249 250 public boolean hasMoreElements() { 251 return cur < array.length; 252 } 253 254 public Object nextElement() { 255 return array[cur++]; 256 } 257 } 258 259 public synchronized void clear() { 260 if (readOnly) 261 throw new UnsupportedOperationException (); 262 } 263 264 public synchronized boolean containsKey(Object key) { 265 return getIndex(key) >= 0; 266 } 267 268 public boolean containsValue(Object var0) { 269 throw new UnsupportedOperationException (); 270 } 271 272 public Set entrySet() { 273 throw new UnsupportedOperationException (); 274 } 275 276 public Set keySet() { 277 throw new UnsupportedOperationException (); 278 } 279 280 public void putAll(Map var0) { 281 throw new UnsupportedOperationException (); 282 } 283 284 public Collection values() { 285 throw new UnsupportedOperationException (); 286 } 287 } 288 | Popular Tags |