1 package fr.improve.struts.taglib.layout.sort; 2 3 import java.util.Comparator ; 4 5 import fr.improve.struts.taglib.layout.util.LayoutUtils; 6 13 public class BeanComparator implements Comparator { 14 17 protected String property; 18 19 22 protected SortRules stringSortingRules; 23 24 25 30 public BeanComparator(String in_property, SortRules in_rules) { 31 property = in_property; 32 stringSortingRules = in_rules; 33 } 34 64 public int compare(java.lang.Object o1, java.lang.Object o2) { 65 if (o1==null || o2==null) { 67 return compareNull(o1, o2); 68 } 69 70 try { 71 Object lc_object1 = LayoutUtils.getProperty(o1, property); 73 Object lc_object2 = LayoutUtils.getProperty(o2, property); 74 75 if (lc_object1==null || lc_object2==null) { 77 return compareNull(lc_object1, lc_object2); 78 } 79 80 if (lc_object1 instanceof String && lc_object2 instanceof String ) { 82 return compareString((String )lc_object1, (String )lc_object2); 83 } 84 85 if (lc_object1 instanceof Comparable && lc_object2 instanceof Comparable ) { 87 return compareComparable((Comparable )lc_object1, (Comparable )lc_object2); 88 } 89 90 if (lc_object1 instanceof Boolean && lc_object2 instanceof Boolean ) { 92 return compareBoolean((Boolean )lc_object1, (Boolean )lc_object2); 93 } 94 95 throw new ClassCastException ("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 e) { 98 throw new ClassCastException (); 99 } 100 101 } 102 103 106 public int compareBoolean(Boolean in_bool1, Boolean 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 return 0; 124 } 125 126 129 public int compareString(String lc_value1, String 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 140 public int compareComparable(Comparable lc_value1, Comparable lc_value2) { 141 return lc_value1.compareTo(lc_value2); 142 } 143 144 147 public int compareNull(Object o1, Object 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 return 0; 159 } 160 161 } 162 | Popular Tags |