This project has retired. For details please refer to its Attic page.
ScopeMap xref
View Javadoc

1   /*
2    * $Id: ScopeMap.java 1229087 2012-01-09 10:35:14Z mck $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Exposes a scope context as a <String, Object> map.
36   *
37   * @version $Rev: 1229087 $ $Date: 2012-01-09 21:35:14 +1100 (Mon, 09 Jan 2012) $
38   */
39  
40  public class ScopeMap extends ReadOnlyEnumerationMap<Object> {
41  
42      /**
43       * The context to read.
44       */
45      private AttributeExtractor context;
46  
47      /**
48       * Constructor.
49       *
50       * @param context The servlet context to use.
51       */
52      public ScopeMap(AttributeExtractor context) {
53          super(context);
54          this.context = context;
55      }
56  
57  
58      /** {@inheritDoc} */
59      public void clear() {
60          Enumeration<String> keys = context.getKeys();
61          while (keys.hasMoreElements()) {
62              context.removeValue(keys.nextElement());
63          }
64      }
65  
66      /** {@inheritDoc} */
67      public Set<Map.Entry<String, Object>> entrySet() {
68          return new ScopeEntrySet();
69      }
70  
71      /** {@inheritDoc} */
72      public Set<String> keySet() {
73          return new RemovableKeySet(context);
74      }
75  
76      /** {@inheritDoc} */
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      /** {@inheritDoc} */
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      /** {@inheritDoc} */
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      * Entry set implementation for {@link ScopeMap}.
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 }