Coverage Report - org.apache.tapestry.contrib.table.model.simple.SimpleListTableDataModel
 
Classes in this File Line Coverage Branch Coverage Complexity
SimpleListTableDataModel
0%
0/37
0%
0/6
1.308
 
 1  
 // Copyright 2004, 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.contrib.table.model.simple;
 16  
 
 17  
 import java.io.Serializable;
 18  
 import java.util.ArrayList;
 19  
 import java.util.Arrays;
 20  
 import java.util.Collection;
 21  
 import java.util.Iterator;
 22  
 import java.util.List;
 23  
 
 24  
 import org.apache.tapestry.contrib.table.model.CTableDataModelEvent;
 25  
 import org.apache.tapestry.contrib.table.model.common.AbstractTableDataModel;
 26  
 import org.apache.tapestry.contrib.table.model.common.ArrayIterator;
 27  
 
 28  
 /**
 29  
  * A minimal list implementation of the
 30  
  * {@link org.apache.tapestry.contrib.table.model.ITableDataModel} interface.
 31  
  * 
 32  
  * @author mindbridge
 33  
  */
 34  
 public class SimpleListTableDataModel extends AbstractTableDataModel implements
 35  
         Serializable
 36  
 {
 37  
 
 38  
     private static final long serialVersionUID = 1L;
 39  
 
 40  
     private List m_arrRows;
 41  
 
 42  
     public SimpleListTableDataModel(Object[] arrRows)
 43  
     {
 44  0
         this(Arrays.asList(arrRows));
 45  0
     }
 46  
 
 47  
     public SimpleListTableDataModel(List arrRows)
 48  0
     {
 49  0
         m_arrRows = arrRows;
 50  0
     }
 51  
 
 52  
     public SimpleListTableDataModel(Collection arrRows)
 53  0
     {
 54  0
         m_arrRows = new ArrayList(arrRows);
 55  0
     }
 56  
 
 57  
     public SimpleListTableDataModel(Iterator objRows)
 58  0
     {
 59  0
         m_arrRows = new ArrayList();
 60  0
         addAll(m_arrRows, objRows);
 61  0
     }
 62  
 
 63  
     private void addAll(List arrRows, Iterator objRows)
 64  
     {
 65  0
         while(objRows.hasNext())
 66  0
             arrRows.add(objRows.next());
 67  0
     }
 68  
 
 69  
     /**
 70  
      * @see org.apache.tapestry.contrib.table.model.ITableDataModel#getRowCount()
 71  
      */
 72  
     public int getRowCount()
 73  
     {
 74  0
         return m_arrRows.size();
 75  
     }
 76  
 
 77  
     /**
 78  
      * Returns the row element at the given position.
 79  
      * 
 80  
      * @param nRow
 81  
      *            the index of the element to return
 82  
      */
 83  
     public Object getRow(int nRow)
 84  
     {
 85  0
         if (nRow < 0 || nRow >= m_arrRows.size())
 86  
         {
 87  
             // error message
 88  0
             return null;
 89  
         }
 90  0
         return m_arrRows.get(nRow);
 91  
     }
 92  
 
 93  
     /**
 94  
      * Returns an Iterator with the elements from the given range.
 95  
      * 
 96  
      * @param nFrom
 97  
      *            the start of the range (inclusive)
 98  
      * @param nTo
 99  
      *            the stop of the range (exclusive)
 100  
      */
 101  
     public Iterator getRows(int nFrom, int nTo)
 102  
     {
 103  0
         return new ArrayIterator(m_arrRows.toArray(), nFrom, nTo);
 104  
     }
 105  
 
 106  
     /**
 107  
      * @see org.apache.tapestry.contrib.table.model.ITableDataModel#getRows()
 108  
      */
 109  
     public Iterator getRows()
 110  
     {
 111  0
         return m_arrRows.iterator();
 112  
     }
 113  
 
 114  
     /**
 115  
      * Method addRow. Adds a row object to the model at its end
 116  
      * 
 117  
      * @param objRow
 118  
      *            the row object to add
 119  
      */
 120  
     public void addRow(Object objRow)
 121  
     {
 122  0
         m_arrRows.add(objRow);
 123  
 
 124  0
         CTableDataModelEvent objEvent = new CTableDataModelEvent();
 125  0
         fireTableDataModelEvent(objEvent);
 126  0
     }
 127  
 
 128  
     public void addRows(Collection arrRows)
 129  
     {
 130  0
         m_arrRows.addAll(arrRows);
 131  
 
 132  0
         CTableDataModelEvent objEvent = new CTableDataModelEvent();
 133  0
         fireTableDataModelEvent(objEvent);
 134  0
     }
 135  
 
 136  
     /**
 137  
      * Method removeRow. Removes a row object from the model
 138  
      * 
 139  
      * @param objRow
 140  
      *            the row object to remove
 141  
      */
 142  
     public void removeRow(Object objRow)
 143  
     {
 144  0
         m_arrRows.remove(objRow);
 145  
 
 146  0
         CTableDataModelEvent objEvent = new CTableDataModelEvent();
 147  0
         fireTableDataModelEvent(objEvent);
 148  0
     }
 149  
 
 150  
     public void removeRows(Collection arrRows)
 151  
     {
 152  0
         m_arrRows.removeAll(arrRows);
 153  
 
 154  0
         CTableDataModelEvent objEvent = new CTableDataModelEvent();
 155  0
         fireTableDataModelEvent(objEvent);
 156  0
     }
 157  
 
 158  
 }