KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > internal > module > MappedList


1 /*******************************************************************************
2  * Copyright (c) 2005 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 package org.eclipse.osgi.internal.module;
12
13 import java.util.*;
14
15 /*
16  * A MappedList maps values into keyed list arrays. All values with the same key are stored
17  * into the same array. Extending classes may override the sort method to sort the individual
18  * arrays in the MappedList. By default the MappedList appends new values to the end of the array.
19  */

20 public class MappedList {
21     // the mapping with key -> Object[] mapping
22
protected HashMap internal = new HashMap();
23
24     public void put(Object JavaDoc key, Object JavaDoc value) {
25         Object JavaDoc[] existing = (Object JavaDoc[]) internal.get(key);
26         if (existing == null) {
27             existing = new Object JavaDoc[1]; // be optimistic; start small
28
existing[0] = value;
29             internal.put(key, existing);
30         } else {
31             // append the new value to the end.
32
Object JavaDoc[] newValues = new Object JavaDoc[existing.length + 1];
33             System.arraycopy(existing, 0, newValues, 0, existing.length);
34             newValues[existing.length] = value;
35             sort(newValues); // sort the new values array
36
internal.put(key, newValues); // overwrite the old values in tha map
37
}
38     }
39
40     protected void sort(Object JavaDoc[] values) {
41         // a MappedList is not sorted by default
42
// extending classes may override this method to sort lists
43
}
44
45     // removes all values with the specified key
46
public Object JavaDoc[] remove(Object JavaDoc key) {
47         return get(key, true);
48     }
49
50     // gets all values with the specified key
51
public Object JavaDoc[] get(Object JavaDoc key) {
52         return get(key, false);
53     }
54
55     // gets all values with the specified and optionally removes them
56
private Object JavaDoc[] get(Object JavaDoc key, boolean remove) {
57         Object JavaDoc[] result = (Object JavaDoc[]) (remove ? internal.remove(key) : internal.get(key));
58         return result == null ? new Object JavaDoc[0] : result;
59     }
60
61     // returns the number of keyed lists
62
public int getSize() {
63         return internal.size();
64     }
65
66     // returns all values of all keys
67
public Object JavaDoc[] getAllValues() {
68         if (getSize() == 0)
69             return new Object JavaDoc[0];
70         ArrayList results = new ArrayList(getSize());
71         Iterator iter = internal.values().iterator();
72         while (iter.hasNext()) {
73             Object JavaDoc[] values = (Object JavaDoc[]) iter.next();
74             for (int i = 0; i < values.length; i++)
75                 results.add(values[i]);
76         }
77         return results.toArray();
78     }
79
80     // removes all keys from the map
81
public void clear() {
82         internal.clear();
83     }
84
85 }
86
Popular Tags