| 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 | 16 | 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 | 13 | super(context); |
| 54 | 13 | this.context = context; |
| 55 | 13 | } |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
public void clear() { |
| 60 | 2 | Enumeration<String> keys = context.getKeys(); |
| 61 | 6 | while (keys.hasMoreElements()) { |
| 62 | 4 | context.removeValue(keys.nextElement()); |
| 63 | |
} |
| 64 | 2 | } |
| 65 | |
|
| 66 | |
|
| 67 | |
public Set<Map.Entry<String, Object>> entrySet() { |
| 68 | 8 | return new ScopeEntrySet(); |
| 69 | |
} |
| 70 | |
|
| 71 | |
|
| 72 | |
public Set<String> keySet() { |
| 73 | 1 | return new RemovableKeySet(context); |
| 74 | |
} |
| 75 | |
|
| 76 | |
|
| 77 | |
public Object put(String key, Object value) { |
| 78 | 1 | String skey = key(key); |
| 79 | 1 | Object previous = context.getValue(skey); |
| 80 | 1 | context.setValue(skey, value); |
| 81 | 1 | return previous; |
| 82 | |
} |
| 83 | |
|
| 84 | |
|
| 85 | |
public void putAll(Map<? extends String, ? extends Object> map) { |
| 86 | 1 | Iterator<? extends String> keys = map.keySet().iterator(); |
| 87 | 3 | while (keys.hasNext()) { |
| 88 | 2 | String key = keys.next(); |
| 89 | 2 | context.setValue(key, map.get(key)); |
| 90 | 2 | } |
| 91 | 1 | } |
| 92 | |
|
| 93 | |
|
| 94 | |
public Object remove(Object key) { |
| 95 | 1 | String skey = key(key); |
| 96 | 1 | Object previous = context.getValue(skey); |
| 97 | 1 | context.removeValue(skey); |
| 98 | 1 | return (previous); |
| 99 | |
} |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | 18 | private class ScopeEntrySet extends ReadOnlyEnumerationMap<Object>.ReadOnlyEnumerationMapEntrySet { |
| 105 | |
|
| 106 | |
@Override |
| 107 | |
public boolean add(java.util.Map.Entry<String, Object> e) { |
| 108 | 4 | String key = e.getKey(); |
| 109 | 4 | Object value = e.getValue(); |
| 110 | 4 | Object oldValue = get(key); |
| 111 | 4 | if (oldValue == null || !oldValue.equals(value)) { |
| 112 | 3 | context.setValue(key, value); |
| 113 | 3 | return true; |
| 114 | |
} |
| 115 | 1 | return false; |
| 116 | |
} |
| 117 | |
|
| 118 | |
@Override |
| 119 | |
public boolean addAll( |
| 120 | |
Collection<? extends java.util.Map.Entry<String, Object>> c) { |
| 121 | 1 | boolean retValue = false; |
| 122 | 1 | for (Map.Entry<String, Object> entry : c) { |
| 123 | 2 | retValue |= add(entry); |
| 124 | 2 | } |
| 125 | 1 | return retValue; |
| 126 | |
} |
| 127 | |
|
| 128 | |
@Override |
| 129 | |
public void clear() { |
| 130 | 1 | ScopeMap.this.clear(); |
| 131 | 1 | } |
| 132 | |
|
| 133 | |
@SuppressWarnings("unchecked") |
| 134 | |
@Override |
| 135 | |
public boolean remove(Object o) { |
| 136 | 4 | Map.Entry<String, Object> entry = (java.util.Map.Entry<String, Object>) o; |
| 137 | 4 | String key = entry.getKey(); |
| 138 | 4 | Object currentValue = context.getValue(key); |
| 139 | 4 | if (currentValue != null && currentValue.equals(entry.getValue())) { |
| 140 | 3 | context.removeValue(key); |
| 141 | 3 | return true; |
| 142 | |
} |
| 143 | 1 | return false; |
| 144 | |
} |
| 145 | |
|
| 146 | |
@SuppressWarnings("unchecked") |
| 147 | |
@Override |
| 148 | |
public boolean removeAll(Collection<?> c) { |
| 149 | 1 | Collection<Map.Entry<String, Object>> realCollection = (Collection<java.util.Map.Entry<String, Object>>) c; |
| 150 | 1 | boolean retValue = false; |
| 151 | 1 | for (Map.Entry<String, Object> entry : realCollection) { |
| 152 | 2 | retValue |= remove(entry); |
| 153 | 2 | } |
| 154 | 1 | return retValue; |
| 155 | |
} |
| 156 | |
|
| 157 | |
@SuppressWarnings("unchecked") |
| 158 | |
@Override |
| 159 | |
public boolean retainAll(Collection<?> c) { |
| 160 | 1 | Collection<Map.Entry<String, Object>> realCollection = (Collection<java.util.Map.Entry<String, Object>>) c; |
| 161 | 1 | boolean retValue = false; |
| 162 | 1 | Set<String> keysToRemove = new LinkedHashSet<String>(); |
| 163 | 1 | for (Enumeration<String> keys = context.getKeys(); keys.hasMoreElements();) { |
| 164 | 3 | String key = keys.nextElement(); |
| 165 | 3 | Object value = context.getValue(key); |
| 166 | 3 | Map.Entry<String, Object> entry = new MapEntry<String, Object>(key, value, false); |
| 167 | 3 | if (!realCollection.contains(entry)) { |
| 168 | 2 | retValue = true; |
| 169 | 2 | keysToRemove.add(key); |
| 170 | |
} |
| 171 | 3 | } |
| 172 | 1 | for (String key : keysToRemove) { |
| 173 | 2 | context.removeValue(key); |
| 174 | 2 | } |
| 175 | 1 | return retValue; |
| 176 | |
} |
| 177 | |
} |
| 178 | |
} |