Version 4.10.0

hirondelle.web4j.util
Class Args

Object
  extended by hirondelle.web4j.util.Args

public final class Args
extends Object

Utility methods for common argument validations.

Replaces if statements at the start of a method with more compact method calls.

Example use case.

Instead of :

 public void doThis(String aText){
   if (!Util.textHasContent(aText)){
     throw new IllegalArgumentException();
   }
   //..main body elided
 }
 

One may instead write :

 public void doThis(String aText){
   Args.checkForContent(aText);
   //..main body elided
 }
 


Method Summary
static void checkForContent(String aText)
          If aText does not satisfy Util.textHasContent(java.lang.String), then throw an IllegalArgumentException.
static void checkForMatch(Pattern aPattern, String aText)
          If Util.matches(java.util.regex.Pattern, java.lang.String) returns false, then throw an IllegalArgumentException.
static void checkForNull(Object aObject)
          If aObject is null, then throw a NullPointerException.
static void checkForPositive(int aNumber)
          If aNumber is less than 1, then throw an IllegalArgumentException.
static void checkForRange(int aNumber, int aLow, int aHigh)
          If Util.isInRange(int, int, int) returns false, then throw an IllegalArgumentException.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

checkForContent

public static void checkForContent(String aText)
If aText does not satisfy Util.textHasContent(java.lang.String), then throw an IllegalArgumentException.

Most text used in an application is meaningful only if it has visible content.


checkForRange

public static void checkForRange(int aNumber,
                                 int aLow,
                                 int aHigh)
If Util.isInRange(int, int, int) returns false, then throw an IllegalArgumentException.

Parameters:
aLow - is less than or equal to aHigh.

checkForPositive

public static void checkForPositive(int aNumber)
If aNumber is less than 1, then throw an IllegalArgumentException.


checkForMatch

public static void checkForMatch(Pattern aPattern,
                                 String aText)
If Util.matches(java.util.regex.Pattern, java.lang.String) returns false, then throw an IllegalArgumentException.


checkForNull

public static void checkForNull(Object aObject)
If aObject is null, then throw a NullPointerException.

Use cases :

   doSomething( Football aBall ){
     //1. call some method on the argument : 
     //if aBall is null, then exception is automatically thrown, so 
     //there is no need for an explicit check for null.
     aBall.inflate();

     //2. assign to a corresponding field (common in constructors): 
     //if aBall is null, no exception is immediately thrown, so 
     //an explicit check for null may be useful here
     Args.checkForNull( aBall );
     fBall = aBall;

     //3. pass on to some other method as parameter : 
     //it may or may not be appropriate to have an explicit check 
     //for null here, according the needs of the problem
     Args.checkForNull( aBall ); //??
     fReferee.verify( aBall );
   }
   


Version 4.10.0

Copyright Hirondelle Systems. Published October 19, 2013 - User Guide - All Docs.