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