Skip to main content
summaryrefslogtreecommitdiffstats
blob: 507ec775253d9f0f3b5bc1c215b8134a97dabc1e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*******************************************************************************
 * Copyright (c) 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     
 *******************************************************************************/
package org.eclipse.jst.jsp.core.internal.java;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;

/**
 * This implementation of a hashtable maps keys to arrays
 * to support multiple values being associated with a single
 * key. To remove a specific entry, a key and inserted value must
 * be provided. Removing just based on the key name will remove all
 * values stored under that key
 * 
 */
public class ArrayMap extends Hashtable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public ArrayMap(int size) {
		super(size);
	}
	
	public synchronized Object put(Object key, Object value) {
		Object[] values = (Object[]) super.get(key);
		Object[] newValues = null;
		
		if (values == null || values.length == 0)
			newValues = new Object[1];
		else {
			newValues = new Object[values.length + 1];
			System.arraycopy(values, 0, newValues, 0, values.length);
		}

		newValues[newValues.length - 1] = value;
		return super.put(key, newValues);
	}
	
	/**
	 * Removes the first occurrence of <code>value</code> from the list 
	 * of values associated with <code>key</code>
	 * 
	 * @param key the key that has <code>value</code>
	 * @param value the specific value to remove from the key
	 * @return The item removed from the list of values
	 */
	public synchronized Object remove(Object key, Object value) {
		Object[] values = (Object[]) super.get(key);
		Object removed = null;
		Object[] result = null;
		if (values != null && value != null) {
			for (int i = 0; i < values.length; i++) {
				if (value.equals(values[i])) {
					removed = values[i];
					result = new Object[values.length - 1];
					
					if (result.length > 0) {
						// Copy left of value
						System.arraycopy(values, 0, result, 0, i);
						// Copy right of value
						if (i < (values.length - 1))
							System.arraycopy(values, i+1, result, i, result.length - i);
					}
					else
						super.remove(key);
					
					break;
				}
			}
		}
		
		if(result != null && result.length > 0)
			super.put(key, result);
		
		return removed;
	}
	
	public Collection values() {
		Collection valuemaps = super.values();
		Collection values = new ArrayList();
		
		for(Iterator i = valuemaps.iterator(); i.hasNext();)
			values.addAll(Arrays.asList((Object[]) i.next()));
		
		return values;
	}

}

Back to the top