KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > util > Headers


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.pde.internal.core.util;
13
14 import java.util.Collection JavaDoc;
15 import java.util.Dictionary JavaDoc;
16 import java.util.Enumeration JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Set JavaDoc;
19
20 /**
21  * Headers classes. This class implements a Dictionary that has
22  * the following behavior:
23  * <ul>
24  * <li>put and remove clear throw UnsupportedOperationException.
25  * The Dictionary is thus read-only to others.
26  * <li>The String keys in the Dictionary are case-preserved,
27  * but the get operation is case-insensitive.
28  * </ul>
29  * @since 3.3
30  */

31 public class Headers extends Dictionary JavaDoc implements Map JavaDoc {
32     private boolean readOnly = false;
33     private Object JavaDoc[] headers;
34     private Object JavaDoc[] values;
35     private int size = 0;
36
37     /**
38      * Create an empty Headers dictionary.
39      *
40      * @param initialCapacity The initial capacity of this Headers object.
41      */

42     public Headers(int initialCapacity) {
43         super();
44         headers = new Object JavaDoc[initialCapacity];
45         values = new Object JavaDoc[initialCapacity];
46     }
47
48     /**
49      * Create a Headers dictionary from a Dictionary.
50      *
51      * @param values The initial dictionary for this Headers object.
52      * @exception IllegalArgumentException If a case-variant of the key is
53      * in the dictionary parameter.
54      */

55     public Headers(Dictionary JavaDoc values) {
56         this(values.size());
57         /* initialize the headers and values */
58         Enumeration JavaDoc keys = values.keys();
59         while (keys.hasMoreElements()) {
60             Object JavaDoc key = keys.nextElement();
61             set(key, values.get(key));
62         }
63     }
64
65     /**
66      * Case-preserved keys.
67      */

68     public synchronized Enumeration JavaDoc keys() {
69         return new ArrayEnumeration(headers, size);
70     }
71
72     /**
73      * Values.
74      */

75     public synchronized Enumeration JavaDoc elements() {
76         return new ArrayEnumeration(values, size);
77     }
78
79     private int getIndex(Object JavaDoc key) {
80         boolean stringKey = key instanceof String JavaDoc;
81         for (int i = 0; i < size; i++) {
82             if (stringKey && (headers[i] instanceof String JavaDoc)) {
83                 if (((String JavaDoc) headers[i]).equalsIgnoreCase((String JavaDoc) 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 JavaDoc remove(int remove) {
94         Object JavaDoc 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 JavaDoc header, Object JavaDoc value) {
110         if (size == headers.length) {
111             // grow the arrays
112
Object JavaDoc[] newHeaders = new Object JavaDoc[headers.length + 10];
113             Object JavaDoc[] newValues = new Object JavaDoc[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     /**
125      * Support case-insensitivity for keys.
126      *
127      * @param key name.
128      */

129     public synchronized Object JavaDoc get(Object JavaDoc key) {
130         int i = -1;
131         if ((i = getIndex(key)) != -1)
132             return values[i];
133         return null;
134     }
135
136     /**
137      * Set a header value or optionally replace it if it already exists.
138      *
139      * @param key Key name.
140      * @param value Value of the key or null to remove key.
141      * @param replace A value of true will allow a previous
142      * value of the key to be replaced. A value of false
143      * will cause an IllegalArgumentException to be thrown
144      * if a previous value of the key exists.
145      * @return the previous value to which the key was mapped,
146      * or null if the key did not have a previous mapping.
147      *
148      * @exception IllegalArgumentException If a case-variant of the key is
149      * already present.
150      * @since 3.2
151      */

152     public synchronized Object JavaDoc set(Object JavaDoc key, Object JavaDoc value, boolean replace) {
153         if (readOnly)
154             throw new UnsupportedOperationException JavaDoc();
155         if (key instanceof String JavaDoc)
156             key = ((String JavaDoc) key).intern();
157         int i = getIndex(key);
158         if (value == null) { /* remove */
159             if (i != -1)
160                 return remove(i);
161         } else { /* put */
162             if (i != -1) { /* duplicate key */
163                 if (!replace)
164                     throw new IllegalArgumentException JavaDoc();
165                 Object JavaDoc oldVal = values[i];
166                 values[i] = value;
167                 return oldVal;
168             }
169             add(key, value);
170         }
171         return null;
172     }
173
174     /**
175      * Set a header value.
176      *
177      * @param key Key name.
178      * @param value Value of the key or null to remove key.
179      * @return the previous value to which the key was mapped,
180      * or null if the key did not have a previous mapping.
181      *
182      * @exception IllegalArgumentException If a case-variant of the key is
183      * already present.
184      */

185     public synchronized Object JavaDoc set(Object JavaDoc key, Object JavaDoc value) {
186         return set(key, value, false);
187     }
188
189     public synchronized void setReadOnly() {
190         readOnly = true;
191     }
192
193     /**
194      * Returns the number of entries (distinct keys) in this dictionary.
195      *
196      * @return the number of keys in this dictionary.
197      */

198     public synchronized int size() {
199         return size;
200     }
201
202     /**
203      * Tests if this dictionary maps no keys to value. The general contract
204      * for the <tt>isEmpty</tt> method is that the result is true if and only
205      * if this dictionary contains no entries.
206      *
207      * @return <code>true</code> if this dictionary maps no keys to values;
208      * <code>false</code> otherwise.
209      */

210     public synchronized boolean isEmpty() {
211         return size == 0;
212     }
213
214     /**
215      * Always throws UnsupportedOperationException.
216      *
217      * @param key header name.
218      * @param value header value.
219      * @throws UnsupportedOperationException
220      */

221     public synchronized Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
222         if (readOnly)
223             throw new UnsupportedOperationException JavaDoc();
224         return set(key, value, true);
225     }
226
227     /**
228      * Always throws UnsupportedOperationException.
229      *
230      * @param key header name.
231      * @throws UnsupportedOperationException
232      */

233     public Object JavaDoc remove(Object JavaDoc key) {
234         throw new UnsupportedOperationException JavaDoc();
235     }
236
237     public String JavaDoc toString() {
238         return (values.toString());
239     }
240
241     class ArrayEnumeration implements Enumeration JavaDoc {
242         private Object JavaDoc[] array;
243         int cur = 0;
244
245         public ArrayEnumeration(Object JavaDoc[] array, int size) {
246             this.array = new Object JavaDoc[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 JavaDoc nextElement() {
255             return array[cur++];
256         }
257     }
258
259     public synchronized void clear() {
260         if (readOnly)
261             throw new UnsupportedOperationException JavaDoc();
262     }
263
264     public synchronized boolean containsKey(Object JavaDoc key) {
265         return getIndex(key) >= 0;
266     }
267
268     public boolean containsValue(Object JavaDoc var0) {
269         throw new UnsupportedOperationException JavaDoc();
270     }
271
272     public Set JavaDoc entrySet() {
273         throw new UnsupportedOperationException JavaDoc();
274     }
275
276     public Set JavaDoc keySet() {
277         throw new UnsupportedOperationException JavaDoc();
278     }
279
280     public void putAll(Map JavaDoc var0) {
281         throw new UnsupportedOperationException JavaDoc();
282     }
283
284     public Collection JavaDoc values() {
285         throw new UnsupportedOperationException JavaDoc();
286     }
287 }
288
Popular Tags