| 
 | ||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.jgoodies.common.base.Preconditions
public final class Preconditions
Reduces the code necessary to check preconditions on method state and parameters.
| Method Summary | ||
|---|---|---|
| static void | checkArgument(boolean expression,
              String message)Checks the truth of the given expression and throws a customized IllegalArgumentExceptionif it is false. | |
| static void | checkArgument(boolean expression,
              String messageFormat,
              Object... messageArgs)Checks the truth of the given expression and throws a customized IllegalArgumentExceptionif it is false. | |
| static String | checkNotBlank(String str,
              String message)Checks that the given string is not blank and throws a customized NullPointerExceptionif it isnull, and a customizedIllegalArgumentExceptionif 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 NullPointerExceptionif it isnull, and a customizedIllegalArgumentExceptionif it is empty or whitespace. | |
| static
 | checkNotNull(T reference,
             String message)Checks that the given object reference is not nulland throws a customizedNullPointerExceptionif it is. | |
| static
 | checkNotNull(T reference,
             String messageFormat,
             Object... messageArgs)Checks that the given object reference is not nulland throws a customizedNullPointerExceptionif it is. | |
| static void | checkState(boolean expression,
           String message)Checks the truth of the given expression and throws a customized IllegalStateExceptionif it is false. | |
| static void | checkState(boolean expression,
           String messageFormat,
           Object... messageArgs)Checks the truth of the given expression and throws a customized IllegalStateExceptionif it is false. | |
| Methods inherited from class java.lang.Object | 
|---|
| clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait | 
| Method Detail | 
|---|
public static void checkArgument(boolean expression,
                                 String message)
IllegalArgumentException if it is false. Intended for doing
 parameter validation in methods and constructors, e.g.:
 
 public void foo(int count) {
    Preconditions.checkArgument(count > 0, "count must be positive.");
 }
 
expression - the precondition to check involving one ore more
                      parameters to the calling method or constructormessage - the detail message to be used in the event that
                      an exception is thrown
IllegalArgumentException - if expression is false
public static void checkArgument(boolean expression,
                                 String messageFormat,
                                 Object... messageArgs)
IllegalArgumentException if it is false. Intended for doing
 parameter validation in methods and constructors, e.g.:
 
 public void foo(int count) {
    Preconditions.checkArgument(count > 0, "count must be positive: %s.", count);
 }
 
expression - the precondition to check involving one ore more
                      parameters to the calling method or constructormessageFormat - a 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 messageFormat
IllegalArgumentException - if expression is false
public static <T> T checkNotNull(T reference,
                                 String message)
null
 and throws a customized NullPointerException if it is.
 Intended for doing parameter validation in methods and constructors,
 e.g.:
 
 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.");
 }
 
T - the type of the referencereference - the object reference to check for being nullmessage - the detail message to be used in the event that
                      an exception is thrown
reference if not null
NullPointerException - if reference is null
public static <T> T checkNotNull(T reference,
                                 String messageFormat,
                                 Object... messageArgs)
null
 and throws a customized NullPointerException if it is.
 Intended for doing parameter validation in methods and constructors,
 e.g.:
 
 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");
 }
 
T - the type of the referencereference - the object reference to check for being nullmessageFormat - a 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 messageFormat
reference if not null
NullPointerException - if reference is null
public static void checkState(boolean expression,
                              String message)
IllegalStateException if it is false. Intended for doing
 validation in methods involving the state of the calling instance,
 but not involving parameters of the calling method, e.g.:
 
 public void unlock() {
    Preconditions.checkState(locked, "Must be locked to be unlocked.");
 }
 
expression - the precondition to check involving the state
                      of the calling instancemessage - the detail message to be used in the event that
                      an exception is thrown
IllegalStateException - if expression is false
public static void checkState(boolean expression,
                              String messageFormat,
                              Object... messageArgs)
IllegalStateException if it is false. Intended for doing
 validation in methods involving the state of the calling instance,
 but not involving parameters of the calling method, e.g.:
 
 public void unlock() {
    Preconditions.checkState(locked,
        "Must be locked to be unlocked. Most recent lock: %s",
        mostRecentLock);
 }
 
expression - the precondition to check involving the state
                      of the calling instancemessageFormat - a 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 messageFormat
IllegalStateException - if expression is false
public static String checkNotBlank(String str,
                                   String message)
NullPointerException if it is null, and a customized
 IllegalArgumentException if it is empty or whitespace.
 Intended for doing parameter validation in methods and constructors,
 e.g.:
 
 public void foo(String text) {
      checkNotBlank(text, "The text must not be null, empty or whitespace.");
 }
 
str - the string to check for being blankmessage - the detail message to be used in the event that
                      an exception is thrown
str if not null
NullPointerException - if str is null
IllegalArgumentException - if str is empty or whitespace
public static String checkNotBlank(String str,
                                   String messageFormat,
                                   Object... messageArgs)
NullPointerException if it is null, and a customized
 IllegalArgumentException if it is empty or whitespace.
 Intended for doing parameter validation in methods and constructors,
 e.g.:
 
 public void foo(String text, String id) {
      checkNotBlank(
          text,
          "The text for %s must not be null, empty or whitespace.",
          id);
 }
 
str - the string to check for being blankmessageFormat - a 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 messageFormat
str if not null
NullPointerException - if str is null
IllegalArgumentException - if str is empty or whitespace| 
 | ||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||