Skip to main content
summaryrefslogtreecommitdiffstats
blob: ecd42a0f3de9f0bbe08eb393171f19e5eb58fcbd (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*******************************************************************************
 * Copyright (c) 2007 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.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * @author nitin
 * 
 */
class StackMap {

	private Map fInternalMap = null;

	public StackMap() {
		fInternalMap = new HashMap();
	}

	/**
	 * Removes all mappings from this StackMap
	 */
	public void clear() {
		fInternalMap.clear();
	}

	/**
	 * Returns the most recently pushed value to which this map maps the
	 * specified key. Returns <tt>null</tt> if the map contains no mapping
	 * for this key.
	 * 
	 * @param key
	 *            key whose associated value is to be returned.
	 * @return the most recently put value to which this map maps the
	 *         specified key, or <tt>null</tt> if the map contains no
	 *         mapping for this key.
	 */
	public Object peek(Object key) {
		Stack stack = (Stack) fInternalMap.get(key);
		if (stack != null) {
			Object o = stack.peek();
			if (stack.isEmpty()) {
				fInternalMap.remove(key);
			}
			return o;
		}
		return null;
	}

	/**
	 * Associates the specified value with the specified key in this map. If
	 * the map previously contained a mapping for this key, the old value is
	 * pushed onto the top of this key's private stack.
	 * 
	 * @param key
	 *            key with which the specified value is to be associated.
	 * @param value
	 *            value to be associated with the specified key.
	 * @return newest value associated with specified key
	 * 
	 * @throws UnsupportedOperationException
	 *             if the <tt>put</tt> operation is not supported by this
	 *             StackMap.
	 * @throws ClassCastException
	 *             if the class of the specified key or value prevents it from
	 *             being stored in this StackMap.
	 * @throws IllegalArgumentException
	 *             if some aspect of this key or value prevents it from being
	 *             stored in this StackMap.
	 * @throws NullPointerException,
	 *             as this map does not permit <tt>null</tt> keys or values
	 */
	public Object push(Object key, Object value) {
		Stack stack = (Stack) fInternalMap.get(key);
		if (stack == null) {
			stack = new Stack();
			fInternalMap.put(key, stack);
		}
		Object o = stack.push(value);
		return o;
	}

	/**
	 * Removes the most-recent mapping for this key from this StackMap if it
	 * is present.
	 * 
	 * <p>
	 * Returns the value to which the map previously associated the key, or
	 * <tt>null</tt> if the map contained no mapping for this key. The map
	 * will not contain a mapping for the specified key once the call returns.
	 * 
	 * @param key
	 *            key whose stack is to be popped
	 * @return most-recently pushed value associated with specified key, or
	 *         <tt>null</tt> if there was no mapping for key.
	 * 
	 * @throws ClassCastException
	 *             if the key is of an inappropriate type for this map.
	 * @throws NullPointerException
	 *             if the key is <tt>null</tt> as this class does not permit
	 *             <tt>null</tt> keys
	 */
	public Object pop(Object key) {
		Stack stack = (Stack) fInternalMap.get(key);
		if (stack != null) {
			Object o = stack.pop();
			if (stack.isEmpty()) {
				fInternalMap.remove(key);
			}
			return o;
		}
		return null;
	}
}

Back to the top