Coverage Report - org.apache.tapestry5.internal.grid.CollectionGridDataSource
 
Classes in this File Line Coverage Branch Coverage Complexity
CollectionGridDataSource
100%
17/17
67%
4/6
0
CollectionGridDataSource$1
100%
5/5
100%
6/6
0
CollectionGridDataSource$2
100%
4/4
N/A
0
CollectionGridDataSource$3
100%
3/3
100%
2/2
0
 
 1  
 // Copyright 2007, 2008 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.tapestry5.internal.grid;
 16  
 
 17  
 import org.apache.tapestry5.PropertyConduit;
 18  
 import org.apache.tapestry5.grid.ColumnSort;
 19  
 import org.apache.tapestry5.grid.GridDataSource;
 20  
 import org.apache.tapestry5.grid.SortConstraint;
 21  
 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
 22  
 import org.apache.tapestry5.ioc.internal.util.Defense;
 23  
 
 24  
 import java.util.Collection;
 25  
 import java.util.Collections;
 26  
 import java.util.Comparator;
 27  
 import java.util.List;
 28  
 
 29  
 public class CollectionGridDataSource implements GridDataSource
 30  
 {
 31  
     private final List list;
 32  
 
 33  
     @SuppressWarnings("unchecked")
 34  
     public CollectionGridDataSource(final Collection collection)
 35  120
     {
 36  120
         Defense.notNull(collection, "collection");
 37  
 
 38  
         // Copy the collection so that we can sort it without disturbing the original
 39  
 
 40  120
         list = CollectionFactory.newList(collection);
 41  120
     }
 42  
 
 43  
     public int getAvailableRows()
 44  
     {
 45  116
         return list.size();
 46  
     }
 47  
 
 48  
     public void prepare(int startIndex, int endIndex, List<SortConstraint> sortConstraints)
 49  
     {
 50  112
         for (SortConstraint constraint : sortConstraints)
 51  
         {
 52  34
             final ColumnSort sort = constraint.getColumnSort();
 53  
 
 54  34
             if (sort == ColumnSort.UNSORTED) continue;
 55  
 
 56  34
             final PropertyConduit conduit = constraint.getPropertyModel().getConduit();
 57  
 
 58  34
             final Comparator valueComparator = new Comparator<Comparable>()
 59  
             {
 60  210126
                 public int compare(Comparable o1, Comparable o2)
 61  
                 {
 62  
                     // Simplify comparison, and handle case where both are nulls.
 63  
 
 64  210092
                     if (o1 == o2) return 0;
 65  
 
 66  210066
                     if (o2 == null) return 1;
 67  
 
 68  209886
                     if (o1 == null) return -1;
 69  
 
 70  209838
                     return o1.compareTo(o2);
 71  
                 }
 72  
             };
 73  
 
 74  34
             final Comparator rowComparator = new Comparator()
 75  
             {
 76  34
                 public int compare(Object row1, Object row2)
 77  
                 {
 78  210092
                     Comparable value1 = (Comparable) conduit.get(row1);
 79  210092
                     Comparable value2 = (Comparable) conduit.get(row2);
 80  
 
 81  210092
                     return valueComparator.compare(value1, value2);
 82  
                 }
 83  
             };
 84  
 
 85  34
             final Comparator reverseComparator = new Comparator()
 86  
             {
 87  34
                 public int compare(Object o1, Object o2)
 88  
                 {
 89  210092
                     int modifier = sort == ColumnSort.ASCENDING ? 1 : -1;
 90  
 
 91  210092
                     return modifier * rowComparator.compare(o1, o2);
 92  
                 }
 93  
             };
 94  
 
 95  
             // We can freely sort this list because its just a copy.
 96  
 
 97  34
             Collections.sort(list, reverseComparator);
 98  34
         }
 99  112
     }
 100  
 
 101  
     /**
 102  
      * Returns the type of the first element in the list, or null if the list is empty.
 103  
      */
 104  
     public Class getRowType()
 105  
     {
 106  102
         return list.isEmpty() ? null : list.get(0).getClass();
 107  
     }
 108  
 
 109  
     public Object getRowValue(int index)
 110  
     {
 111  1826
         return list.get(index);
 112  
     }
 113  
 
 114  
 }