KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > util > Headers


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.osgi.framework.util;
13
14 import java.io.*;
15 import java.util.*;
16 import org.eclipse.osgi.framework.internal.core.Msg;
17 import org.eclipse.osgi.util.ManifestElement;
18 import org.eclipse.osgi.util.NLS;
19 import org.osgi.framework.BundleException;
20
21 /**
22  * Headers classes. This class implements a Dictionary that has
23  * the following behaviour:
24  * <ul>
25  * <li>put and remove clear throw UnsupportedOperationException.
26  * The Dictionary is thus read-only to others.
27  * <li>The String keys in the Dictionary are case-preserved,
28  * but the get operation is case-insensitive.
29  * </ul>
30  * @since 3.1
31  */

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

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

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

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

76     public synchronized Enumeration elements() {
77         return new ArrayEnumeration(values, size);
78     }
79
80     private int getIndex(Object JavaDoc key) {
81         boolean stringKey = key instanceof String JavaDoc;
82         for (int i = 0; i < size; i++) {
83             if (stringKey && (headers[i] instanceof String JavaDoc)) {
84                 if (((String JavaDoc) headers[i]).equalsIgnoreCase((String JavaDoc) key))
85                     return i;
86             } else {
87                 if (headers[i].equals(key))
88                     return i;
89             }
90         }
91         return -1;
92     }
93
94     private Object JavaDoc remove(int remove) {
95         Object JavaDoc removed = values[remove];
96         for (int i = remove; i < size; i++) {
97             if (i == headers.length - 1) {
98                 headers[i] = null;
99                 values[i] = null;
100             } else {
101                 headers[i] = headers[i + 1];
102                 values[i] = values[i + 1];
103             }
104         }
105         if (remove < size)
106             size--;
107         return removed;
108     }
109
110     private void add(Object JavaDoc header, Object JavaDoc value) {
111         if (size == headers.length) {
112             // grow the arrays
113
Object JavaDoc[] newHeaders = new Object JavaDoc[headers.length + 10];
114             Object JavaDoc[] newValues = new Object JavaDoc[values.length + 10];
115             System.arraycopy(headers, 0, newHeaders, 0, headers.length);
116             System.arraycopy(values, 0, newValues, 0, values.length);
117             headers = newHeaders;
118             values = newValues;
119         }
120         headers[size] = header;
121         values[size] = value;
122         size++;
123     }
124
125     /**
126      * Support case-insensitivity for keys.
127      *
128      * @param key name.
129      */

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

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

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

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

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

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

234     public Object JavaDoc remove(Object JavaDoc key) {
235         throw new UnsupportedOperationException JavaDoc();
236     }
237
238     public String JavaDoc toString() {
239         return (values.toString());
240     }
241
242     public static Headers parseManifest(InputStream in) throws BundleException {
243         Headers headers = new Headers(10);
244         try {
245             ManifestElement.parseBundleManifest(in, headers);
246         } catch (IOException e) {
247             throw new BundleException(Msg.MANIFEST_IOEXCEPTION, e);
248         }
249         headers.setReadOnly();
250         return headers;
251     }
252
253     class ArrayEnumeration implements Enumeration {
254         private Object JavaDoc[] array;
255         int cur = 0;
256
257         public ArrayEnumeration(Object JavaDoc[] array, int size) {
258             this.array = new Object JavaDoc[size];
259             System.arraycopy(array, 0, this.array, 0, this.array.length);
260         }
261
262         public boolean hasMoreElements() {
263             return cur < array.length;
264         }
265
266         public Object JavaDoc nextElement() {
267             return array[cur++];
268         }
269     }
270
271     public synchronized void clear() {
272         if (readOnly)
273             throw new UnsupportedOperationException JavaDoc();
274     }
275
276     public synchronized boolean containsKey(Object JavaDoc key) {
277         return getIndex(key) >= 0;
278     }
279
280     public boolean containsValue(Object JavaDoc var0) {
281         throw new UnsupportedOperationException JavaDoc();
282     }
283
284     public Set entrySet() {
285         throw new UnsupportedOperationException JavaDoc();
286     }
287
288     public Set keySet() {
289         throw new UnsupportedOperationException JavaDoc();
290     }
291
292     public void putAll(Map var0) {
293         throw new UnsupportedOperationException JavaDoc();
294     }
295
296     public Collection values() {
297         throw new UnsupportedOperationException JavaDoc();
298     }
299 }
300
Popular Tags