KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > sort > BeanComparator


1 package fr.improve.struts.taglib.layout.sort;
2
3 import java.util.Comparator JavaDoc;
4
5 import fr.improve.struts.taglib.layout.util.LayoutUtils;
6 /**
7  * Generic bean comparator.
8  *
9  * Creation date: (27/10/2001 15:49:40)
10  *
11  * @author: Jean-Noël Ribette
12  */

13 public class BeanComparator implements Comparator JavaDoc {
14     /**
15      * The property to use to compare the beans.
16      */

17     protected String JavaDoc property;
18     
19     /**
20      * The rules to use to compare String properties.
21      */

22     protected SortRules stringSortingRules;
23     
24     
25     /**
26      * Constructor
27      * @param in_property The property to use to compare objects.
28      * @param in_rules The string sorting rules to use.
29      */

30     public BeanComparator(String JavaDoc in_property, SortRules in_rules) {
31         property = in_property;
32         stringSortingRules = in_rules;
33     }
34     /**
35      * Compares its two arguments for order. Returns a negative integer,
36      * zero, or a positive integer as the first argument is less than, equal
37      * to, or greater than the second.<p>
38      *
39      * The implementor must ensure that <tt>sgn(compare(x, y)) ==
40      * -sgn(compare(y, x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This
41      * implies that <tt>compare(x, y)</tt> must throw an exception if and only
42      * if <tt>compare(y, x)</tt> throws an exception.)<p>
43      *
44      * The implementor must also ensure that the relation is transitive:
45      * <tt>((compare(x, y)&gt;0) &amp;&amp; (compare(y, z)&gt;0))</tt> implies
46      * <tt>compare(x, z)&gt;0</tt>.<p>
47      *
48      * Finally, the implementer must ensure that <tt>compare(x, y)==0</tt>
49      * implies that <tt>sgn(compare(x, z))==sgn(compare(y, z))</tt> for all
50      * <tt>z</tt>.<p>
51      *
52      * It is generally the case, but <i>not</i> strictly required that
53      * <tt>(compare(x, y)==0) == (x.equals(y))</tt>. Generally speaking,
54      * any comparator that violates this condition should clearly indicate
55      * this fact. The recommended language is "Note: this comparator
56      * imposes orderings that are inconsistent with equals."
57      *
58      * @return a negative integer, zero, or a positive integer as the
59      * first argument is less than, equal to, or greater than the
60      * second.
61      * @throws ClassCastException if the arguments' types prevent them from
62      * being compared by this Comparator.
63      */

64 public int compare(java.lang.Object JavaDoc o1, java.lang.Object JavaDoc o2) {
65     // Don't try to compare null values.
66
if (o1==null || o2==null) {
67         return compareNull(o1, o2);
68     }
69
70     try {
71         // Get the values to compare from the beans
72
Object JavaDoc lc_object1 = LayoutUtils.getProperty(o1, property);
73         Object JavaDoc lc_object2 = LayoutUtils.getProperty(o2, property);
74         
75         // Don't try to compare null values.
76
if (lc_object1==null || lc_object2==null) {
77             return compareNull(lc_object1, lc_object2);
78         }
79         
80         // Values are Strings.
81
if (lc_object1 instanceof String JavaDoc && lc_object2 instanceof String JavaDoc) {
82             return compareString((String JavaDoc)lc_object1, (String JavaDoc)lc_object2);
83         }
84         
85         // Values are comparable.
86
if (lc_object1 instanceof Comparable JavaDoc && lc_object2 instanceof Comparable JavaDoc) {
87             return compareComparable((Comparable JavaDoc)lc_object1, (Comparable JavaDoc)lc_object2);
88         }
89         
90         // Value are boolean.
91
if (lc_object1 instanceof Boolean JavaDoc && lc_object2 instanceof Boolean JavaDoc) {
92             return compareBoolean((Boolean JavaDoc)lc_object1, (Boolean JavaDoc)lc_object2);
93         }
94         
95         // Don't nowhow to compare the values
96
throw new ClassCastException JavaDoc("Cannot compare objects of class " + (lc_object1!=null ? lc_object1.getClass().getName() : "null") + " and " + (lc_object2!=null ? lc_object2.getClass().getName() : "null"));
97     } catch (javax.servlet.jsp.JspException JavaDoc e) {
98         throw new ClassCastException JavaDoc();
99     }
100     
101 }
102
103     /**
104      * Compare boolean values.
105      */

106     public int compareBoolean(Boolean JavaDoc in_bool1, Boolean JavaDoc in_bool2) {
107         boolean lc_b1 = in_bool1.booleanValue();
108         boolean lc_b2 = in_bool2.booleanValue();
109         
110         if (lc_b1 && lc_b2 || !lc_b1 && !lc_b2) {
111             return 0;
112         }
113         
114         if (!lc_b1) {
115             return -1;
116         }
117         
118         if (!lc_b2) {
119             return 1;
120         }
121         
122         // Should not happen.
123
return 0;
124     }
125     
126     /**
127      * Compare string values
128      */

129     public int compareString(String JavaDoc lc_value1, String JavaDoc lc_value2) {
130         if (stringSortingRules==null) {
131             return lc_value1.compareTo(lc_value2);
132         } else {
133             return stringSortingRules.getRules().compare(lc_value1, lc_value2);
134         }
135     }
136
137     /**
138      * Compare comparable values.
139      */

140     public int compareComparable(Comparable JavaDoc lc_value1, Comparable JavaDoc lc_value2) {
141         return lc_value1.compareTo(lc_value2);
142     }
143     
144     /**
145      * Compare null values.
146      */

147     public int compareNull(Object JavaDoc o1, Object JavaDoc o2) {
148         if (o1==null && o2==null) {
149             return 0;
150         }
151         if (o1==null) {
152             return -1;
153         }
154         if (o2==null) {
155             return 1;
156         }
157         // Should not happen
158
return 0;
159     }
160
161 }
162
Popular Tags