Coverage Report - javax.faces.component._DeltaList
 
Classes in this File Line Coverage Branch Coverage Complexity
_DeltaList
73%
85/116
85%
46/54
1.941
 
 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.util.ArrayList;
 22  
 import java.util.Collection;
 23  
 import java.util.Iterator;
 24  
 import java.util.List;
 25  
 import java.util.ListIterator;
 26  
 import java.util.RandomAccess;
 27  
 
 28  
 import javax.faces.context.FacesContext;
 29  
 
 30  
 /**
 31  
  * This class handle deltas on facesListener and validatorList.
 32  
  * 
 33  
  * It is only used by this methods on UIComponentBase:
 34  
  * 
 35  
  * addFacesListener
 36  
  * broadcast
 37  
  * getFacesListeners
 38  
  * removeFacesListener
 39  
  * 
 40  
  * A facesListener could hold PartialStateHolder instances, so it 
 41  
  * is necessary to provide convenient methods to track deltas.
 42  
  */
 43  
 class _DeltaList<T> implements List<T>, PartialStateHolder, RandomAccess
 44  
 {
 45  2
     private static Object[] emptyObjectArray = new Object[]{};
 46  
 
 47  
     private List<T> _delegate;
 48  
     private boolean _initialStateMarked;
 49  
     
 50  
     public _DeltaList()
 51  44
     {
 52  44
     }
 53  
     
 54  
     public _DeltaList(List<T> delegate)
 55  132
     {
 56  132
         _delegate = delegate;
 57  132
     }
 58  
     
 59  
     public void add(int index, T element)
 60  
     {
 61  0
         clearInitialState();
 62  0
         _delegate.add(index, element);
 63  0
     }
 64  
 
 65  
     public boolean add(T e)
 66  
     {
 67  172
         clearInitialState();
 68  172
         return _delegate.add(e);
 69  
     }
 70  
 
 71  
     public boolean addAll(Collection<? extends T> c)
 72  
     {
 73  0
         clearInitialState();
 74  0
         return _delegate.addAll(c);
 75  
     }
 76  
 
 77  
     public boolean addAll(int index, Collection<? extends T> c)
 78  
     {
 79  0
         clearInitialState();
 80  0
         return _delegate.addAll(index, c);
 81  
     }
 82  
 
 83  
     public void clear()
 84  
     {
 85  0
         clearInitialState();
 86  0
         _delegate.clear();
 87  0
     }
 88  
 
 89  
     public boolean contains(Object o)
 90  
     {
 91  152
         return _delegate.contains(o);
 92  
     }
 93  
 
 94  
     public boolean containsAll(Collection<?> c)
 95  
     {
 96  0
         return _delegate.containsAll(c);
 97  
     }
 98  
 
 99  
     public boolean equals(Object o)
 100  
     {
 101  0
         return _delegate.equals(o);
 102  
     }
 103  
 
 104  
     public T get(int index)
 105  
     {
 106  216
         return _delegate.get(index);
 107  
     }
 108  
 
 109  
     public int hashCode()
 110  
     {
 111  0
         return _delegate.hashCode();
 112  
     }
 113  
 
 114  
     public int indexOf(Object o)
 115  
     {
 116  4
         return _delegate.indexOf(o);
 117  
     }
 118  
 
 119  
     public boolean isEmpty()
 120  
     {
 121  2
         return _delegate.isEmpty();
 122  
     }
 123  
 
 124  
     public Iterator<T> iterator()
 125  
     {
 126  0
         return _delegate.iterator();
 127  
     }
 128  
 
 129  
     public int lastIndexOf(Object o)
 130  
     {
 131  0
         return _delegate.lastIndexOf(o);
 132  
     }
 133  
 
 134  
     public ListIterator<T> listIterator()
 135  
     {
 136  0
         return _delegate.listIterator();
 137  
     }
 138  
 
 139  
     public ListIterator<T> listIterator(int index)
 140  
     {
 141  0
         return _delegate.listIterator(index);
 142  
     }
 143  
 
 144  
     public T remove(int index)
 145  
     {
 146  0
         clearInitialState();
 147  0
         return _delegate.remove(index);
 148  
     }
 149  
 
 150  
     public boolean remove(Object o)
 151  
     {
 152  102
         clearInitialState();
 153  102
         return _delegate.remove(o);
 154  
     }
 155  
 
 156  
     public boolean removeAll(Collection<?> c)
 157  
     {
 158  0
         clearInitialState();
 159  0
         return _delegate.removeAll(c);
 160  
     }
 161  
 
 162  
     public boolean retainAll(Collection<?> c)
 163  
     {
 164  0
         clearInitialState();
 165  0
         return _delegate.retainAll(c);
 166  
     }
 167  
 
 168  
     public T set(int index, T element)
 169  
     {
 170  0
         clearInitialState();
 171  0
         return _delegate.set(index, element);
 172  
     }
 173  
 
 174  
     public int size()
 175  
     {
 176  222
         return _delegate == null ? 0 : _delegate.size();
 177  
     }
 178  
 
 179  
     public List<T> subList(int fromIndex, int toIndex)
 180  
     {
 181  0
         return _delegate.subList(fromIndex, toIndex);
 182  
     }
 183  
 
 184  
     public Object[] toArray()
 185  
     {
 186  0
         return _delegate.toArray();
 187  
     }
 188  
 
 189  
     public <T> T[] toArray(T[] a)
 190  
     {
 191  10
         return _delegate.toArray(a);
 192  
     }
 193  
 
 194  
     public boolean isTransient()
 195  
     {
 196  44
         return false;
 197  
     }
 198  
 
 199  
     public void setTransient(boolean newTransientValue)
 200  
     {
 201  0
         throw new UnsupportedOperationException();
 202  
     }
 203  
 
 204  
     public void restoreState(FacesContext context, Object state)
 205  
     {
 206  60
         if (state == null)
 207  
         {
 208  0
             return;
 209  
         }
 210  
         
 211  60
         if (initialStateMarked())
 212  
         {            
 213  
             //Restore delta
 214  16
             Object[] lst = (Object[]) state;
 215  16
             int j = 0;
 216  16
             int i = 0;
 217  40
             while (i < lst.length)
 218  
             {
 219  24
                 if (lst[i] instanceof _AttachedDeltaWrapper)
 220  
                 {
 221  
                     //Delta
 222  2
                     ((StateHolder)_delegate.get(j)).restoreState(context,
 223  
                             ((_AttachedDeltaWrapper) lst[i]).getWrappedStateObject());
 224  2
                     j++;
 225  
                 }
 226  22
                 else if (lst[i] != null)
 227  
                 {
 228  
                     //Full
 229  12
                     _delegate.set(j, (T) UIComponentBase.restoreAttachedState(context, lst[i]));
 230  12
                     j++;
 231  
                 }
 232  
                 else
 233  
                 {
 234  10
                     _delegate.remove(j);
 235  
                 }
 236  24
                 i++;
 237  
             }
 238  16
             if (i != j)
 239  
             {
 240  
                 // StateHolder transient objects found, next time save and restore it fully
 241  
                 //because the size of the list changes.
 242  10
                 clearInitialState();
 243  
             }
 244  16
         }
 245  
         else
 246  
         {
 247  
             //Restore delegate
 248  44
             Object[] lst = (Object[]) state;
 249  44
             _delegate = new ArrayList<T>(lst.length);
 250  100
             for (int i = 0; i < lst.length; i++)
 251  
             {
 252  56
                 T value = (T) UIComponentBase.restoreAttachedState(context, lst[i]);
 253  56
                 if (value != null)
 254  
                 {
 255  44
                     _delegate.add(value);
 256  
                 }
 257  
             }
 258  
         }
 259  60
     }
 260  
 
 261  
     public Object saveState(FacesContext context)
 262  
     {
 263  68
         int size = _delegate.size();
 264  68
         if (initialStateMarked())
 265  
         {
 266  24
             Object [] lst = null;
 267  24
             boolean nullDelta = true;
 268  24
             if (size > 0)
 269  
             {
 270  24
                 lst = new Object[size];
 271  56
                 for (int i = 0; i < size; i++)
 272  
                 {
 273  32
                     Object value = _delegate.get(i);
 274  32
                     if (value instanceof PartialStateHolder)
 275  
                     {
 276  
                         //Delta
 277  6
                         PartialStateHolder holder = (PartialStateHolder) value;
 278  6
                         if (!holder.isTransient())
 279  
                         {
 280  6
                             Object attachedState = holder.saveState(context);
 281  6
                             if (attachedState != null)
 282  
                             {
 283  0
                                 nullDelta = false;
 284  
                             }
 285  6
                             lst[i] = new _AttachedDeltaWrapper(value.getClass(),
 286  
                                 attachedState);
 287  
                         }
 288  6
                     }
 289  
                     else
 290  
                     {
 291  
                         //Full
 292  26
                         lst[i] = UIComponentBase.saveAttachedState(context, value);
 293  26
                         if (value instanceof StateHolder || value instanceof List)
 294  
                         {
 295  20
                             nullDelta = false;
 296  
                         }
 297  
                     }
 298  
                 }
 299  
             }
 300  
             else
 301  
             {
 302  0
                 lst = emptyObjectArray;
 303  
             }
 304  24
             if (nullDelta)
 305  
             {
 306  8
                 return null;
 307  
             }
 308  16
             return lst;
 309  
         }
 310  
         else
 311  
         {
 312  44
             if (size > 0)
 313  
             {
 314  36
                 Object [] lst = new Object[size];
 315  92
                 for (int i = 0; i < size; i++)
 316  
                 {
 317  56
                     lst[i] = UIComponentBase.saveAttachedState(context, _delegate.get(i));
 318  
                 }
 319  36
                 return lst;
 320  
             }
 321  
             else
 322  
             {
 323  8
                 return emptyObjectArray;
 324  
             }
 325  
         }
 326  
     }
 327  
 
 328  
     public void clearInitialState()
 329  
     {
 330  
         //Reset delta setting to null
 331  292
         if (_initialStateMarked)
 332  
         {
 333  54
             _initialStateMarked = false;
 334  54
             if (_delegate != null)
 335  
             {
 336  54
                 for (T value : _delegate)
 337  
                 {
 338  48
                     if (value instanceof PartialStateHolder)
 339  
                     {
 340  12
                         ((PartialStateHolder)value).clearInitialState();
 341  
                     }
 342  48
                 }
 343  
             }
 344  
         }
 345  292
     }
 346  
 
 347  
     public boolean initialStateMarked()
 348  
     {
 349  180
         return _initialStateMarked;
 350  
     }
 351  
 
 352  
     public void markInitialState()
 353  
     {
 354  88
         _initialStateMarked = true;
 355  88
         if (_delegate != null)
 356  
         {
 357  88
             int size = _delegate.size();
 358  192
             for (int i = 0; i < size; i++)
 359  
             {
 360  104
                 T value = _delegate.get(i);
 361  104
                 if (value instanceof PartialStateHolder)
 362  
                 {
 363  20
                     ((PartialStateHolder)value).markInitialState();
 364  
                 }
 365  
             }
 366  
         }
 367  88
     }
 368  
 }