|
Version 4.10.0 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Object hirondelle.web4j.util.Args
public final class Args
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 |
---|
public static void checkForContent(String aText)
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.
public static void checkForRange(int aNumber, int aLow, int aHigh)
Util.isInRange(int, int, int)
returns false
, then
throw an IllegalArgumentException
.
aLow
- is less than or equal to aHigh
.public static void checkForPositive(int aNumber)
public static void checkForMatch(Pattern aPattern, String aText)
Util.matches(java.util.regex.Pattern, java.lang.String)
returns false, then
throw an IllegalArgumentException
.
public static void checkForNull(Object aObject)
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 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |