Coverage Report - javax.faces.model.CollectionDataModel
 
Classes in this File Line Coverage Branch Coverage Complexity
CollectionDataModel
61%
24/39
57%
15/26
2.889
 
 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.model;
 20  
 
 21  
 import java.util.Collection;
 22  
 
 23  
 public class CollectionDataModel<E> extends DataModel<E>
 24  
 {
 25  
 
 26  
     // FIELDS
 27  2
     private int _rowIndex = -1;
 28  
     private Collection<E> _data;
 29  
     private E[] _dataArray;
 30  
 
 31  
     // CONSTRUCTORS
 32  
     public CollectionDataModel()
 33  
     {
 34  0
         super();
 35  0
     }
 36  
 
 37  
     public CollectionDataModel(Collection<E> collection)
 38  2
     {
 39  2
         if (collection == null)
 40  
         {
 41  0
             throw new NullPointerException("collection");
 42  
         }
 43  2
         setWrappedData(collection);
 44  2
     }
 45  
 
 46  
     // METHODS
 47  
     @Override
 48  
     public int getRowCount()
 49  
     {
 50  0
         if (_data == null)
 51  
         {
 52  0
             return -1;
 53  
         }
 54  0
         return _data.size();
 55  
     }
 56  
 
 57  
     @Override
 58  
     public E getRowData()
 59  
     {
 60  16
         if (_data == null)
 61  
         {
 62  0
             return null;
 63  
         }
 64  16
         if (!isRowAvailable())
 65  
         {
 66  0
             throw new IllegalArgumentException("row is unavailable");
 67  
         }
 68  16
         return _dataArray[_rowIndex];
 69  
     }
 70  
 
 71  
     @Override
 72  
     public int getRowIndex()
 73  
     {
 74  0
         return _rowIndex;
 75  
     }
 76  
 
 77  
     @Override
 78  
     public Object getWrappedData()
 79  
     {
 80  0
         return _data;
 81  
     }
 82  
 
 83  
     @Override
 84  
     public boolean isRowAvailable()
 85  
     {
 86  34
         return _data != null && _rowIndex >= 0 && _rowIndex < _data.size();
 87  
     }
 88  
 
 89  
     @Override
 90  
     public void setRowIndex(int rowIndex)
 91  
     {
 92  12
         if (rowIndex < -1)
 93  
         {
 94  0
             throw new IllegalArgumentException("illegal rowIndex " + rowIndex);
 95  
         }
 96  12
         int oldRowIndex = _rowIndex;
 97  12
         _rowIndex = rowIndex;
 98  12
         if (_data != null && oldRowIndex != _rowIndex)
 99  
         {
 100  10
             E data = isRowAvailable() ? getRowData() : null;
 101  10
             DataModelEvent event = new DataModelEvent(this, _rowIndex, data);
 102  10
             DataModelListener[] listeners = getDataModelListeners();
 103  10
             for (int i = 0; i < listeners.length; i++)
 104  
             {
 105  0
                 listeners[i].rowSelected(event);
 106  
             }
 107  
         }
 108  12
     }
 109  
 
 110  
     @Override
 111  
     public void setWrappedData(Object data)
 112  
     {
 113  2
         if (data == null)
 114  
         {
 115  0
             setRowIndex(-1);
 116  0
             _data = null;
 117  0
             _dataArray = null;
 118  
         }
 119  
         else
 120  
         {
 121  2
             _data = (Collection<E>)data;
 122  2
             _dataArray = _data.toArray((E[]) new Object[_data.size()]);
 123  2
             _rowIndex = -1;
 124  2
             setRowIndex(0);
 125  
         }
 126  2
     }
 127  
 
 128  
 }