Clover coverage report - Maven Clover report
Coverage timestamp: Sun Jan 3 2010 19:08:46 CET
file stats: LOC: 213   Methods: 5
NCLOC: 117   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
RowSorter.java 54.2% 71.1% 60% 64.9%
coverage coverage
 1    /**
 2    * Licensed under the Artistic License; you may not use this file
 3    * except in compliance with the License.
 4    * You may obtain a copy of the License at
 5    *
 6    * http://displaytag.sourceforge.net/license.html
 7    *
 8    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
 9    * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 10    * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 11    */
 12    package org.displaytag.model;
 13   
 14    import java.util.Comparator;
 15   
 16    import org.apache.commons.lang.builder.EqualsBuilder;
 17    import org.apache.commons.lang.builder.HashCodeBuilder;
 18    import org.displaytag.decorator.TableDecorator;
 19    import org.displaytag.exception.ObjectLookupException;
 20    import org.displaytag.exception.RuntimeLookupException;
 21    import org.displaytag.util.LookupUtil;
 22   
 23   
 24    /**
 25    * Comparator for rows.
 26    * @author Fabrizio Giustina
 27    * @version $Revision: 1139 $ ($Author: fgiust $)
 28    */
 29    public class RowSorter implements Comparator
 30    {
 31   
 32    /**
 33    * name of the property in bean.
 34    */
 35    private String property;
 36   
 37    /**
 38    * table decorator.
 39    */
 40    private TableDecorator decorator;
 41   
 42    /**
 43    * sort order ascending?
 44    */
 45    private boolean ascending;
 46   
 47    /**
 48    * Index of the sorted column.
 49    */
 50    private int columnIndex;
 51   
 52    /**
 53    * Comparator used for comparisons.
 54    */
 55    private Comparator comparator;
 56   
 57    /**
 58    * Initialize a new RowSorter.
 59    * @param sortedColumnIndex index of the sorted column
 60    * @param beanProperty name of the property. If pProperty is null column index is used to get a static cell value
 61    * from the row object
 62    * @param tableDecorator TableDecorator instance
 63    * @param ascendingOrder boolean ascending order?
 64    * @param compar the comparator to use
 65    */
 66  102 public RowSorter(
 67    int sortedColumnIndex,
 68    String beanProperty,
 69    TableDecorator tableDecorator,
 70    boolean ascendingOrder,
 71    Comparator compar)
 72    {
 73  102 this.columnIndex = sortedColumnIndex;
 74  102 this.property = beanProperty;
 75  102 this.decorator = tableDecorator;
 76  102 this.ascending = ascendingOrder;
 77  102 this.comparator = compar;
 78  102 if (compar == null)
 79    {
 80  0 throw new IllegalArgumentException(
 81    "A null comparator has been passed to RowSorter. A comparator instance is required");
 82    }
 83    }
 84   
 85    /**
 86    * Compares two objects by first fetching a property from each object and then comparing that value. If there are
 87    * any errors produced while trying to compare these objects then a RunTimeException will be thrown as any error
 88    * found here will most likely be a programming error that needs to be quickly addressed (like trying to compare
 89    * objects that are not comparable, or trying to read a property from a bean that is invalid, etc...)
 90    * @param object1 Object
 91    * @param object2 Object
 92    * @return int
 93    * @see java.util.Comparator#compare(Object, Object)
 94    */
 95  156 public final int compare(Object object1, Object object2)
 96    {
 97   
 98  156 Object obj1 = null;
 99  156 Object obj2 = null;
 100   
 101    // if property is null compare using two static cell objects
 102  156 if (this.property == null)
 103    {
 104  12 if (object1 instanceof Row)
 105    {
 106  12 obj1 = ((Row) object1).getCellList().get(this.columnIndex);
 107    }
 108  12 if (object2 instanceof Row)
 109    {
 110  12 obj2 = ((Row) object2).getCellList().get(this.columnIndex);
 111    }
 112   
 113  12 return checkNullsAndCompare(obj1, obj2);
 114    }
 115   
 116  144 if (object1 instanceof Row)
 117    {
 118  144 obj1 = ((Row) object1).getObject();
 119    }
 120  144 if (object2 instanceof Row)
 121    {
 122  144 obj2 = ((Row) object2).getObject();
 123    }
 124   
 125  144 try
 126    {
 127  144 Object result1;
 128  144 Object result2;
 129   
 130    // If they have supplied a decorator, then make sure and use it for the sorting as well
 131  144 if (this.decorator != null && this.decorator.hasGetterFor(this.property))
 132    {
 133    // set the row before sending to the decorator
 134  0 this.decorator.initRow(obj1, 0, 0);
 135   
 136  0 result1 = LookupUtil.getBeanProperty(this.decorator, this.property);
 137   
 138    // set the row before sending to the decorator
 139  0 this.decorator.initRow(obj2, 0, 0);
 140   
 141  0 result2 = LookupUtil.getBeanProperty(this.decorator, this.property);
 142    }
 143    else
 144    {
 145  144 result1 = LookupUtil.getBeanProperty(obj1, this.property);
 146  144 result2 = LookupUtil.getBeanProperty(obj2, this.property);
 147    }
 148   
 149  144 return checkNullsAndCompare(result1, result2);
 150    }
 151    catch (ObjectLookupException e)
 152    {
 153  0 throw new RuntimeLookupException(getClass(), this.property, e);
 154    }
 155    }
 156   
 157    /**
 158    * Compares two given objects, and handles the case where nulls are present.
 159    * @param object1 first object to compare
 160    * @param object2 second object to compare
 161    * @return int result
 162    */
 163  156 private int checkNullsAndCompare(Object object1, Object object2)
 164    {
 165  156 int returnValue;
 166  156 if (object1 == null && object2 != null)
 167    {
 168  0 returnValue = -1;
 169    }
 170  156 else if (object1 != null && object2 == null)
 171    {
 172  0 returnValue = 1;
 173    }
 174  156 else if (object1 == null && object2 == null)
 175    {
 176    // both null
 177  0 returnValue = 0;
 178    }
 179    else
 180    {
 181  156 returnValue = comparator.compare(object1, object2);
 182    }
 183  156 int ascendingInt = this.ascending ? 1 : -1;
 184  156 return ascendingInt * returnValue;
 185    }
 186   
 187    /**
 188    * Is this Comparator the same as another one?
 189    * @param object Object
 190    * @return boolean
 191    * @see java.util.Comparator#equals(Object)
 192    */
 193  0 public final boolean equals(Object object)
 194    {
 195  0 if (object instanceof RowSorter)
 196    {
 197  0 return new EqualsBuilder().append(this.property, ((RowSorter) object).property).append(
 198    this.columnIndex,
 199    ((RowSorter) object).columnIndex).isEquals();
 200    }
 201   
 202  0 return false;
 203    }
 204   
 205    /**
 206    * @see java.lang.Object#hashCode()
 207    */
 208  0 public final int hashCode()
 209    {
 210  0 return new HashCodeBuilder(31, 33).append(this.property).append(this.columnIndex).toHashCode();
 211    }
 212   
 213    }