1 package net.avcompris.examples.users3.dao.impl;
2
3 import static com.google.common.base.Preconditions.checkArgument;
4 import static com.google.common.base.Preconditions.checkNotNull;
5 import static net.avcompris.commons3.dao.impl.ComparatorUtils.compareNullable_ASC;
6 import static net.avcompris.commons3.dao.impl.ComparatorUtils.compareNullable_DESC;
7 import static net.avcompris.commons3.dao.impl.ComparatorUtils.compare_ASC;
8 import static net.avcompris.commons3.dao.impl.ComparatorUtils.compare_DESC;
9 import static net.avcompris.commons3.dao.impl.ComparatorUtils.getComparator_CREATED_AT;
10 import static net.avcompris.commons3.dao.impl.ComparatorUtils.getComparator_CREATED_AT_DESC;
11 import static net.avcompris.commons3.dao.impl.ComparatorUtils.getComparator_REVISION;
12 import static net.avcompris.commons3.dao.impl.ComparatorUtils.getComparator_REVISION_DESC;
13 import static net.avcompris.commons3.dao.impl.ComparatorUtils.getComparator_UPDATED_AT;
14 import static net.avcompris.commons3.dao.impl.ComparatorUtils.getComparator_UPDATED_AT_DESC;
15
16 import java.util.Comparator;
17
18 import org.apache.commons.lang3.NotImplementedException;
19 import org.joda.time.DateTime;
20
21 import net.avcompris.examples.users3.dao.UserDto;
22 import net.avcompris.examples.users3.dao.UserSessionDto;
23 import net.avcompris.examples.users3.dao.UserSessionsDtoQuery;
24 import net.avcompris.examples.users3.dao.UsersDtoQuery;
25
26 abstract class Comparators {
27
28 public static Comparator<? super UserDto> get(final UsersDtoQuery.SortBy sortBy) {
29
30 checkNotNull(sortBy, "sortBy");
31
32 switch (sortBy) {
33
34 case SORT_BY_USERNAME:
35
36 return new Comparator<UserDto>() {
37
38 @Override
39 public int compare(final UserDtoes/users3/dao/UserDto.html#UserDto">UserDto u1, final UserDto u2) {
40 return compare_ASC(u1.getUsername(), u2.getUsername());
41 }
42 };
43
44 case SORT_BY_USERNAME_DESC:
45
46 return new Comparator<UserDto>() {
47
48 @Override
49 public int compare(final UserDtoes/users3/dao/UserDto.html#UserDto">UserDto u1, final UserDto u2) {
50 return compare_DESC(u1.getUsername(), u2.getUsername());
51 }
52 };
53
54 case SORT_BY_ROLENAME:
55
56 return new Comparator<UserDto>() {
57
58 @Override
59 public int compare(final UserDtoes/users3/dao/UserDto.html#UserDto">UserDto u1, final UserDto u2) {
60 return compare_ASC(u1.getRolename(), u2.getRolename());
61 }
62 };
63
64 case SORT_BY_ROLENAME_DESC:
65
66 return new Comparator<UserDto>() {
67
68 @Override
69 public int compare(final UserDtoes/users3/dao/UserDto.html#UserDto">UserDto u1, final UserDto u2) {
70 return compare_DESC(u1.getRolename(), u2.getRolename());
71 }
72 };
73
74 case SORT_BY_ENABLED:
75
76 return new Comparator<UserDto>() {
77
78 @Override
79 public int compare(final UserDtoes/users3/dao/UserDto.html#UserDto">UserDto u1, final UserDto u2) {
80 return compare_ASC(u1.isEnabled(), u2.isEnabled());
81 }
82 };
83
84 case SORT_BY_ENABLED_DESC:
85
86 return new Comparator<UserDto>() {
87
88 @Override
89 public int compare(final UserDtoes/users3/dao/UserDto.html#UserDto">UserDto u1, final UserDto u2) {
90 return compare_DESC(u1.isEnabled(), u2.isEnabled());
91 }
92 };
93
94 case SORT_BY_CREATED_AT:
95
96 return getComparator_CREATED_AT();
97
98 case SORT_BY_CREATED_AT_DESC:
99
100 return getComparator_CREATED_AT_DESC();
101
102 case SORT_BY_UPDATED_AT:
103
104 return getComparator_UPDATED_AT();
105
106 case SORT_BY_UPDATED_AT_DESC:
107
108 return getComparator_UPDATED_AT_DESC();
109
110 case SORT_BY_LAST_ACTIVE_AT:
111
112 return new Comparator<UserDto>() {
113
114 @Override
115 public int compare(final UserDtoes/users3/dao/UserDto.html#UserDto">UserDto u1, final UserDto u2) {
116 return compareNullable_ASC(u1.getLastActiveAt(), u2.getLastActiveAt());
117 }
118 };
119
120 case SORT_BY_LAST_ACTIVE_AT_DESC:
121
122 return new Comparator<UserDto>() {
123
124 @Override
125 public int compare(final UserDtoes/users3/dao/UserDto.html#UserDto">UserDto u1, final UserDto u2) {
126 return compareNullable_DESC(u1.getLastActiveAt(), u2.getLastActiveAt());
127 }
128 };
129
130 case SORT_BY_REVISION:
131
132 return getComparator_REVISION();
133
134 case SORT_BY_REVISION_DESC:
135
136 return getComparator_REVISION_DESC();
137
138 default:
139
140 throw new NotImplementedException("sortBy: " + sortBy);
141 }
142 }
143
144 public static Comparator<? super UserDto> get(final UsersDtoQuery.SortBy... sortBys) {
145
146 checkArgument(sortBys != null && sortBys.length != 0, "sortBys should not be empty");
147
148 return new Comparator<UserDto>() {
149
150 @Override
151 public int compare(final UserDtoes/users3/dao/UserDto.html#UserDto">UserDto u1, final UserDto u2) {
152
153 for (final UsersDtoQuery.SortBy sortBy : sortBys) {
154
155 final Comparator<? super UserDto> comparator = get(sortBy);
156
157 final int compare = comparator.compare(u1, u2);
158
159 if (compare != 0) {
160
161 return compare;
162 }
163 }
164
165 return 0;
166 }
167
168 };
169 }
170
171 public static Comparator<? super UserSessionDto> get(final UserSessionsDtoQuery.SortBy sortBy) {
172
173 checkNotNull(sortBy, "sortBy");
174
175 switch (sortBy) {
176
177 case SORT_BY_USERNAME:
178
179 return new Comparator<UserSessionDto>() {
180
181 @Override
182 public int compare(final UserSessionDtos3/dao/UserSessionDto.html#UserSessionDto">UserSessionDto s1, final UserSessionDto s2) {
183 return compare_ASC(s1.getUsername(), s2.getUsername());
184 }
185 };
186
187 case SORT_BY_USERNAME_DESC:
188
189 return new Comparator<UserSessionDto>() {
190
191 @Override
192 public int compare(final UserSessionDtos3/dao/UserSessionDto.html#UserSessionDto">UserSessionDto s1, final UserSessionDto s2) {
193 return compare_DESC(s1.getUsername(), s2.getUsername());
194 }
195 };
196
197 case SORT_BY_USER_SESSION_ID:
198
199 return new Comparator<UserSessionDto>() {
200
201 @Override
202 public int compare(final UserSessionDtos3/dao/UserSessionDto.html#UserSessionDto">UserSessionDto s1, final UserSessionDto s2) {
203 return compare_ASC(s1.getUserSessionId(), s2.getUserSessionId());
204 }
205 };
206
207 case SORT_BY_USER_SESSION_ID_DESC:
208
209 return new Comparator<UserSessionDto>() {
210
211 @Override
212 public int compare(final UserSessionDtos3/dao/UserSessionDto.html#UserSessionDto">UserSessionDto s1, final UserSessionDto s2) {
213 return compare_DESC(s1.getUserSessionId(), s2.getUserSessionId());
214 }
215 };
216
217 case SORT_BY_CREATED_AT:
218
219 return new Comparator<UserSessionDto>() {
220
221 @Override
222 public int compare(final UserSessionDtos3/dao/UserSessionDto.html#UserSessionDto">UserSessionDto s1, final UserSessionDto s2) {
223 return s1.getCreatedAt().compareTo(s2.getCreatedAt());
224 }
225 };
226
227 case SORT_BY_CREATED_AT_DESC:
228
229 return new Comparator<UserSessionDto>() {
230
231 @Override
232 public int compare(final UserSessionDtos3/dao/UserSessionDto.html#UserSessionDto">UserSessionDto s1, final UserSessionDto s2) {
233 return s2.getCreatedAt().compareTo(s1.getCreatedAt());
234 }
235 };
236
237 case SORT_BY_UPDATED_AT:
238
239 return new Comparator<UserSessionDto>() {
240
241 @Override
242 public int compare(final UserSessionDtos3/dao/UserSessionDto.html#UserSessionDto">UserSessionDto s1, final UserSessionDto s2) {
243 return s1.getUpdatedAt().compareTo(s2.getUpdatedAt());
244 }
245 };
246
247 case SORT_BY_UPDATED_AT_DESC:
248
249 return new Comparator<UserSessionDto>() {
250
251 @Override
252 public int compare(final UserSessionDtos3/dao/UserSessionDto.html#UserSessionDto">UserSessionDto s1, final UserSessionDto s2) {
253 return s2.getUpdatedAt().compareTo(s1.getUpdatedAt());
254 }
255 };
256
257 case SORT_BY_EXPIRES_AT:
258
259 return new Comparator<UserSessionDto>() {
260
261 @Override
262 public int compare(final UserSessionDtos3/dao/UserSessionDto.html#UserSessionDto">UserSessionDto s1, final UserSessionDto s2) {
263 return s1.getExpiresAt().compareTo(s2.getExpiresAt());
264 }
265 };
266
267 case SORT_BY_EXPIRES_AT_DESC:
268
269 return new Comparator<UserSessionDto>() {
270
271 @Override
272 public int compare(final UserSessionDtos3/dao/UserSessionDto.html#UserSessionDto">UserSessionDto s1, final UserSessionDto s2) {
273 return s2.getExpiresAt().compareTo(s1.getExpiresAt());
274 }
275 };
276
277 case SORT_BY_EXPIRED_AT:
278
279 return new Comparator<UserSessionDto>() {
280
281 @Override
282 public int compare(final UserSessionDtos3/dao/UserSessionDto.html#UserSessionDto">UserSessionDto s1, final UserSessionDto s2) {
283 final DateTime expiredAt1 = s1.getExpiredAt();
284 final DateTime expiredAt2 = s2.getExpiredAt();
285
286 return expiredAt1 == null && expiredAt2 == null
287 ? 0
288 : expiredAt1 == null
289 ? 1
290 : expiredAt2 == null
291 ? -1
292 : expiredAt1.compareTo(expiredAt1);
293 }
294 };
295
296 case SORT_BY_EXPIRED_AT_DESC:
297
298 return new Comparator<UserSessionDto>() {
299
300 @Override
301 public int compare(final UserSessionDtos3/dao/UserSessionDto.html#UserSessionDto">UserSessionDto s1, final UserSessionDto s2) {
302 final DateTime expiredAt2 = s2.getExpiredAt();
303 final DateTime expiredAt1 = s1.getExpiredAt();
304
305 return expiredAt1 == null && expiredAt2 == null
306 ? 0
307 : expiredAt2 == null
308 ? 1
309 : expiredAt1 == null
310 ? -1
311 : expiredAt2.compareTo(expiredAt1);
312 }
313 };
314
315 default:
316
317 throw new NotImplementedException("sortBy: " + sortBy);
318 }
319 }
320
321 public static Comparator<? super UserSessionDto> get(final UserSessionsDtoQuery.SortBy... sortBys) {
322
323 checkArgument(sortBys != null && sortBys.length != 0,
324 "sortBys should not be empty");
325
326 return new Comparator<UserSessionDto>() {
327
328 @Override
329 public int compare(final UserSessionDtos3/dao/UserSessionDto.html#UserSessionDto">UserSessionDto s1, final UserSessionDto s2) {
330
331 for (final UserSessionsDtoQuery.SortBy sortBy : sortBys) {
332
333 final Comparator<? super UserSessionDto> comparator = get(sortBy);
334
335 final int compare = comparator.compare(s1, s2);
336
337 if (compare != 0) {
338
339 return compare;
340 }
341 }
342
343 return 0;
344 }
345
346 };
347 }
348 }