com.springsource.util.common
Class Assert

java.lang.Object
  extended by com.springsource.util.common.Assert

public final class Assert
extends java.lang.Object

A set of useful assertions based on those provided by the Spring Framework's Assert class. Concurrent Semantics
This class is thread safe.

Since:
Jersey
See Also:
org.junit.Assert

Constructor Summary
private Assert()
           
 
Method Summary
static void hasLength(java.lang.String text, java.lang.String message, java.lang.Object... inserts)
          Assert that the given String is not empty; that is, it must not be null and not the empty String.
static
<T,U> void
isAssignable(java.lang.Class<T> superType, java.lang.Class<U> subType, java.lang.String message, java.lang.Object... inserts)
          Assert that superType.isAssignableFrom(subType) is true.
static void isFalse(boolean expression, java.lang.String message, java.lang.Object... inserts)
          Assert a boolean expression, throwing a IllegalArgumentException if the test result is true.
static
<T> void
isInstanceOf(java.lang.Class<T> type, java.lang.Object obj, java.lang.String message, java.lang.Object... inserts)
          Assert that the provided object is a non-null instance of the provided class.
static void isNull(java.lang.Object object, java.lang.String message, java.lang.Object... inserts)
          Assert that an object is null.
static void isTrue(boolean expression, java.lang.String message, java.lang.Object... inserts)
          Assert a boolean expression, throwing a IllegalArgumentException if the test result is false.
static
<T> void
notEmpty(java.util.Collection<T> collection, java.lang.String message, java.lang.Object... inserts)
          Assert that a collection has elements; that is, it must not be null and must have at least one element.
static
<K,V> void
notEmpty(java.util.Map<K,V> map, java.lang.String message, java.lang.Object... inserts)
          Assert that a Map has entries; that is, it must not be null and must have at least one entry.
static void notEmpty(java.lang.Object[] array, java.lang.String message, java.lang.Object... inserts)
          Assert that an array has elements; that is, it must not be null and must have at least one element.
static void notNull(java.lang.Object object, java.lang.String message, java.lang.Object... inserts)
          Assert that an object is not null.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Assert

private Assert()
Method Detail

isTrue

public static void isTrue(boolean expression,
                          java.lang.String message,
                          java.lang.Object... inserts)
Assert a boolean expression, throwing a IllegalArgumentException if the test result is false.
 Assert.isTrue(i > 0, "The value must be greater than zero");
 

Parameters:
expression - a boolean expression
message - the exception message to use if the assertion fails
inserts - any inserts to include if the message is a format string.
Throws:
java.lang.IllegalArgumentException - if expression is false

isFalse

public static void isFalse(boolean expression,
                           java.lang.String message,
                           java.lang.Object... inserts)
Assert a boolean expression, throwing a IllegalArgumentException if the test result is true.
 Assert.isFalse(state.isBroken(), "The state is broken");
 

Parameters:
expression - a boolean expression
message - the exception message to use if the assertion fails
inserts - any inserts to include if the message is a format string.
Throws:
java.lang.IllegalArgumentException - if expression is false

isNull

public static void isNull(java.lang.Object object,
                          java.lang.String message,
                          java.lang.Object... inserts)
Assert that an object is null.
 Assert.isNull(value, "The value must be null");
 

Parameters:
object - the object to check
message - the exception message to use if the assertion fails
inserts - any inserts to include if the message is a format string.
Throws:
java.lang.IllegalArgumentException - if the object is not null

notNull

public static void notNull(java.lang.Object object,
                           java.lang.String message,
                           java.lang.Object... inserts)
Assert that an object is not null.
 Assert.notNull(clazz, "The class must not be null");
 

Parameters:
object - the object to check
message - the exception message to use if the assertion fails
inserts - any inserts to include if the message is a format string.
Throws:
java.lang.IllegalArgumentException - if the object is null

hasLength

public static void hasLength(java.lang.String text,
                             java.lang.String message,
                             java.lang.Object... inserts)
Assert that the given String is not empty; that is, it must not be null and not the empty String.
 Assert.hasLength(name, "Name must not be empty");
 

Parameters:
text - the String to check
message - the exception message to use if the assertion fails
inserts - any inserts to include if the message is a format string.
See Also:
StringUtils.hasLength(java.lang.CharSequence)

notEmpty

public static void notEmpty(java.lang.Object[] array,
                            java.lang.String message,
                            java.lang.Object... inserts)
Assert that an array has elements; that is, it must not be null and must have at least one element.
 Assert.notEmpty(array, "The array must have elements");
 

Parameters:
array - the array to check
message - the exception message to use if the assertion fails
inserts - any inserts to include if the message is a format string.
Throws:
java.lang.IllegalArgumentException - if the object array is null or has no elements

notEmpty

public static <T> void notEmpty(java.util.Collection<T> collection,
                                java.lang.String message,
                                java.lang.Object... inserts)
Assert that a collection has elements; that is, it must not be null and must have at least one element.
 Assert.notEmpty(collection, "Collection must have elements");
 

Type Parameters:
T - the type of the elements of the collection
Parameters:
collection - the collection to check
message - the exception message to use if the assertion fails
inserts - any inserts to include if the message is a format string.
Throws:
java.lang.IllegalArgumentException - if the collection is null or has no elements

notEmpty

public static <K,V> void notEmpty(java.util.Map<K,V> map,
                                  java.lang.String message,
                                  java.lang.Object... inserts)
Assert that a Map has entries; that is, it must not be null and must have at least one entry.
 Assert.notEmpty(map, "Map must have entries");
 

Type Parameters:
K - type of Key (domain)
V - type of Value (range)
Parameters:
map - the map to check
message - the exception message to use if the assertion fails
inserts - any inserts to include if the message is a format string.
Throws:
java.lang.IllegalArgumentException - if the map is null or has no entries

isInstanceOf

public static <T> void isInstanceOf(java.lang.Class<T> type,
                                    java.lang.Object obj,
                                    java.lang.String message,
                                    java.lang.Object... inserts)
Assert that the provided object is a non-null instance of the provided class.
 Assert.instanceOf(Foo.class, foo);
 

Type Parameters:
T - the type to check this for
Parameters:
type - the type to check against
obj - the object to check
message - a message which will be prepended to the message produced by the function itself, and which may be used to provide context. It should normally end in a ": " or ". " so that the function generate message looks ok when appended to it.
inserts - any inserts to include if the message is a format string.
Throws:
java.lang.IllegalArgumentException - if the object is not an instance of clazz
See Also:
Class.isInstance(java.lang.Object)

isAssignable

public static <T,U> void isAssignable(java.lang.Class<T> superType,
                                      java.lang.Class<U> subType,
                                      java.lang.String message,
                                      java.lang.Object... inserts)
Assert that superType.isAssignableFrom(subType) is true.
 Assert.isAssignable(Number.class, myClass);
 

Type Parameters:
T - the supertype
U - the subtype
Parameters:
superType - the super type to check against
subType - the sub type to check
message - a message which will be prepended to the message produced by the function itself, and which may be used to provide context. It should normally end in a ": " or ". " so that the function generate message looks ok when appended to it.
inserts - any inserts to include if the message is a format string.
Throws:
java.lang.IllegalArgumentException - if the classes are not assignable