1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.orangesignal.csv.filters;
18
19 import java.io.IOException;
20 import java.lang.reflect.Field;
21 import java.util.Collection;
22 import java.util.Comparator;
23 import java.util.Map;
24 import java.util.regex.Pattern;
25
26 import com.orangesignal.csv.bean.FieldUtils;
27
28
29
30
31
32
33
34 abstract class BeanExpressionUtils {
35
36
37
38
39 protected BeanExpressionUtils() {}
40
41
42
43
44
45
46
47
48
49
50
51 public static boolean isNull(final Object bean, final String name) throws IOException {
52 return getFieldValue(bean, name) == null;
53 }
54
55
56
57
58
59
60
61
62
63 public static boolean isNotNull(final Object bean, final String name) throws IOException {
64 return !isNull(bean, name);
65 }
66
67
68
69
70
71
72
73
74
75 public static boolean isEmpty(final Object bean, final String name) throws IOException {
76 final Field field = FieldUtils.getField(bean.getClass(), name);
77 final Object value = FieldUtils.getFieldValue(bean, field);
78 if (value == null) {
79 return true;
80 }
81 if (field.getType().isArray()) {
82 return ((Object[]) value).length == 0;
83 }
84 if (value instanceof String) {
85 return ((String) value).isEmpty();
86 } else if (value instanceof Collection) {
87 return ((Collection<?>) value).isEmpty();
88 } else if (value instanceof Map) {
89 return ((Map<?, ?>) value).isEmpty();
90 }
91 return false;
92 }
93
94
95
96
97
98
99
100
101
102 public static boolean isNotEmpty(final Object bean, final String name) throws IOException {
103 return !isEmpty(bean, name);
104 }
105
106
107
108
109
110
111
112
113
114
115
116
117
118 public static boolean eq(final Object bean, final String name, final Object criteria, final boolean ignoreCase) throws IOException {
119 final Object value = getFieldValue(bean, name);
120 if (criteria == null) {
121 throw new IllegalArgumentException("Criteria must not be null");
122 }
123 if (ignoreCase) {
124 return value != null && ((String) criteria).equalsIgnoreCase((String) value);
125 }
126 return criteria.equals(value);
127 }
128
129
130
131
132
133
134
135
136
137
138
139 public static boolean ne(final Object bean, final String name, final Object criteria, final boolean ignoreCase) throws IOException {
140 return !eq(bean, name, criteria, ignoreCase);
141 }
142
143
144
145
146
147
148
149
150
151
152
153 public static boolean in(final Object bean, final String name, final Object[] criterias, final boolean ignoreCase) throws IOException {
154 if (criterias == null) {
155 throw new IllegalArgumentException("Criterias must not be null");
156 }
157 for (final Object criteria : criterias) {
158 if (eq(bean, name, criteria, ignoreCase)) {
159 return true;
160 }
161 }
162 return false;
163 }
164
165
166
167
168
169
170
171
172
173
174
175 public static boolean notIn(final Object bean, final String name, final Object[] criterias, final boolean ignoreCase) throws IOException {
176 return !in(bean, name, criterias, ignoreCase);
177 }
178
179
180
181
182
183
184
185
186
187
188
189
190 public static boolean regex(final Object bean, final String name, final Pattern pattern) throws IOException {
191 final Object value = getFieldValue(bean, name);
192 return value != null && pattern.matcher((CharSequence) value).matches();
193 }
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208 @SuppressWarnings({ "unchecked", "rawtypes" })
209 public static boolean gt(final Object bean, final String name, final Object criteria, final Comparator comparator) throws IOException {
210 final Object value = getFieldValue(bean, name);
211 if (comparator != null) {
212 return value != null && criteria != null && comparator.compare(value, criteria) > 0;
213 }
214 return value != null && criteria != null && ((Comparable) value).compareTo(criteria) > 0;
215 }
216
217
218
219
220
221
222
223
224
225
226
227
228 @SuppressWarnings({ "unchecked", "rawtypes" })
229 public static boolean lt(final Object bean, final String name, final Object criteria, final Comparator comparator) throws IOException {
230 final Object value = getFieldValue(bean, name);
231 if (comparator != null) {
232 return value != null && criteria != null && comparator.compare(value, criteria) < 0;
233 }
234 return value != null && criteria != null && ((Comparable) value).compareTo(criteria) < 0;
235 }
236
237
238
239
240
241
242
243
244
245
246
247
248 @SuppressWarnings({ "unchecked", "rawtypes" })
249 public static boolean ge(final Object bean, final String name, final Object criteria, final Comparator comparator) throws IOException {
250 final Object value = getFieldValue(bean, name);
251 if (comparator != null) {
252 return value != null && criteria != null && comparator.compare(value, criteria) >= 0;
253 }
254 return value != null && criteria != null && ((Comparable) value).compareTo(criteria) >= 0;
255 }
256
257
258
259
260
261
262
263
264
265
266
267
268 @SuppressWarnings({ "unchecked", "rawtypes" })
269 public static boolean le(final Object bean, final String name, final Object criteria, final Comparator comparator) throws IOException {
270 final Object value = getFieldValue(bean, name);
271 if (comparator != null) {
272 return value != null && criteria != null && comparator.compare(value, criteria) <= 0;
273 }
274 return value != null && criteria != null && ((Comparable) value).compareTo(criteria) <= 0;
275 }
276
277
278
279
280
281
282
283
284
285
286
287
288
289 @SuppressWarnings("rawtypes")
290 public static boolean between(final Object bean, final String name, final Object low, final Object high, final Comparator comparator) throws IOException {
291 return ge(bean, name, low, comparator) && le(bean, name, high, comparator);
292 }
293
294
295
296
297
298
299
300
301
302
303
304
305 private static Object getFieldValue(final Object bean, final String name) throws IOException {
306 return FieldUtils.getFieldValue(bean, FieldUtils.getField(bean.getClass(), name));
307 }
308
309 }