View Javadoc
1   package net.avcompris.commons3.dao.impl;
2   
3   import static com.google.common.base.Preconditions.checkNotNull;
4   
5   import java.util.Comparator;
6   
7   import javax.annotation.Nullable;
8   
9   import org.joda.time.DateTime;
10  
11  import net.avcompris.commons3.dao.EntityDto;
12  
13  public abstract class ComparatorUtils {
14  
15  	public static Comparator<EntityDto> getComparator_CREATED_AT() {
16  
17  		return new Comparator<EntityDto>() {
18  
19  			@Override
20  			public int compare(final EntityDto e1, final EntityDto e2) {
21  				return e1.getCreatedAt().compareTo(e2.getCreatedAt());
22  			}
23  		};
24  	}
25  
26  	public static Comparator<EntityDto> getComparator_CREATED_AT_DESC() {
27  
28  		return new Comparator<EntityDto>() {
29  
30  			@Override
31  			public int compare(final EntityDto e1, final EntityDto e2) {
32  				return e2.getCreatedAt().compareTo(e1.getCreatedAt());
33  			}
34  		};
35  	}
36  
37  	public static Comparator<EntityDto> getComparator_UPDATED_AT() {
38  
39  		return new Comparator<EntityDto>() {
40  
41  			@Override
42  			public int compare(final EntityDto e1, final EntityDto e2) {
43  				return e1.getUpdatedAt().compareTo(e2.getUpdatedAt());
44  			}
45  		};
46  	}
47  
48  	public static Comparator<EntityDto> getComparator_UPDATED_AT_DESC() {
49  
50  		return new Comparator<EntityDto>() {
51  
52  			@Override
53  			public int compare(final EntityDto e1, final EntityDto e2) {
54  				return e2.getUpdatedAt().compareTo(e1.getUpdatedAt());
55  			}
56  		};
57  	}
58  
59  	public static Comparator<EntityDto> getComparator_REVISION() {
60  
61  		return new Comparator<EntityDto>() {
62  
63  			@Override
64  			public int compare(final EntityDto e1, final EntityDto e2) {
65  				return Integer.compare(e1.getRevision(), e2.getRevision());
66  			}
67  		};
68  	}
69  
70  	public static Comparator<EntityDto> getComparator_REVISION_DESC() {
71  
72  		return new Comparator<EntityDto>() {
73  
74  			@Override
75  			public int compare(final EntityDto e1, final EntityDto e2) {
76  				return Integer.compare(e2.getRevision(), e1.getRevision());
77  			}
78  		};
79  	}
80  
81  	public static int compare_ASC(final String s1, final String s2) {
82  
83  		checkNotNull(s1, "s1");
84  		checkNotNull(s2, "s2");
85  
86  		return s1.compareToIgnoreCase(s2);
87  	}
88  
89  	public static int compare_DESC(final String s1, final String s2) {
90  
91  		checkNotNull(s1, "s1");
92  		checkNotNull(s2, "s2");
93  
94  		return s2.compareToIgnoreCase(s1);
95  	}
96  
97  	public static int compare_ASC(final int n1, final int n2) {
98  
99  		return Integer.compare(n1, n2);
100 	}
101 
102 	public static int compare_DESC(final int n1, final int n2) {
103 
104 		return Integer.compare(n2, n1);
105 	}
106 
107 	public static int compare_ASC(final long n1, final long n2) {
108 
109 		return Long.compare(n1, n2);
110 	}
111 
112 	public static int compare_DESC(final long n1, final long n2) {
113 
114 		return Long.compare(n2, n1);
115 	}
116 
117 	public static int compare_ASC(final boolean b1, final boolean b2) {
118 
119 		return Boolean.compare(b1, b2);
120 	}
121 
122 	public static int compare_DESC(final boolean b1, final boolean b2) {
123 
124 		return Boolean.compare(b2, b1);
125 	}
126 
127 	public static int compare_ASC(final DateTime dateTime1, final DateTime dateTime2) {
128 
129 		checkNotNull(dateTime1, "dateTime1");
130 		checkNotNull(dateTime2, "dateTime2");
131 
132 		return dateTime1.compareTo(dateTime2);
133 	}
134 
135 	public static int compare_DESC(final DateTime dateTime1, final DateTime dateTime2) {
136 
137 		checkNotNull(dateTime1, "dateTime1");
138 		checkNotNull(dateTime2, "dateTime2");
139 
140 		return dateTime2.compareTo(dateTime1);
141 	}
142 
143 	public static int compareNullable_ASC(@Nullable final String s1, @Nullable final String s2) {
144 
145 		return s1 == null && s2 == null //
146 				? 0 //
147 				: s1 == null //
148 						? 1 //
149 						: s2 == null //
150 								? -1 //
151 								: s1.compareTo(s2);
152 	}
153 
154 	public static int compareNullable_DESC(@Nullable final String s1, @Nullable final String s2) {
155 
156 		return s1 == null && s2 == null //
157 				? 0 //
158 				: s2 == null //
159 						? 1 //
160 						: s1 == null //
161 								? -1 //
162 								: s2.compareTo(s1);
163 	}
164 
165 	public static int compareNullable_ASC(@Nullable final Integer n1, @Nullable final Integer n2) {
166 
167 		return n1 == null && n2 == null //
168 				? 0 //
169 				: n1 == null //
170 						? 1 //
171 						: n2 == null //
172 								? -1 //
173 								: n1.compareTo(n2);
174 	}
175 
176 	public static int compareNullable_DESC(@Nullable final Integer n1, @Nullable final Integer n2) {
177 
178 		return n1 == null && n2 == null //
179 				? 0 //
180 				: n2 == null //
181 						? 1 //
182 						: n1 == null //
183 								? -1 //
184 								: n2.compareTo(n1);
185 	}
186 
187 	public static int compareNullable_ASC(@Nullable final DateTime dateTime1, @Nullable final DateTime dateTime2) {
188 
189 		return dateTime1 == null && dateTime2 == null //
190 				? 0 //
191 				: dateTime1 == null //
192 						? 1 //
193 						: dateTime2 == null //
194 								? -1 //
195 								: dateTime1.compareTo(dateTime2);
196 	}
197 
198 	public static int compareNullable_DESC(@Nullable final DateTime dateTime1, @Nullable final DateTime dateTime2) {
199 
200 		return dateTime1 == null && dateTime2 == null //
201 				? 0 //
202 				: dateTime2 == null //
203 						? 1 //
204 						: dateTime1 == null //
205 								? -1 //
206 								: dateTime2.compareTo(dateTime1);
207 	}
208 }