com.jgoodies.common.base
public final class Preconditions extends Object
Version: $Revision: 1.10 $
Method Summary | |
---|---|
static void | checkArgument(boolean expression, String message)
Checks the truth of the given expression and throws a customized
IllegalArgumentException if it is false. |
static void | checkArgument(boolean expression, String messageFormat, Object... messageArgs)
Checks the truth of the given expression and throws a customized
IllegalArgumentException if it is false. |
static String | checkNotBlank(String str, String message)
Checks that the given string is not blank and throws a customized
NullPointerException if it is {@code null}, and a customized
IllegalArgumentException if it is empty or whitespace.
|
static String | checkNotBlank(String str, String messageFormat, Object... messageArgs)
Checks that the given string is not blank and throws a customized
NullPointerException if it is {@code null}, and a customized
IllegalArgumentException if it is empty or whitespace.
|
static <T> T | checkNotNull(T reference, String message)
Checks that the given object reference is not {@code null}
and throws a customized NullPointerException if it is.
|
static <T> T | checkNotNull(T reference, String messageFormat, Object... messageArgs)
Checks that the given object reference is not {@code null}
and throws a customized NullPointerException if it is.
|
static void | checkState(boolean expression, String message)
Checks the truth of the given expression and throws a customized
IllegalStateException if it is false. |
static void | checkState(boolean expression, String messageFormat, Object... messageArgs)
Checks the truth of the given expression and throws a customized
IllegalStateException if it is false. |
public void foo(int count) { Preconditions.checkArgument(count > 0, "count must be positive."); }
Parameters: expression the precondition to check involving one ore more parameters to the calling method or constructor message the detail message to be used in the event that an exception is thrown
Throws: IllegalArgumentException if {@code expression} is false
public void foo(int count) { Preconditions.checkArgument(count > 0, "count must be positive: %s.", count); }
Parameters: expression the precondition to check involving one ore more
parameters to the calling method or constructor messageFormat a Formatter format
string for the detail message to be used
in the event that an exception is thrown. messageArgs the arguments referenced by the format specifiers
in the {@code messageFormat}
Throws: IllegalArgumentException if {@code expression} is false
public void foo(String text) { checkNotBlank(text, "The text must not be null, empty or whitespace."); }
Parameters: str the string to check for being blank message the detail message to be used in the event that an exception is thrown
Returns: {@code str} if not {@code null}
Throws: NullPointerException if {@code str} is {@code null} IllegalArgumentException if {@code str} is empty or whitespace
public void foo(String text, String id) { checkNotBlank( text, "The text for %s must not be null, empty or whitespace.", id); }
Parameters: str the string to check for being blank messageFormat a Formatter format
string for the detail message to be used
in the event that an exception is thrown. messageArgs the arguments referenced by the format specifiers
in the {@code messageFormat}
Returns: {@code str} if not {@code null}
Throws: NullPointerException if {@code str} is {@code null} IllegalArgumentException if {@code str} is empty or whitespace
public void foo(Bar bar, Baz baz) { this.bar = Preconditions.checkNotNull(bar, "bar must not be null."); Preconditions.checkNotBull(baz, "baz must not be null."); }
Parameters: reference the object reference to check for being {@code null} message the detail message to be used in the event that
an exception is thrown
Returns: {@code reference} if not {@code null}
Throws: NullPointerException if {@code reference} is {@code null}
public void foo(Bar bar, Baz baz) { this.bar = Preconditions.checkNotNull(bar, "bar must not be null."); Preconditions.checkNotBull(baz, "The %s must not be null.", "baz"); }
Parameters: reference the object reference to check for being {@code null} messageFormat a Formatter format
string for the detail message to be used
in the event that an exception is thrown. messageArgs the arguments referenced by the format specifiers
in the {@code messageFormat}
Returns: {@code reference} if not {@code null}
Throws: NullPointerException if {@code reference} is {@code null}
public void unlock() { Preconditions.checkState(locked, "Must be locked to be unlocked."); }
Parameters: expression the precondition to check involving the state of the calling instance message the detail message to be used in the event that an exception is thrown
Throws: IllegalStateException if {@code expression} is false
public void unlock() { Preconditions.checkState(locked, "Must be locked to be unlocked. Most recent lock: %s", mostRecentLock); }
Parameters: expression the precondition to check involving the state
of the calling instance messageFormat a Formatter format
string for the detail message to be used
in the event that an exception is thrown. messageArgs the arguments referenced by the format specifiers
in the {@code messageFormat}
Throws: IllegalStateException if {@code expression} is false