1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.tobago.util;
21
22 import org.apache.commons.beanutils.PropertyUtils;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import java.util.Comparator;
27 import java.io.Serializable;
28
29 public class BeanComparator extends ComparatorBase implements Serializable {
30
31 private static final long serialVersionUID = -7450094725566090886L;
32
33 private static final Logger LOG = LoggerFactory.getLogger(BeanComparator.class);
34
35 private String property;
36
37 public BeanComparator(String property) {
38 this.property = property;
39 }
40
41 public BeanComparator(String property, boolean reverse) {
42 super(reverse);
43 this.property = property;
44 }
45
46 public BeanComparator(String property, Comparator comparator) {
47 super(comparator);
48 this.property = property;
49 }
50
51 public BeanComparator(String property, Comparator comparator, boolean reverse) {
52 super(reverse, comparator);
53 this.property = property;
54 }
55
56
57
58
59
60 public boolean equals(Object param1) {
61 if (this == param1) {
62 return true;
63 }
64 if (param1 instanceof BeanComparator) {
65 return ((BeanComparator) param1).getProperty().equals(property)
66 && super.equals(param1);
67 }
68 return false;
69 }
70
71 public int hashCode() {
72 int result;
73 result = (property != null ? property.hashCode() : 0);
74 result = 29 * result + super.hashCode();
75 return result;
76 }
77
78
79
80
81
82
83
84
85 public int compare(Object param1, Object param2) {
86 Object obj1;
87 Object obj2;
88 try {
89 obj1 = PropertyUtils.getProperty(param1, property);
90 obj2 = PropertyUtils.getProperty(param2, property);
91
92 } catch (Exception e) {
93 LOG.error(e.getMessage(), e);
94 return 0;
95 }
96
97 return internalCompare(obj1, obj2);
98 }
99
100 public String getProperty() {
101 return this.property;
102 }
103 }