1 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 29 public class RowSorter implements Comparator 30 { 31 32 35 private String property; 36 37 40 private TableDecorator decorator; 41 42 45 private boolean ascending; 46 47 50 private int columnIndex; 51 52 55 private Comparator comparator; 56 57 66 public RowSorter( 67 int sortedColumnIndex, 68 String beanProperty, 69 TableDecorator tableDecorator, 70 boolean ascendingOrder, 71 Comparator compar) 72 { 73 this.columnIndex = sortedColumnIndex; 74 this.property = beanProperty; 75 this.decorator = tableDecorator; 76 this.ascending = ascendingOrder; 77 this.comparator = compar; 78 } 79 80 90 public final int compare(Object object1, Object object2) 91 { 92 93 Object obj1 = null; 94 Object obj2 = null; 95 96 if (this.property == null) 98 { 99 if (object1 instanceof Row) 100 { 101 obj1 = ((Row) object1).getCellList().get(this.columnIndex); 102 } 103 if (object2 instanceof Row) 104 { 105 obj2 = ((Row) object2).getCellList().get(this.columnIndex); 106 } 107 108 return checkNullsAndCompare(obj1, obj2); 109 } 110 111 if (object1 instanceof Row) 112 { 113 obj1 = ((Row) object1).getObject(); 114 } 115 if (object2 instanceof Row) 116 { 117 obj2 = ((Row) object2).getObject(); 118 } 119 120 try 121 { 122 Object result1; 123 Object result2; 124 125 if (this.decorator != null && this.decorator.hasGetterFor(this.property)) 127 { 128 this.decorator.initRow(obj1, 0, 0); 130 131 result1 = LookupUtil.getBeanProperty(this.decorator, this.property); 132 133 this.decorator.initRow(obj2, 0, 0); 135 136 result2 = LookupUtil.getBeanProperty(this.decorator, this.property); 137 } 138 else 139 { 140 result1 = LookupUtil.getBeanProperty(obj1, this.property); 141 result2 = LookupUtil.getBeanProperty(obj2, this.property); 142 } 143 144 return checkNullsAndCompare(result1, result2); 145 } 146 catch (ObjectLookupException e) 147 { 148 throw new RuntimeLookupException(getClass(), this.property, e); 149 } 150 } 151 152 158 private int checkNullsAndCompare(Object object1, Object object2) 159 { 160 int returnValue; 161 if (object1 == null && object2 != null) 162 { 163 returnValue = -1; 164 } 165 else if (object1 != null && object2 == null) 166 { 167 returnValue = 1; 168 } 169 else if (object1 == null && object2 == null) 170 { 171 returnValue = 0; 173 } 174 else 175 { 176 returnValue = comparator.compare(object1, object2); 177 } 178 int ascendingInt = this.ascending ? 1 : -1; 179 return ascendingInt * returnValue; 180 } 181 182 188 public final boolean equals(Object object) 189 { 190 if (object instanceof RowSorter) 191 { 192 return new EqualsBuilder().append(this.property, ((RowSorter) object).property).append( 193 this.columnIndex, 194 ((RowSorter) object).columnIndex).isEquals(); 195 } 196 197 return false; 198 } 199 200 203 public final int hashCode() 204 { 205 return new HashCodeBuilder(31, 33).append(this.property).append(this.columnIndex).toHashCode(); 206 } 207 208 } 209 | Popular Tags |