This project has retired. For details please refer to its
Attic page.
ScopeMap xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.tiles.request.collection;
22
23 import static org.apache.tiles.request.collection.CollectionUtil.*;
24
25 import java.util.Collection;
26 import java.util.Enumeration;
27 import java.util.Iterator;
28 import java.util.LinkedHashSet;
29 import java.util.Map;
30 import java.util.Set;
31
32 import org.apache.tiles.request.attribute.AttributeExtractor;
33
34
35
36
37
38
39
40 public class ScopeMap extends ReadOnlyEnumerationMap<Object> {
41
42
43
44
45 private AttributeExtractor context;
46
47
48
49
50
51
52 public ScopeMap(AttributeExtractor context) {
53 super(context);
54 this.context = context;
55 }
56
57
58
59 public void clear() {
60 Enumeration<String> keys = context.getKeys();
61 while (keys.hasMoreElements()) {
62 context.removeValue(keys.nextElement());
63 }
64 }
65
66
67 public Set<Map.Entry<String, Object>> entrySet() {
68 return new ScopeEntrySet();
69 }
70
71
72 public Set<String> keySet() {
73 return new RemovableKeySet(context);
74 }
75
76
77 public Object put(String key, Object value) {
78 String skey = key(key);
79 Object previous = context.getValue(skey);
80 context.setValue(skey, value);
81 return previous;
82 }
83
84
85 public void putAll(Map<? extends String, ? extends Object> map) {
86 Iterator<? extends String> keys = map.keySet().iterator();
87 while (keys.hasNext()) {
88 String key = keys.next();
89 context.setValue(key, map.get(key));
90 }
91 }
92
93
94 public Object remove(Object key) {
95 String skey = key(key);
96 Object previous = context.getValue(skey);
97 context.removeValue(skey);
98 return (previous);
99 }
100
101
102
103
104 private class ScopeEntrySet extends ReadOnlyEnumerationMap<Object>.ReadOnlyEnumerationMapEntrySet {
105
106 @Override
107 public boolean add(java.util.Map.Entry<String, Object> e) {
108 String key = e.getKey();
109 Object value = e.getValue();
110 Object oldValue = get(key);
111 if (oldValue == null || !oldValue.equals(value)) {
112 context.setValue(key, value);
113 return true;
114 }
115 return false;
116 }
117
118 @Override
119 public boolean addAll(
120 Collection<? extends java.util.Map.Entry<String, Object>> c) {
121 boolean retValue = false;
122 for (Map.Entry<String, Object> entry : c) {
123 retValue |= add(entry);
124 }
125 return retValue;
126 }
127
128 @Override
129 public void clear() {
130 ScopeMap.this.clear();
131 }
132
133 @SuppressWarnings("unchecked")
134 @Override
135 public boolean remove(Object o) {
136 Map.Entry<String, Object> entry = (java.util.Map.Entry<String, Object>) o;
137 String key = entry.getKey();
138 Object currentValue = context.getValue(key);
139 if (currentValue != null && currentValue.equals(entry.getValue())) {
140 context.removeValue(key);
141 return true;
142 }
143 return false;
144 }
145
146 @SuppressWarnings("unchecked")
147 @Override
148 public boolean removeAll(Collection<?> c) {
149 Collection<Map.Entry<String, Object>> realCollection = (Collection<java.util.Map.Entry<String, Object>>) c;
150 boolean retValue = false;
151 for (Map.Entry<String, Object> entry : realCollection) {
152 retValue |= remove(entry);
153 }
154 return retValue;
155 }
156
157 @SuppressWarnings("unchecked")
158 @Override
159 public boolean retainAll(Collection<?> c) {
160 Collection<Map.Entry<String, Object>> realCollection = (Collection<java.util.Map.Entry<String, Object>>) c;
161 boolean retValue = false;
162 Set<String> keysToRemove = new LinkedHashSet<String>();
163 for (Enumeration<String> keys = context.getKeys(); keys.hasMoreElements();) {
164 String key = keys.nextElement();
165 Object value = context.getValue(key);
166 Map.Entry<String, Object> entry = new MapEntry<String, Object>(key, value, false);
167 if (!realCollection.contains(entry)) {
168 retValue = true;
169 keysToRemove.add(key);
170 }
171 }
172 for (String key : keysToRemove) {
173 context.removeValue(key);
174 }
175 return retValue;
176 }
177 }
178 }