View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.flow.impl;
20  
21  import java.util.Collection;
22  import java.util.Map;
23  import java.util.Set;
24  import javax.faces.context.FacesContext;
25  
26  /**
27   *
28   * @author Leonardo Uribe
29   */
30  class FlowScopeMap implements Map<Object,Object>
31  {
32      private DefaultFacesFlowProvider _provider;
33      
34      private String _flowMapKey;
35      
36      private Map<Object,Object> _delegate;
37          
38      public FlowScopeMap(DefaultFacesFlowProvider provider, String flowMapKey)
39      {
40          this._provider = provider;
41          this._flowMapKey = flowMapKey;
42      }
43      
44      private Map<Object,Object> getWrapped(boolean create)
45      {
46          if (_delegate == null)
47          {
48              String fullToken = DefaultFacesFlowProvider.FLOW_SESSION_MAP_SUBKEY_PREFIX + 
49                  DefaultFacesFlowProvider.SEPARATOR_CHAR + _flowMapKey;
50              FacesContext context = FacesContext.getCurrentInstance();
51              _delegate = _provider.createOrRestoreMap(context, fullToken, create);
52          }
53          return _delegate;
54      }
55  
56      public int size()
57      {
58          Map<Object,Object> map = getWrapped(false);
59          if (map == null)
60          {
61              return 0;
62          }
63          return map.size();
64      }
65  
66      public boolean isEmpty()
67      {
68          Map<Object,Object> map = getWrapped(false);
69          if (map == null)
70          {
71              return false;
72          }
73          return map.isEmpty();
74      }
75  
76      public boolean containsKey(Object key)
77      {
78          Map<Object,Object> map = getWrapped(false);
79          if (map == null)
80          {
81              return false;
82          }
83          return map.containsKey(key);
84      }
85  
86      public boolean containsValue(Object value)
87      {
88          Map<Object,Object> map = getWrapped(false);
89          if (map == null)
90          {
91              return false;
92          }
93          return map.containsValue(value);
94      }
95  
96      public Object get(Object key)
97      {
98          Map<Object,Object> map = getWrapped(false);
99          if (map == null)
100         {
101             return null;
102         }
103         return map.get(key);
104     }
105 
106     public Object put(Object key, Object value)
107     {
108         return getWrapped(true).put(key, value);
109     }
110 
111     public Object remove(Object key)
112     {
113         Map<Object,Object> map = getWrapped(false);
114         if (map == null)
115         {
116             return null;
117         }
118         return map.remove(key);
119     }
120 
121     public void putAll(Map<? extends Object, ? extends Object> m)
122     {
123         getWrapped(true).putAll(m);
124     }
125 
126     public void clear()
127     {
128         Map<Object,Object> map = getWrapped(false);
129         if (map == null)
130         {
131             return;
132         }
133         map.clear();
134     }
135 
136     public Set<Object> keySet()
137     {
138         return getWrapped(true).keySet();
139     }
140 
141     public Collection<Object> values()
142     {
143         return getWrapped(true).values();
144     }
145 
146     public Set<Entry<Object, Object>> entrySet()
147     {
148         return getWrapped(true).entrySet();
149     }
150     
151 }