Coverage Report - javax.faces.component._ComponentChildrenList
 
Classes in this File Line Coverage Branch Coverage Complexity
_ComponentChildrenList
87%
48/55
59%
13/22
2.167
 
 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.AbstractList;
 23  
 import java.util.ArrayList;
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 import java.util.Map;
 27  
 import java.util.RandomAccess;
 28  
 
 29  1674
 class _ComponentChildrenList extends AbstractList<UIComponent> implements Serializable, RandomAccess
 30  
 {
 31  
     private static final long serialVersionUID = -6775078929331154224L;
 32  
     private UIComponent _component;
 33  252
     private List<UIComponent> _list = new ArrayList<UIComponent>(4);
 34  
 
 35  
     _ComponentChildrenList(UIComponent component)
 36  252
     {
 37  252
         _component = component;
 38  252
     }
 39  
 
 40  
     @Override
 41  
     public UIComponent get(int index)
 42  
     {
 43  1336
         return _list.get(index);
 44  
     }
 45  
 
 46  
     @Override
 47  
     public int size()
 48  
     {
 49  3072
         return _list.size();
 50  
     }
 51  
 
 52  
     @Override
 53  
     public UIComponent set(int index, UIComponent value)
 54  
     {
 55  4
         checkValue(value);
 56  4
         removeChildrenFromParent(value);
 57  4
         UIComponent child = _list.set(index, value);
 58  4
         if (child != value)
 59  
         {
 60  4
             updateParent(value);
 61  4
             if (child != null)
 62  
             {
 63  4
                 childRemoved(child);
 64  
             }
 65  
         }
 66  
         
 67  4
         return child;
 68  
     }
 69  
 
 70  
     @Override
 71  
     public boolean add(UIComponent value)
 72  
     {
 73  328
         checkValue(value);
 74  
 
 75  328
         removeChildrenFromParent(value);
 76  328
         boolean res = _list.add(value);
 77  
         
 78  328
         updateParent(value);
 79  
         
 80  328
         return res;
 81  
     }
 82  
 
 83  
     @Override
 84  
     public void add(int index, UIComponent value)
 85  
     {
 86  6
         checkValue(value);
 87  
         
 88  6
         removeChildrenFromParent(value);
 89  
         
 90  6
         _list.add(index, value);
 91  
         
 92  6
         updateParent(value);
 93  6
     }
 94  
 
 95  
     @Override
 96  
     public UIComponent remove(int index)
 97  
     {
 98  0
         UIComponent child = _list.remove(index);
 99  0
         if (child != null)
 100  
         {
 101  0
             childRemoved(child);
 102  
         }
 103  
         
 104  0
         return child;
 105  
     }
 106  
 
 107  
     private void checkValue(Object value)
 108  
     {
 109  370
         if (value == null)
 110  
         {
 111  0
             throw new NullPointerException("value");
 112  
         }
 113  
         
 114  
         // Not necessary anymore because  
 115  
         //if (!(value instanceof UIComponent))
 116  
         //{
 117  
         //    throw new ClassCastException("value is not a UIComponent");
 118  
         //}
 119  370
     }
 120  
 
 121  
     private void childRemoved(UIComponent child)
 122  
     {
 123  32
         child.setParent(null);
 124  32
     }
 125  
 
 126  
     private void updateParent(UIComponent child)
 127  
     {
 128  338
         child.setParent(_component);
 129  338
     }
 130  
     
 131  
     private void removeChildrenFromParent(UIComponent child)
 132  
     {
 133  338
         UIComponent oldParent = child.getParent();
 134  338
         if (oldParent != null)
 135  
         {
 136  6
             if (!oldParent.getChildren().remove(child))
 137  
             {
 138  
                 // Check if the component is inside a facet and remove from there
 139  2
                 if (oldParent.getFacetCount() > 0)
 140  
                 {
 141  2
                     for (Iterator< Map.Entry<String, UIComponent > > it = 
 142  2
                         oldParent.getFacets().entrySet().iterator() ; it.hasNext() ; )
 143  
                     {
 144  2
                         Map.Entry<String, UIComponent > entry = it.next();
 145  
                         
 146  2
                         if (entry.getValue().equals(child))
 147  
                         {
 148  2
                             it.remove();
 149  2
                             break;
 150  
                         }
 151  0
                     }
 152  
                 }
 153  
             }
 154  
         }
 155  338
     }
 156  
 
 157  
     @Override
 158  
     public boolean remove(Object value)
 159  
     {
 160  32
         if (!(value instanceof UIComponent))
 161  
         {
 162  0
             throw new ClassCastException("value is not a UIComponent");
 163  
         }
 164  
         
 165  32
         checkValue(value);
 166  
 
 167  32
         if (_list.remove(value))
 168  
         {
 169  28
             childRemoved((UIComponent)value);
 170  28
             return true;
 171  
         }
 172  4
         return false;
 173  
     }
 174  
 }