KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > displaytag > test > NumberComparator


1 package org.displaytag.test;
2
3 import java.util.Comparator JavaDoc;
4
5 import org.apache.commons.beanutils.ConvertUtils;
6
7
8 /**
9  * Sorts 2 numbers, converted from objects using beanutils Converters.
10  * @author rapruitt
11  * @author Fabrizio Giustina
12  * @version $Revision$ ($Author$)
13  */

14 public class NumberComparator implements Comparator JavaDoc
15 {
16
17     /**
18      * @see Comparator#compare(Object, Object)
19      */

20     public int compare(Object JavaDoc obj1, Object JavaDoc obj2)
21     {
22         double dbl1 = 0;
23         if (obj1 instanceof Number JavaDoc)
24         {
25             dbl1 = ((Number JavaDoc) obj1).doubleValue();
26         }
27         else if (obj1 != null)
28         {
29             dbl1 = ((Number JavaDoc) ConvertUtils.convert(obj1.toString(), Number JavaDoc.class)).doubleValue();
30         }
31
32         double dbl2 = 0;
33         if (obj2 instanceof Number JavaDoc)
34         {
35             dbl2 = ((Number JavaDoc) obj2).doubleValue();
36         }
37         else if (obj1 != null)
38         {
39             dbl2 = ((Number JavaDoc) ConvertUtils.convert(obj2.toString(), Number JavaDoc.class)).doubleValue();
40         }
41
42         return new Double JavaDoc(dbl1).compareTo(new Double JavaDoc(dbl2));
43     }
44 }
45
Popular Tags