You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2011/07/28 03:43:41 UTC

svn commit: r1151695 - /myfaces/core/branches/2.0.x/api/src/main/java/javax/faces/component/_ComponentFacetMap.java

Author: lu4242
Date: Thu Jul 28 01:43:40 2011
New Revision: 1151695

URL: http://svn.apache.org/viewvc?rev=1151695&view=rev
Log:
MYFACES-3248 UIComponentBase.getFacets() should support all Map methods

Modified:
    myfaces/core/branches/2.0.x/api/src/main/java/javax/faces/component/_ComponentFacetMap.java

Modified: myfaces/core/branches/2.0.x/api/src/main/java/javax/faces/component/_ComponentFacetMap.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.0.x/api/src/main/java/javax/faces/component/_ComponentFacetMap.java?rev=1151695&r1=1151694&r2=1151695&view=diff
==============================================================================
--- myfaces/core/branches/2.0.x/api/src/main/java/javax/faces/component/_ComponentFacetMap.java (original)
+++ myfaces/core/branches/2.0.x/api/src/main/java/javax/faces/component/_ComponentFacetMap.java Thu Jul 28 01:43:40 2011
@@ -19,7 +19,13 @@
 package javax.faces.component;
 
 import java.io.Serializable;
-import java.util.*;
+import java.util.AbstractCollection;
+import java.util.AbstractSet;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
 
 /**
  * @author Manfred Geiler (latest modification by $Author$)
@@ -30,6 +36,9 @@ class _ComponentFacetMap<V extends UICom
     private static final long serialVersionUID = -3456937594422167629L;
     private UIComponent _component;
     private Map<String, V> _map = new HashMap<String, V>();
+    private Set<Entry<String, V>> _entrySet = null;
+    private Set<String> _keySet = null;
+    private Collection<V> _valueCollection = null;
 
     _ComponentFacetMap(UIComponent component)
     {
@@ -43,7 +52,14 @@ class _ComponentFacetMap<V extends UICom
 
     public void clear()
     {
+        UIComponent[] values = _map.values().toArray(new UIComponent[_map.size()]);
+        //remove all elements from underlying map
         _map.clear();
+        //Set parent to null
+        for (int i = 0; i < values.length; i++)
+        {
+            values[i].setParent(null);
+        }
     }
 
     public boolean isEmpty()
@@ -65,7 +81,11 @@ class _ComponentFacetMap<V extends UICom
 
     public Collection<V> values()
     {
-        return _map.values();
+        if (_valueCollection == null)
+        {
+            _valueCollection= new ComponentFacetValueCollection();
+        }
+        return _valueCollection;
     }
 
     public void putAll(Map<? extends String, ? extends V> t)
@@ -78,12 +98,20 @@ class _ComponentFacetMap<V extends UICom
 
     public Set<Entry<String, V>> entrySet()
     {
-        return _map.entrySet();
+        if (_entrySet == null)
+        {
+            _entrySet = new ComponentFacetEntrySet();
+        }
+        return _entrySet;
     }
 
     public Set<String> keySet()
     {
-        return _map.keySet();
+        if (_keySet == null)
+        {
+            _keySet = new ComponentFacetKeySet();
+        }
+        return _keySet;
     }
 
     public V get(Object key)
@@ -161,4 +189,418 @@ class _ComponentFacetMap<V extends UICom
             throw new ClassCastException("value is not a UIComponent");
     }
 
+    private class ComponentFacetEntrySet extends AbstractSet<Entry<String, V>>
+    {
+        public ComponentFacetEntrySet()
+        {
+        }
+        
+        public int size()
+        {
+            return _map.size();
+        }
+
+        public boolean isEmpty()
+        {
+            return _map.isEmpty();
+        }
+
+        public boolean contains(Object o)
+        {
+            return _map.entrySet().contains(o);
+        }
+
+        public Iterator<java.util.Map.Entry<String, V>> iterator()
+        {
+            return new ComponentFacetEntryIterator(_map.entrySet().iterator());
+        }
+
+        public Object[] toArray()
+        {
+            return _map.entrySet().toArray();
+        }
+
+        public <T> T[] toArray(T[] a)
+        {
+            return _map.entrySet().toArray(a);
+        }
+
+        public boolean add(java.util.Map.Entry<String, V> o)
+        {
+            // Add over the entry set is not allowed, because this should be done
+            // through the outer Map instance.
+            throw new UnsupportedOperationException();
+        }
+
+        @SuppressWarnings("unchecked")
+        public boolean remove(Object o)
+        {
+            if (_map.entrySet().remove(o))
+            {
+                if (o instanceof Map.Entry)
+                {
+                    Object value = ((Map.Entry<String, V>) o).getValue();
+                    
+                    if (value != null && value instanceof UIComponent)
+                    {
+                        ((UIComponent) value).setParent(null);
+                    }
+                }
+                return true;
+            }
+            else
+            {
+                return false;
+            }
+        }
+
+        public boolean containsAll(Collection<?> c)
+        {
+            return _map.entrySet().containsAll(c);
+        }
+
+        public boolean addAll(
+                Collection<? extends java.util.Map.Entry<String, V>> c)
+        {
+            // Add over the entry set is not allowed, because this should be done
+            // through the outer Map instance.
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        public boolean equals(Object obj)
+        {
+            return _map.entrySet().equals(obj);
+        }
+
+        @Override
+        public int hashCode()
+        {
+            return _map.entrySet().hashCode();
+        }
+
+        @Override
+        public String toString()
+        {
+            return _map.entrySet().toString();
+        }
+    }
+    
+    private class ComponentFacetEntryIterator implements Iterator<Map.Entry<String, V>>
+    {
+        private Iterator<Map.Entry<String, V>> _delegate;
+        private V _currentEntryValue;
+        
+        public ComponentFacetEntryIterator(Iterator<Map.Entry<String, V>> it)
+        {
+            _delegate = it;
+            _currentEntryValue = null;
+        }
+        
+        public boolean hasNext()
+        {
+            return _delegate.hasNext();
+        }
+
+        public java.util.Map.Entry<String, V> next()
+        {
+            java.util.Map.Entry<String, V> next = _delegate.next(); 
+            _currentEntryValue = next.getValue();
+            return new ComponentFacetEntry(next);
+        }
+
+        public void remove()
+        {
+            _delegate.remove();
+            if (_currentEntryValue != null)
+            {
+                _currentEntryValue.setParent(null);
+            }
+        }
+    }
+
+    /**
+     * Wrapper used to override setValue() method
+     * 
+     */
+    private class ComponentFacetEntry implements Map.Entry<String, V>
+    {
+        private java.util.Map.Entry<String, V> _entry;
+        
+        public ComponentFacetEntry(java.util.Map.Entry<String, V> entry)
+        {
+            _entry = entry;
+        }
+
+        public String getKey()
+        {
+            return _entry.getKey();
+        }
+
+        public V getValue()
+        {
+            return _entry.getValue();
+        }
+
+        public V setValue(V value)
+        {
+            setNewParent(_entry.getKey(), value);
+            V previousValue = _entry.setValue(value);
+            if (previousValue != null)
+            {
+                previousValue.setParent(null);
+            }
+            return previousValue;
+        }
+
+        @Override
+        public int hashCode()
+        {
+            return _entry.hashCode();
+        }
+
+        @Override
+        public boolean equals(Object obj)
+        {
+            return _entry.equals(obj);
+        }
+
+        @Override
+        public String toString()
+        {
+            return _entry.toString();
+        }
+    }
+    
+    private class ComponentFacetKeySet extends AbstractSet<String>
+    {
+
+        public ComponentFacetKeySet()
+        {
+        }
+        
+        public int size()
+        {
+            return _map.keySet().size();
+        }
+
+        public boolean isEmpty()
+        {
+            return _map.keySet().isEmpty();
+        }
+
+        public boolean contains(Object o)
+        {
+            return _map.keySet().contains(o);
+        }
+
+        public Iterator<String> iterator()
+        {
+            // Iterate over entrySet is equals to iterate over keySet, but
+            // in this case is better use entrySet iterator, because we can
+            // get the value directly and call setParent(null) if the entry is
+            // removed
+            return new ComponentFacetKeyIterator(_map.entrySet().iterator());
+        }
+
+        public Object[] toArray()
+        {
+            return _map.keySet().toArray();
+        }
+
+        public <T> T[] toArray(T[] a)
+        {
+            return _map.keySet().toArray(a);
+        }
+
+        public boolean add(String o)
+        {
+            throw new UnsupportedOperationException();
+        }
+
+        public boolean remove(Object o)
+        {
+            V previousValue = _map.get(o);
+            if (_map.keySet().remove(o))
+            {
+                if (previousValue != null)
+                {
+                    ((UIComponent) previousValue).setParent(null);
+                }
+                return true;
+            }
+            else
+            {
+                return false;
+            }
+        }
+
+        public boolean containsAll(Collection<?> c)
+        {
+            return _map.keySet().containsAll(c);
+        }
+
+        public boolean addAll(Collection<? extends String> c)
+        {
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        public boolean equals(Object obj)
+        {
+            return _map.keySet().equals(obj);
+        }
+
+        @Override
+        public int hashCode()
+        {
+            return _map.keySet().hashCode();
+        }
+
+        @Override
+        public String toString()
+        {
+            return _map.keySet().toString();
+        }
+    }
+    
+    private class ComponentFacetKeyIterator implements Iterator<String>
+    {
+        private Iterator<Map.Entry<String, V>> _delegate;
+        private V _currentEntryValue;
+        
+        public ComponentFacetKeyIterator(Iterator<Map.Entry<String, V>> it)
+        {
+            _delegate = it;
+            _currentEntryValue = null;
+        }
+        
+        public boolean hasNext()
+        {
+            return _delegate.hasNext();
+        }
+
+        public String next()
+        {
+            java.util.Map.Entry<String, V> next = _delegate.next(); 
+            _currentEntryValue = next.getValue();
+            return next.getKey();
+        }
+
+        public void remove()
+        {
+            _delegate.remove();
+            if (_currentEntryValue != null)
+            {
+                _currentEntryValue.setParent(null);
+            }
+        }
+    }
+
+    private class ComponentFacetValueCollection extends AbstractCollection<V>
+    {
+        public ComponentFacetValueCollection()
+        {
+        }
+
+        public int size()
+        {
+            return _map.values().size();
+        }
+
+        public boolean isEmpty()
+        {
+            return _map.values().isEmpty();
+        }
+
+        public boolean contains(Object o)
+        {
+            return _map.values().contains(o);
+        }
+
+        public Iterator<V> iterator()
+        {
+            return new ComponentFacetValueIterator(_map.entrySet().iterator());
+        }
+
+        public Object[] toArray()
+        {
+            return _map.values().toArray();
+        }
+
+        public <T> T[] toArray(T[] a)
+        {
+            return _map.values().toArray(a);
+        }
+
+        public boolean add(V o)
+        {
+            // Add over the entry set is not allowed, because this should be done
+            // through the outer Map instance.
+            throw new UnsupportedOperationException();
+        }
+
+        public boolean containsAll(Collection<?> c)
+        {
+            return _map.values().containsAll(c);
+        }
+
+        public boolean addAll(Collection<? extends V> c)
+        {
+            // Add over the entry set is not allowed, because this should be done
+            // through the outer Map instance.
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        public boolean equals(Object obj)
+        {
+            return _map.values().equals(obj);
+        }
+
+        @Override
+        public int hashCode()
+        {
+            return _map.values().hashCode();
+        }
+
+        @Override
+        public String toString()
+        {
+            return _map.values().toString();
+        }
+    }
+    
+    private class ComponentFacetValueIterator implements Iterator<V>
+    {
+        private Iterator<Map.Entry<String, V>> _delegate;
+        private V _currentEntryValue;
+        
+        public ComponentFacetValueIterator(Iterator<Map.Entry<String, V>> it)
+        {
+            _delegate = it;
+            _currentEntryValue = null;
+        }
+        
+        public boolean hasNext()
+        {
+            return _delegate.hasNext();
+        }
+
+        public V next()
+        {
+            java.util.Map.Entry<String, V> next = _delegate.next(); 
+            _currentEntryValue = next.getValue();
+            return next.getValue();
+        }
+
+        public void remove()
+        {
+            _delegate.remove();
+            if (_currentEntryValue != null)
+            {
+                _currentEntryValue.setParent(null);
+            }
+        }
+    }
 }