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