44import javax .annotation .Nonnull ;
55import javax .annotation .Nullable ;
66import javax .annotation .concurrent .Immutable ;
7- import java .util .*;
7+ import java .util .Arrays ;
8+ import java .util .Collection ;
9+ import java .util .LinkedList ;
10+ import java .util .List ;
11+ import java .util .Objects ;
12+ import java .util .Set ;
813import java .util .regex .Pattern ;
914
1015/**
@@ -30,7 +35,8 @@ public final class Validation {
3035 private static final Pattern VALID_IP_REGEX = Pattern .compile ("^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\ -]*[a-zA-Z0-9])\\ .)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\\ -]*[A-Za-z0-9])$" );
3136 /** Regex pattern for valid email address */
3237 @ Nonnull
33- private static final Pattern VALID_EMAIL_REGEX = Pattern .compile ("(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\ .[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\" (?:[\\ x01-\\ x08\\ x0b\\ x0c\\ x0e-\\ x1f\\ x21\\ x23-\\ x5b\\ x5d-\\ x7f]|\\ \\ [\\ x01-\\ x09\\ x0b\\ x0c\\ x0e-\\ x7f])*\" )@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\ .)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\ [(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\ .){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\ x01-\\ x08\\ x0b\\ x0c\\ x0e-\\ x1f\\ x21-\\ x5a\\ x53-\\ x7f]|\\ \\ [\\ x01-\\ x09\\ x0b\\ x0c\\ x0e-\\ x7f])+)])" );
38+ private static final Pattern VALID_EMAIL_REGEX = Pattern .compile (
39+ "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\ .[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\" (?:[\\ x01-\\ x08\\ x0b\\ x0c\\ x0e-\\ x1f\\ x21\\ x23-\\ x5b\\ x5d-\\ x7f]|\\ \\ [\\ x01-\\ x09\\ x0b\\ x0c\\ x0e-\\ x7f])*\" )@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\ .)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\ [(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\ .){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\ x01-\\ x08\\ x0b\\ x0c\\ x0e-\\ x1f\\ x21-\\ x5a\\ x53-\\ x7f]|\\ \\ [\\ x01-\\ x09\\ x0b\\ x0c\\ x0e-\\ x7f])+)])" );
3440
3541 /** Message for object with null values */
3642 @ Nonnull
@@ -40,6 +46,14 @@ public final class Validation {
4046 @ Nonnull
4147 static final String NATURAL_NUMBER_MESSAGE = "is expected to be a natural number (1, 2, ..., N-1, N) but it is actually not a natural number" ;
4248
49+ /** Message for greater comparison error */
50+ @ Nonnull
51+ static final String GREATER_NUMBER_MESSAGE = "is expected to be greater number, but it is not" ;
52+
53+ /** Message for lesser comparison error */
54+ @ Nonnull
55+ static final String LESSER_NUMBER_MESSAGE = "is expected to be lesser number, but it is not" ;
56+
4357 /** Message for non-negative number error */
4458 @ Nonnull
4559 static final String NONNEGATIVE_NUMBER_MESSAGE = "is expected to be non-negative but value is actually a negative number" ;
@@ -305,14 +319,276 @@ private static <T> T innerAssertNonnull(@Nullable T object, @Nullable String var
305319 throw updateStackTrace (iae , level );
306320 }
307321
322+ /**
323+ * Asserts that number is a greater than compared number. If it is not a greater an exception will be thrown.
324+ *
325+ * @param number number to be asserted
326+ * @param compare number being compared
327+ * @return valid number
328+ * @throws IllegalArgumentException thrown if the number is invalid in any way
329+ */
330+ public static int assertGreaterThan (int number , long compare ) throws IllegalArgumentException {
331+ return (int ) innerAssertGreaterThan (number , compare , null );
332+ }
333+
334+ /**
335+ * Asserts that number is a greater than compared number. If it is not a greater an exception will be thrown.
336+ *
337+ * @param number number to be asserted
338+ * @param compare number being compared
339+ * @param variable_name name of variable
340+ * @return valid number
341+ * @throws IllegalArgumentException thrown if the number is invalid in any way
342+ */
343+ public static int assertGreaterThan (int number , long compare , @ Nonnull String variable_name )
344+ throws IllegalArgumentException {
345+ assertNonnull (variable_name , "variable_name" );
346+ return (int ) innerAssertGreaterThan (number , compare , variable_name );
347+ }
348+
349+ /**
350+ * Asserts that number is a greater than compared number. If it is not a greater an exception will be thrown.
351+ *
352+ * @param number number to be asserted
353+ * @param compare number being compared
354+ * @return valid number
355+ * @throws IllegalArgumentException thrown if the number is invalid in any way
356+ */
357+ public static long assertGreaterThan (long number , long compare ) throws IllegalArgumentException {
358+ return innerAssertGreaterThan (number , compare , null );
359+ }
360+
361+ /**
362+ * Asserts that number is a greater than compared number. If it is not a greater an exception will be thrown.
363+ *
364+ * @param number number to be asserted
365+ * @param compare number being compared
366+ * @param variable_name name of variable
367+ * @return valid number
368+ * @throws IllegalArgumentException thrown if the number is invalid in any way
369+ */
370+ public static long assertGreaterThan (long number , long compare , @ Nonnull String variable_name )
371+ throws IllegalArgumentException {
372+ assertNonnull (variable_name , "variable_name" );
373+ return innerAssertGreaterThan (number , compare , variable_name );
374+ }
375+
376+ /**
377+ * Asserts that number is a greater than compared number. If it is not a greater an exception will be thrown.
378+ *
379+ * @param number number to be asserted
380+ * @param compare number being compared
381+ * @param variable_name name of variable
382+ * @return valid number
383+ * @throws IllegalArgumentException thrown if the number is invalid in any way
384+ */
385+ private static long innerAssertGreaterThan (long number , long compare , @ Nullable String variable_name )
386+ throws IllegalArgumentException {
387+
388+ // Exit if not null
389+ if (number > compare ) return number ;
390+
391+ // Otherwise go ahead and throw exception
392+ String message = composeMessage (variable_name , GREATER_NUMBER_MESSAGE );
393+
394+ // Create exception
395+ IllegalArgumentException iae = new IllegalArgumentException (message );
396+
397+ // Update stacktrace and throw it
398+ throw updateStackTrace (iae , 0 );
399+ }
400+
401+ /**
402+ * Asserts that number is a lesser than compared number. If it is not a lesser an exception will be thrown.
403+ *
404+ * @param number number to be asserted
405+ * @param compare number being compared
406+ * @return valid number
407+ * @throws IllegalArgumentException thrown if the number is invalid in any way
408+ */
409+ public static int assertLesserThan (int number , long compare ) throws IllegalArgumentException {
410+ return (int ) innerAssertLesserThan (number , compare , null );
411+ }
412+
413+ /**
414+ * Asserts that number is a lesser than compared number. If it is not a lesser an exception will be thrown.
415+ *
416+ * @param number number to be asserted
417+ * @param compare number being compared
418+ * @param variable_name name of variable
419+ * @return valid number
420+ * @throws IllegalArgumentException thrown if the number is invalid in any way
421+ */
422+ public static int assertLesserThan (int number , long compare , @ Nonnull String variable_name )
423+ throws IllegalArgumentException {
424+ assertNonnull (variable_name , "variable_name" );
425+ return (int ) innerAssertLesserThan (number , compare , variable_name );
426+ }
427+
428+ /**
429+ * Asserts that number is a lesser than compared number. If it is not a lesser an exception will be thrown.
430+ *
431+ * @param number number to be asserted
432+ * @param compare number being compared
433+ * @return valid number
434+ * @throws IllegalArgumentException thrown if the number is invalid in any way
435+ */
436+ public static long assertLesserThan (long number , long compare ) throws IllegalArgumentException {
437+ return innerAssertLesserThan (number , compare , null );
438+ }
439+
440+ /**
441+ * Asserts that number is a lesser than compared number. If it is not a lesser an exception will be thrown.
442+ *
443+ * @param number number to be asserted
444+ * @param compare number being compared
445+ * @param variable_name name of variable
446+ * @return valid number
447+ * @throws IllegalArgumentException thrown if the number is invalid in any way
448+ */
449+ public static long assertLesserThan (long number , long compare , @ Nonnull String variable_name )
450+ throws IllegalArgumentException {
451+ assertNonnull (variable_name , "variable_name" );
452+ return innerAssertLesserThan (number , compare , variable_name );
453+ }
454+
455+
456+ /**
457+ * Asserts that number is a greater than or equals to compared number. If it is not greater or equal, an exception will be thrown.
458+ *
459+ * @param number number to be asserted
460+ * @param compare number being compared
461+ * @return valid number
462+ * @throws IllegalArgumentException thrown if the number is invalid in any way
463+ */
464+ public static int assertGreaterThanOrEqual (int number , long compare ) throws IllegalArgumentException {
465+ return (int ) innerAssertGreaterThanOrEqual (number , compare , null );
466+ }
467+
468+ /**
469+ * Asserts that number is a greater than or equals to compared number. If it is not greater or equal, an exception will be thrown.
470+ *
471+ * @param number number to be asserted
472+ * @param compare number being compared
473+ * @param variable_name name of variable
474+ * @return valid number
475+ * @throws IllegalArgumentException thrown if the number is invalid in any way
476+ */
477+ public static int assertGreaterThanOrEqual (int number , long compare , @ Nonnull String variable_name )
478+ throws IllegalArgumentException {
479+ assertNonnull (variable_name , "variable_name" );
480+ return (int ) innerAssertGreaterThanOrEqual (number , compare , variable_name );
481+ }
482+
483+ /**
484+ * Asserts that number is a greater than or equals to compared number. If it is not greater or equal, an exception will be thrown.
485+ *
486+ * @param number number to be asserted
487+ * @param compare number being compared
488+ * @return valid number
489+ * @throws IllegalArgumentException thrown if the number is invalid in any way
490+ */
491+ public static long assertGreaterThanOrEqual (long number , long compare ) throws IllegalArgumentException {
492+ return innerAssertGreaterThanOrEqual (number , compare , null );
493+ }
494+
495+ /**
496+ * Asserts that number is a greater than or equals to compared number. If it is not greater or equal, an exception will be thrown.
497+ *
498+ * @param number number to be asserted
499+ * @param compare number being compared
500+ * @param variable_name name of variable
501+ * @return valid number
502+ * @throws IllegalArgumentException thrown if the number is invalid in any way
503+ */
504+ public static long assertGreaterThanOrEqual (long number , long compare , @ Nonnull String variable_name )
505+ throws IllegalArgumentException {
506+ assertNonnull (variable_name , "variable_name" );
507+ return innerAssertGreaterThanOrEqual (number , compare , variable_name );
508+ }
509+
510+ /**
511+ * Asserts that number is a greater than or equals to compared number. If it is not greater or equal, an exception will be thrown.
512+ *
513+ * @param number number to be asserted
514+ * @param compare number being compared
515+ * @param variable_name name of variable
516+ * @return valid number
517+ * @throws IllegalArgumentException thrown if the number is invalid in any way
518+ */
519+ private static long innerAssertGreaterThanOrEqual (long number , long compare , @ Nullable String variable_name )
520+ throws IllegalArgumentException {
521+
522+ // Exit if not null
523+ if (number > compare ) return number ;
524+
525+ // Otherwise go ahead and throw exception
526+ String message = composeMessage (variable_name , GREATER_NUMBER_MESSAGE );
527+
528+ // Create exception
529+ IllegalArgumentException iae = new IllegalArgumentException (message );
530+
531+ // Update stacktrace and throw it
532+ throw updateStackTrace (iae , 0 );
533+ }
534+
535+ /**
536+ * Asserts that number is a lesser than compared number. If it is not a lesser an exception will be thrown.
537+ *
538+ * @param number number to be asserted
539+ * @param compare number being compared
540+ * @param variable_name name of variable
541+ * @return valid number
542+ * @throws IllegalArgumentException thrown if the number is invalid in any way
543+ */
544+ private static long innerAssertLesserThan (long number , long compare , @ Nullable String variable_name )
545+ throws IllegalArgumentException {
546+
547+ // Exit if not null
548+ if (number < compare ) return number ;
549+
550+ // Otherwise go ahead and throw exception
551+ String message = composeMessage (variable_name , LESSER_NUMBER_MESSAGE );
552+
553+ // Create exception
554+ IllegalArgumentException iae = new IllegalArgumentException (message );
555+
556+ // Update stacktrace and throw it
557+ throw updateStackTrace (iae , 0 );
558+ }
559+
560+ /**
561+ * Asserts that number is a natural number. If it is not a natural number, then an exception will be thrown.
562+ *
563+ * @param number number to be asserted
564+ * @return valid nonnegative number
565+ * @throws IllegalArgumentException thrown if the number is invalid in any way
566+ */
567+ public static int assertNaturalNumber (int number ) throws IllegalArgumentException {
568+ return (int ) innerAssertNaturalNumber (number , null );
569+ }
570+
571+ /**
572+ * Asserts that number is a natural number. If it is not a natural number, then an exception will be thrown.
573+ *
574+ * @param number number to be asserted
575+ * @param variable_name name of variable
576+ * @return valid nonnegative number
577+ * @throws IllegalArgumentException thrown if the number is invalid in any way
578+ */
579+ public static int assertNaturalNumber (int number , @ Nonnull String variable_name ) throws IllegalArgumentException {
580+ assertNonnull (variable_name , "variable_name" );
581+ return (int ) innerAssertNaturalNumber (number , variable_name );
582+ }
583+
308584 /**
309585 * Asserts that number is a natural number. If it is not a natural number, then an exception will be thrown.
310586 *
311587 * @param number number to be asserted
312588 * @return valid nonnegative number
313589 * @throws IllegalArgumentException thrown if the number is invalid in any way
314590 */
315- public static long assertNaturalNumber (long number ) throws IllegalArgumentException {
591+ public static long assertNaturalNumber (long number ) throws IllegalArgumentException {
316592 return innerAssertNaturalNumber (number , null );
317593 }
318594
0 commit comments