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 javax.faces.component;
20  
21  import java.io.Serializable;
22  import java.util.*;
23  
24  /**
25   * @author Manfred Geiler (latest modification by $Author: skitching $)
26   * @version $Revision: 676298 $ $Date: 2008-07-13 05:31:48 -0500 (Sun, 13 Jul 2008) $
27   */
28  class _ComponentFacetMap<V extends UIComponent>
29          implements Map<String, V>, Serializable
30  {
31      private static final long serialVersionUID = -3456937594422167629L;
32      private UIComponent _component;
33      private Map<String, V> _map = new HashMap<String, V>();
34  
35      _ComponentFacetMap(UIComponent component)
36      {
37          _component = component;
38      }
39  
40      public int size()
41      {
42          return _map.size();
43      }
44  
45      public void clear()
46      {
47          _map.clear();
48      }
49  
50      public boolean isEmpty()
51      {
52          return _map.isEmpty();
53      }
54  
55      public boolean containsKey(Object key)
56      {
57          checkKey(key);
58          return _map.containsKey(key);
59      }
60  
61      public boolean containsValue(Object value)
62      {
63          checkValue(value);
64          return _map.containsValue(value);
65      }
66  
67      public Collection<V> values()
68      {
69          return _map.values();
70      }
71  
72      public void putAll(Map<? extends String, ? extends V> t)
73      {
74          for (Iterator it = t.entrySet().iterator(); it.hasNext(); )
75          {
76              Map.Entry<String, V> entry = (Entry<String, V>)it.next();
77              put(entry.getKey(), entry.getValue());
78          }
79      }
80  
81      public Set<Entry<String, V>> entrySet()
82      {
83          return _map.entrySet();
84      }
85  
86      public Set<String> keySet()
87      {
88          return _map.keySet();
89      }
90  
91      public V get(Object key)
92      {
93          checkKey(key);
94          return _map.get(key);
95      }
96  
97      public V remove(Object key)
98      {
99          checkKey(key);
100         V facet = _map.remove(key);
101         if (facet != null) facet.setParent(null);
102         return facet;
103     }
104 
105     public V put(String key, V value)
106     {
107         checkKey(key);
108         checkValue(value);
109         setNewParent(key, value);
110         return _map.put(key, value);
111     }
112 
113 
114     private void setNewParent(String facetName, UIComponent facet)
115     {
116         UIComponent oldParent = facet.getParent();
117         if (oldParent != null)
118         {
119             oldParent.getFacets().remove(facetName);
120         }
121         facet.setParent(_component);
122     }
123 
124     private void checkKey(Object key)
125     {
126         if (key == null) throw new NullPointerException("key");
127         if (!(key instanceof String)) throw new ClassCastException("key is not a String");
128     }
129 
130     private void checkValue(Object value)
131     {
132         if (value == null) throw new NullPointerException("value");
133         if (!(value instanceof UIComponent)) throw new ClassCastException("value is not a UIComponent");
134     }
135 
136 }