blob: 24126871d269b883bf255c36daf4b7b78325ef5a (
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
/*******************************************************************************
* 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.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
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;
}
/**
* Returns the number of entries in this StackMap, the sum of the sizes of
* every remembered stack.
*
* @return the number of entries in this map.
*/
int size() {
int size = 0;
Iterator i = fInternalMap.values().iterator();
while (i.hasNext()) {
Collection c = (Collection) i.next();
size += c.size();
}
return size;
}
/**
* Returns all of the values of this StackMap.
*
* @return the values of every Stack within this StackMap.
*/
Collection values() {
Collection values = new LinkedList();
Iterator i = fInternalMap.values().iterator();
while (i.hasNext()) {
Collection c = (Collection) i.next();
values.addAll(c);
}
return values;
}
}
|