1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.directory.modules.multiview.business.record.column;
35
36 import java.io.Serializable;
37 import java.util.Comparator;
38 import java.util.Date;
39
40 import org.apache.commons.lang3.math.NumberUtils;
41
42
43
44
45 public class RecordColumnCellComparator implements Comparator<RecordColumnCell>, Serializable
46 {
47
48 private static final long serialVersionUID = -7519611978697011989L;
49
50
51 private final String _strSortKey;
52
53
54
55
56
57
58
59 public RecordColumnCellComparator( String strSortKey )
60 {
61 _strSortKey = strSortKey;
62 }
63
64
65
66
67 @Override
68 public int compare( RecordColumnCell recordColumnCellOne, RecordColumnCell recordColumnCellTwo )
69 {
70 int nComparisonResult = NumberUtils.INTEGER_ZERO;
71
72 if ( recordColumnCellOne == null )
73 {
74 if ( recordColumnCellTwo != null )
75 {
76 nComparisonResult = NumberUtils.INTEGER_MINUS_ONE;
77 }
78 }
79 else
80 {
81 if ( recordColumnCellTwo == null )
82 {
83 nComparisonResult = NumberUtils.INTEGER_ONE;
84 }
85 else
86 {
87 Object objectOne = recordColumnCellOne.getRecordColumnCellValueByName( _strSortKey );
88 Object objectTwo = recordColumnCellTwo.getRecordColumnCellValueByName( _strSortKey );
89
90 nComparisonResult = compareRecordColumnCellObject( objectOne, objectTwo );
91 }
92 }
93
94 return nComparisonResult;
95 }
96
97
98
99
100
101
102
103
104
105
106 private int compareRecordColumnCellObject( Object objectOne, Object objectTwo )
107 {
108 int nComparisonResult = NumberUtils.INTEGER_ZERO;
109
110 if ( objectOne == null )
111 {
112 if ( objectTwo != null )
113 {
114 nComparisonResult = NumberUtils.INTEGER_MINUS_ONE;
115 }
116 }
117 else
118 {
119 if ( objectTwo == null )
120 {
121 nComparisonResult = NumberUtils.INTEGER_ONE;
122 }
123 else
124 {
125 nComparisonResult = compareObject( objectOne, objectTwo );
126 }
127 }
128
129 return nComparisonResult;
130 }
131
132
133
134
135
136
137
138
139
140
141
142 private int compareObject( Object objectOne, Object objectTwo )
143 {
144 int nComparisonResult = NumberUtils.INTEGER_ZERO;
145
146 if ( objectOne instanceof Date && objectTwo instanceof Date )
147 {
148 Date dateOne = (Date) objectOne;
149 Date dateTwo = (Date) objectTwo;
150
151 nComparisonResult = dateOne.compareTo( dateTwo );
152 }
153 else
154 {
155 String strObjectOneRepresentation = String.valueOf( objectOne );
156 String strObjectTwoRepresentation = String.valueOf( objectTwo );
157
158 nComparisonResult = strObjectOneRepresentation.compareTo( strObjectTwoRepresentation );
159 }
160
161 return nComparisonResult;
162 }
163 }