001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: ValidityChecks.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.site;
009:
010: import com.uwyn.rife.tools.ArrayUtils;
011: import com.uwyn.rife.tools.StringUtils;
012: import java.lang.reflect.Array;
013: import java.net.MalformedURLException;
014: import java.net.URL;
015: import java.text.Format;
016: import java.text.ParseException;
017: import java.util.Arrays;
018: import java.util.Date;
019: import java.util.regex.Matcher;
020: import java.util.regex.Pattern;
021: import java.util.regex.PatternSyntaxException;
022:
023: public abstract class ValidityChecks {
024: public static boolean checkNotNull(Object value) {
025: return null != value;
026: }
027:
028: public static boolean checkNotEmpty(CharSequence value) {
029: if (null == value) {
030: return true;
031: }
032:
033: String value_string = value.toString();
034: return 0 != StringUtils.trim(value_string).length();
035: }
036:
037: public static boolean checkNotEmpty(Object value) {
038: if (null == value) {
039: return true;
040: }
041:
042: if (value instanceof CharSequence) {
043: return checkNotEmpty((String) value);
044: } else if (value instanceof Character) {
045: return checkNotEmpty((Character) value);
046: } else if (value instanceof Byte) {
047: return checkNotEmpty((Byte) value);
048: } else if (value instanceof Short) {
049: return checkNotEmpty((Short) value);
050: } else if (value instanceof Integer) {
051: return checkNotEmpty((Integer) value);
052: } else if (value instanceof Long) {
053: return checkNotEmpty((Long) value);
054: } else if (value instanceof Float) {
055: return checkNotEmpty((Float) value);
056: } else if (value instanceof Double) {
057: return checkNotEmpty((Double) value);
058: } else if (value.getClass().isArray()) {
059: for (int i = 0; i < Array.getLength(value); i++) {
060: if (!checkNotEmpty(Array.get(value, i))) {
061: return false;
062: }
063: }
064: }
065:
066: return true;
067: }
068:
069: public static boolean checkNotEmpty(Character value) {
070: if (null == value) {
071: return true;
072: }
073:
074: return checkNotEmpty(value.charValue());
075: }
076:
077: public static boolean checkNotEmpty(char value) {
078: return (char) 0 != value;
079: }
080:
081: public static boolean checkNotEmpty(Byte value) {
082: if (null == value) {
083: return true;
084: }
085:
086: return checkNotEmpty(value.byteValue());
087: }
088:
089: public static boolean checkNotEmpty(byte value) {
090: return (byte) 0 != value;
091: }
092:
093: public static boolean checkNotEmpty(Short value) {
094: if (null == value) {
095: return true;
096: }
097:
098: return checkNotEmpty(value.shortValue());
099: }
100:
101: public static boolean checkNotEmpty(short value) {
102: return (short) 0 != value;
103: }
104:
105: public static boolean checkNotEmpty(Integer value) {
106: if (null == value) {
107: return true;
108: }
109:
110: return checkNotEmpty(value.intValue());
111: }
112:
113: public static boolean checkNotEmpty(int value) {
114: return 0 != value;
115: }
116:
117: public static boolean checkNotEmpty(Long value) {
118: if (null == value) {
119: return true;
120: }
121:
122: return checkNotEmpty(value.longValue());
123: }
124:
125: public static boolean checkNotEmpty(long value) {
126: return 0L != value;
127: }
128:
129: public static boolean checkNotEmpty(Float value) {
130: if (null == value) {
131: return true;
132: }
133:
134: return checkNotEmpty(value.floatValue());
135: }
136:
137: public static boolean checkNotEmpty(float value) {
138: return 0.0f != value;
139: }
140:
141: public static boolean checkNotEmpty(Double value) {
142: if (null == value) {
143: return true;
144: }
145:
146: return checkNotEmpty(value.doubleValue());
147: }
148:
149: public static boolean checkNotEmpty(double value) {
150: return 0.0d != value;
151: }
152:
153: public static boolean checkNotEqual(boolean value, boolean reference) {
154: return value != reference;
155: }
156:
157: public static boolean checkNotEqual(byte value, byte reference) {
158: return value != reference;
159: }
160:
161: public static boolean checkNotEqual(char value, char reference) {
162: return value != reference;
163: }
164:
165: public static boolean checkNotEqual(short value, short reference) {
166: return value != reference;
167: }
168:
169: public static boolean checkNotEqual(int value, int reference) {
170: return value != reference;
171: }
172:
173: public static boolean checkNotEqual(long value, long reference) {
174: return value != reference;
175: }
176:
177: public static boolean checkNotEqual(float value, float reference) {
178: return value != reference;
179: }
180:
181: public static boolean checkNotEqual(double value, double reference) {
182: return value != reference;
183: }
184:
185: public static boolean checkNotEqual(Object value, Object reference) {
186: if (null == value || null == reference) {
187: return true;
188: }
189:
190: if (value.getClass().isArray()) {
191: for (int i = 0; i < Array.getLength(value); i++) {
192: if (!checkNotEqual(Array.get(value, i), reference)) {
193: return false;
194: }
195: }
196:
197: return true;
198: } else {
199: return !value.equals(reference);
200: }
201: }
202:
203: public static boolean checkEqual(boolean value, boolean reference) {
204: return value == reference;
205: }
206:
207: public static boolean checkEqual(byte value, byte reference) {
208: return value == reference;
209: }
210:
211: public static boolean checkEqual(char value, char reference) {
212: return value == reference;
213: }
214:
215: public static boolean checkEqual(short value, short reference) {
216: return value == reference;
217: }
218:
219: public static boolean checkEqual(int value, int reference) {
220: return value == reference;
221: }
222:
223: public static boolean checkEqual(long value, long reference) {
224: return value == reference;
225: }
226:
227: public static boolean checkEqual(float value, float reference) {
228: return value == reference;
229: }
230:
231: public static boolean checkEqual(double value, double reference) {
232: return value == reference;
233: }
234:
235: public static boolean checkEqual(Object value, Object reference) {
236: if (null == value || null == reference) {
237: return true;
238: }
239:
240: if (value.getClass().isArray()) {
241: for (int i = 0; i < Array.getLength(value); i++) {
242: if (!checkEqual(Array.get(value, i), reference)) {
243: return false;
244: }
245: }
246:
247: return true;
248: } else {
249: return value.equals(reference);
250: }
251: }
252:
253: public static boolean checkLength(byte value, int min, int max) {
254: return checkLength(String.valueOf(value), min, max);
255: }
256:
257: public static boolean checkLength(char value, int min, int max) {
258: return checkLength(String.valueOf(value), min, max);
259: }
260:
261: public static boolean checkLength(short value, int min, int max) {
262: return checkLength(String.valueOf(value), min, max);
263: }
264:
265: public static boolean checkLength(int value, int min, int max) {
266: return checkLength(String.valueOf(value), min, max);
267: }
268:
269: public static boolean checkLength(long value, int min, int max) {
270: return checkLength(String.valueOf(value), min, max);
271: }
272:
273: public static boolean checkLength(float value, int min, int max) {
274: return checkLength(String.valueOf(value), min, max);
275: }
276:
277: public static boolean checkLength(double value, int min, int max) {
278: return checkLength(String.valueOf(value), min, max);
279: }
280:
281: public static boolean checkLength(CharSequence value, int min,
282: int max) {
283: if (null == value) {
284: return true;
285: }
286:
287: String string = value.toString();
288: if (min > 0 && string.length() < min) {
289: return false;
290: }
291:
292: return !(max >= 0 && string.length() > max);
293: }
294:
295: public static boolean checkRegexp(CharSequence value, String pattern) {
296: if (null == value || null == pattern || 0 == pattern.length()) {
297: return true;
298: }
299:
300: String string = value.toString();
301: if (0 == string.length()) {
302: return true;
303: }
304:
305: Pattern regexp;
306: try {
307: regexp = Pattern.compile(pattern);
308: } catch (PatternSyntaxException e) {
309: return false;
310: }
311:
312: Matcher match = regexp.matcher(string);
313: return match.matches();
314: }
315:
316: public static boolean checkEmail(CharSequence value) {
317: return checkRegexp(value,
318: "^[a-zA-Z0-9][_\\+\\-\\.\\w]*@[\\w\\.\\-]+[\\w]\\.[a-zA-Z]{2,4}$");
319: }
320:
321: public static boolean checkUrl(CharSequence value) {
322: if (null == value) {
323: return true;
324: }
325:
326: String string = value.toString();
327: if (0 == string.length()) {
328: return true;
329: }
330:
331: if (string.startsWith("https://")) {
332: string = "http" + string.substring(5);
333: }
334:
335: try {
336: new URL(string);
337:
338: return true;
339: } catch (MalformedURLException e) {
340: return false;
341: }
342: }
343:
344: public static boolean checkLaterThanNow(Date value) {
345: if (null == value) {
346: return true;
347: }
348:
349: return value.after(new Date());
350: }
351:
352: public static boolean checkLimitedDate(Object value, Date min,
353: Date max) {
354: if (null == value || !(value instanceof Date)) {
355: return true;
356: }
357:
358: Date date = (Date) value;
359: if (min != null && date.before(min)) {
360: return false;
361: }
362:
363: return !(max != null && date.after(max));
364: }
365:
366: public static boolean checkInList(Object value, String[] list) {
367: if (null == value || null == list || 0 == list.length) {
368: return true;
369: }
370:
371: String[] strings = ArrayUtils.createStringArray(value, null);
372: if (null == strings) {
373: return false;
374: }
375: if (0 == strings.length) {
376: return true;
377: }
378:
379: String[] sorted = list.clone();
380: Arrays.sort(sorted);
381:
382: for (String string : strings) {
383: if (0 == string.length()) {
384: continue;
385: }
386:
387: if (Arrays.binarySearch(sorted, string) < 0) {
388: return false;
389: }
390: }
391:
392: return true;
393: }
394:
395: public static boolean checkRange(byte value, byte begin, byte end) {
396: if (value < begin) {
397: return false;
398: }
399:
400: return value <= end;
401: }
402:
403: public static boolean checkRange(char value, char begin, char end) {
404: if (value < begin) {
405: return false;
406: }
407:
408: return value <= end;
409: }
410:
411: public static boolean checkRange(short value, short begin, short end) {
412: if (value < begin) {
413: return false;
414: }
415:
416: return value <= end;
417: }
418:
419: public static boolean checkRange(int value, int begin, int end) {
420: if (value < begin) {
421: return false;
422: }
423:
424: return value <= end;
425: }
426:
427: public static boolean checkRange(long value, long begin, long end) {
428: if (value < begin) {
429: return false;
430: }
431:
432: return value <= end;
433: }
434:
435: public static boolean checkRange(float value, float begin, float end) {
436: if (value < begin) {
437: return false;
438: }
439:
440: return value <= end;
441: }
442:
443: public static boolean checkRange(double value, double begin,
444: double end) {
445: if (value < begin) {
446: return false;
447: }
448:
449: return value <= end;
450: }
451:
452: public static boolean checkRange(Object value, Comparable begin,
453: Comparable end) {
454: if (null == value) {
455: return true;
456: }
457:
458: if (value.getClass().isArray()) {
459: for (int i = 0; i < Array.getLength(value); i++) {
460: if (!checkRange(Array.get(value, i), begin, end)) {
461: return false;
462: }
463: }
464:
465: return true;
466: } else if (!(value instanceof Comparable)) {
467: return true;
468: }
469:
470: Comparable comparable = (Comparable) value;
471: if (begin != null && comparable.compareTo(begin) < 0) {
472: return false;
473: }
474:
475: return !(end != null && comparable.compareTo(end) > 0);
476: }
477:
478: public static boolean checkFormat(Object value, Format format) {
479: if (null == value || null == format) {
480: return true;
481: }
482:
483: if (!(value instanceof String)) {
484: return true;
485: }
486:
487: String string = (String) value;
488: try {
489: Object parsed = format.parseObject(string);
490: if (null == parsed) {
491: return false;
492: }
493:
494: return format.format(parsed).equals(string);
495:
496: } catch (ParseException e) {
497: return false;
498: }
499: }
500: }
|