001: /*
002: * Copyright 2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package javax.faces.component;
017:
018: import java.io.Serializable;
019: import java.util.*;
020:
021: /**
022: * @author Manfred Geiler (latest modification by $Author: mbr $)
023: * @version $Revision: 511135 $ $Date: 2007-02-23 23:08:15 +0100 (Fr, 23 Feb 2007) $
024: */
025: class _ComponentFacetMap<V extends UIComponent> implements
026: Map<String, V>, Serializable {
027: private static final long serialVersionUID = -3456937594422167629L;
028: private UIComponent _component;
029: private Map<String, V> _map = new HashMap<String, V>();
030:
031: _ComponentFacetMap(UIComponent component) {
032: _component = component;
033: }
034:
035: public int size() {
036: return _map.size();
037: }
038:
039: public void clear() {
040: _map.clear();
041: }
042:
043: public boolean isEmpty() {
044: return _map.isEmpty();
045: }
046:
047: public boolean containsKey(Object key) {
048: checkKey(key);
049: return _map.containsKey(key);
050: }
051:
052: public boolean containsValue(Object value) {
053: checkValue(value);
054: return _map.containsValue(value);
055: }
056:
057: public Collection<V> values() {
058: return _map.values();
059: }
060:
061: public void putAll(Map<? extends String, ? extends V> t) {
062: for (Iterator it = t.entrySet().iterator(); it.hasNext();) {
063: Map.Entry<String, V> entry = (Entry<String, V>) it.next();
064: put(entry.getKey(), entry.getValue());
065: }
066: }
067:
068: public Set<Entry<String, V>> entrySet() {
069: return _map.entrySet();
070: }
071:
072: public Set<String> keySet() {
073: return _map.keySet();
074: }
075:
076: public V get(Object key) {
077: checkKey(key);
078: return _map.get(key);
079: }
080:
081: public V remove(Object key) {
082: checkKey(key);
083: V facet = _map.remove(key);
084: if (facet != null)
085: facet.setParent(null);
086: return facet;
087: }
088:
089: public V put(String key, V value) {
090: checkKey(key);
091: checkValue(value);
092: setNewParent(key, value);
093: return _map.put(key, value);
094: }
095:
096: private void setNewParent(String facetName, UIComponent facet) {
097: UIComponent oldParent = facet.getParent();
098: if (oldParent != null) {
099: oldParent.getFacets().remove(facetName);
100: }
101: facet.setParent(_component);
102: }
103:
104: private void checkKey(Object key) {
105: if (key == null)
106: throw new NullPointerException("key");
107: if (!(key instanceof String))
108: throw new ClassCastException("key is not a String");
109: }
110:
111: private void checkValue(Object value) {
112: if (value == null)
113: throw new NullPointerException("value");
114: if (!(value instanceof UIComponent))
115: throw new ClassCastException("value is not a UIComponent");
116: }
117:
118: }
|