KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > displaytag > model > RowSorter


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 JavaDoc;
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: 923 $ ($Author: fgiust $)
28  */

29 public class RowSorter implements Comparator JavaDoc
30 {
31
32     /**
33      * name of the property in bean.
34      */

35     private String JavaDoc 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 JavaDoc 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     public RowSorter(
67         int sortedColumnIndex,
68         String JavaDoc beanProperty,
69         TableDecorator tableDecorator,
70         boolean ascendingOrder,
71         Comparator JavaDoc 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     /**
81      * Compares two objects by first fetching a property from each object and then comparing that value. If there are
82      * any errors produced while trying to compare these objects then a RunTimeException will be thrown as any error
83      * found here will most likely be a programming error that needs to be quickly addressed (like trying to compare
84      * objects that are not comparable, or trying to read a property from a bean that is invalid, etc...)
85      * @param object1 Object
86      * @param object2 Object
87      * @return int
88      * @see java.util.Comparator#compare(Object, Object)
89      */

90     public final int compare(Object JavaDoc object1, Object JavaDoc object2)
91     {
92
93         Object JavaDoc obj1 = null;
94         Object JavaDoc obj2 = null;
95
96         // if property is null compare using two static cell objects
97
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 JavaDoc result1;
123             Object JavaDoc result2;
124
125             // If they have supplied a decorator, then make sure and use it for the sorting as well
126
if (this.decorator != null && this.decorator.hasGetterFor(this.property))
127             {
128                 // set the row before sending to the decorator
129
this.decorator.initRow(obj1, 0, 0);
130
131                 result1 = LookupUtil.getBeanProperty(this.decorator, this.property);
132
133                 // set the row before sending to the decorator
134
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     /**
153      * Compares two given objects, and handles the case where nulls are present.
154      * @param object1 first object to compare
155      * @param object2 second object to compare
156      * @return int result
157      */

158     private int checkNullsAndCompare(Object JavaDoc object1, Object JavaDoc 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             // both null
172
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     /**
183      * Is this Comparator the same as another one?
184      * @param object Object
185      * @return boolean
186      * @see java.util.Comparator#equals(Object)
187      */

188     public final boolean equals(Object JavaDoc 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     /**
201      * @see java.lang.Object#hashCode()
202      */

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