org.jboss.dna.common.util
Class CheckArg

java.lang.Object
  extended by org.jboss.dna.common.util.CheckArg

@Immutable
public final class CheckArg
extends Object

Utility class that checks arguments to methods. This class is to be used only in API methods, where failure to supply correct arguments should result in a useful error message. In all cases, use the assert statement.


Method Summary
static void contains(Collection<?> argument, Object value, String name)
          Check that the collection contains the value
static void containsKey(Map<?,?> argument, Object key, String name)
          Check that the map contains the key
static void containsNoNulls(Iterable<?> argument, String name)
          Check that the collection is not null and contains no nulls
static void containsNoNulls(Object[] argument, String name)
          Check that the array is not null and contains no nulls
static
<C> C
getInstanceOf(Object argument, Class<C> expectedClass, String name)
          Checks that the object is an instance of the specified Class and then returns the object cast to the specified Class
static
<T> T
getNotNull(T argument, String name)
          Returns the specified argument if it is not null.
static void hasSizeOfAtLeast(Collection<?> argument, int minimumSize, String name)
          Check that the collection contains at least the supplied number of elements
static void hasSizeOfAtLeast(Map<?,?> argument, int minimumSize, String name)
          Check that the map contains at least the supplied number of entries
static void hasSizeOfAtLeast(Object[] argument, int minimumSize, String name)
          Check that the array contains at least the supplied number of elements
static void hasSizeOfAtMost(Collection<?> argument, int maximumSize, String name)
          Check that the collection contains no more than the supplied number of elements
static void hasSizeOfAtMost(Map<?,?> argument, int maximumSize, String name)
          Check that the map contains no more than the supplied number of entries
static void hasSizeOfAtMost(Object[] argument, int maximumSize, String name)
          Check that the array contains no more than the supplied number of elements
static void isEmpty(Object[] argument, String name)
          Check that the array is empty
static
<T> void
isEquals(T argument, String argumentName, T object, String objectName)
          Asserts that the specified first object is equal to the specified second object.
static void isGreaterThan(double argument, double greaterThanValue, String name)
          Check that the argument is greater than the supplied value
static void isGreaterThan(int argument, int greaterThanValue, String name)
          Check that the argument is greater than the supplied value
static void isGreaterThanOrEqualTo(int argument, int greaterThanOrEqualToValue, String name)
          Check that the argument is greater than or equal to the supplied value
static void isInstanceOf(Object argument, Class<?> expectedClass, String name)
          Check that the object is an instance of the specified Class
static void isLessThan(int argument, int lessThanValue, String name)
          Check that the argument is less than the supplied value
static void isLessThanOrEqualTo(int argument, int lessThanOrEqualToValue, String name)
          Check that the argument is less than or equal to the supplied value
static void isNegative(double argument, String name)
          Check that the argument is negative (<0).
static void isNegative(int argument, String name)
          Check that the argument is negative (<0).
static void isNegative(long argument, String name)
          Check that the argument is negative (<0).
static void isNonNegative(double argument, String name)
          Check that the argument is non-negative (>=0).
static void isNonNegative(int argument, String name)
          Check that the argument is non-negative (>=0).
static void isNonNegative(long argument, String name)
          Check that the argument is non-negative (>=0).
static void isNonPositive(double argument, String name)
          Check that the argument is non-positive (<=0).
static void isNonPositive(int argument, String name)
          Check that the argument is non-positive (<=0).
static void isNonPositive(long argument, String name)
          Check that the argument is non-positive (<=0).
static void isNotEmpty(Collection<?> argument, String name)
          Check that the collection is not empty
static void isNotEmpty(Iterator<?> argument, String name)
          Checks that the iterator is not empty, and throws an exception if it is.
static void isNotEmpty(Map<?,?> argument, String name)
          Check that the map is not empty
static void isNotEmpty(Object[] argument, String name)
          Check that the array is not empty
static void isNotEmpty(String argument, String name)
          Check that the string is not empty, is not null, and does not contain only whitespace.
static
<T> void
isNotEquals(T argument, String argumentName, T object, String objectName)
          Asserts that the specified first object is not equal to the specified second object.
static void isNotGreaterThan(int argument, int notGreaterThanValue, String name)
          Check that the argument is not greater than the supplied value
static void isNotLessThan(int argument, int notLessThanValue, String name)
          Check that the argument is not less than the supplied value
static void isNotNan(double argument, String name)
          Check that the argument is not NaN.
static void isNotNull(Object argument, String name)
          Check that the specified argument is non-null
static
<T> void
isNotSame(T argument, String argumentName, T object, String objectName)
          Asserts that the specified first object is not the same as (==) the specified second object.
static void isNotZeroLength(String argument, String name)
          Check that the string is non-null and has length > 0
static void isNull(Object argument, String name)
          Check that the argument is null
static void isPositive(double argument, String name)
          Check that the argument is positive (>0).
static void isPositive(int argument, String name)
          Check that the argument is positive (>0).
static void isPositive(long argument, String name)
          Check that the argument is positive (>0).
static
<T> void
isSame(T argument, String argumentName, T object, String objectName)
          Asserts that the specified first object is the same as (==) the specified second object.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

isNotLessThan

public static void isNotLessThan(int argument,
                                 int notLessThanValue,
                                 String name)
Check that the argument is not less than the supplied value

Parameters:
argument - The argument
notLessThanValue - the value that is to be used to check the value
name - The name of the argument
Throws:
IllegalArgumentException - If argument greater than or equal to the supplied vlaue

isNotGreaterThan

public static void isNotGreaterThan(int argument,
                                    int notGreaterThanValue,
                                    String name)
Check that the argument is not greater than the supplied value

Parameters:
argument - The argument
notGreaterThanValue - the value that is to be used to check the value
name - The name of the argument
Throws:
IllegalArgumentException - If argument is less than or equal to the supplied value

isGreaterThan

public static void isGreaterThan(int argument,
                                 int greaterThanValue,
                                 String name)
Check that the argument is greater than the supplied value

Parameters:
argument - The argument
greaterThanValue - the value that is to be used to check the value
name - The name of the argument
Throws:
IllegalArgumentException - If argument is not greater than the supplied value

isGreaterThan

public static void isGreaterThan(double argument,
                                 double greaterThanValue,
                                 String name)
Check that the argument is greater than the supplied value

Parameters:
argument - The argument
greaterThanValue - the value that is to be used to check the value
name - The name of the argument
Throws:
IllegalArgumentException - If argument is not greater than the supplied value

isLessThan

public static void isLessThan(int argument,
                              int lessThanValue,
                              String name)
Check that the argument is less than the supplied value

Parameters:
argument - The argument
lessThanValue - the value that is to be used to check the value
name - The name of the argument
Throws:
IllegalArgumentException - If argument is not less than the supplied value

isGreaterThanOrEqualTo

public static void isGreaterThanOrEqualTo(int argument,
                                          int greaterThanOrEqualToValue,
                                          String name)
Check that the argument is greater than or equal to the supplied value

Parameters:
argument - The argument
greaterThanOrEqualToValue - the value that is to be used to check the value
name - The name of the argument
Throws:
IllegalArgumentException - If argument is not greater than or equal to the supplied value

isLessThanOrEqualTo

public static void isLessThanOrEqualTo(int argument,
                                       int lessThanOrEqualToValue,
                                       String name)
Check that the argument is less than or equal to the supplied value

Parameters:
argument - The argument
lessThanOrEqualToValue - the value that is to be used to check the value
name - The name of the argument
Throws:
IllegalArgumentException - If argument is not less than or equal to the supplied value

isNonNegative

public static void isNonNegative(int argument,
                                 String name)
Check that the argument is non-negative (>=0).

Parameters:
argument - The argument
name - The name of the argument
Throws:
IllegalArgumentException - If argument is negative (<0)

isNonPositive

public static void isNonPositive(int argument,
                                 String name)
Check that the argument is non-positive (<=0).

Parameters:
argument - The argument
name - The name of the argument
Throws:
IllegalArgumentException - If argument is positive (>0)

isNegative

public static void isNegative(int argument,
                              String name)
Check that the argument is negative (<0).

Parameters:
argument - The argument
name - The name of the argument
Throws:
IllegalArgumentException - If argument is non-negative (>=0)

isPositive

public static void isPositive(int argument,
                              String name)
Check that the argument is positive (>0).

Parameters:
argument - The argument
name - The name of the argument
Throws:
IllegalArgumentException - If argument is non-positive (<=0)

isNonNegative

public static void isNonNegative(long argument,
                                 String name)
Check that the argument is non-negative (>=0).

Parameters:
argument - The argument
name - The name of the argument
Throws:
IllegalArgumentException - If argument is negative (<0)

isNonPositive

public static void isNonPositive(long argument,
                                 String name)
Check that the argument is non-positive (<=0).

Parameters:
argument - The argument
name - The name of the argument
Throws:
IllegalArgumentException - If argument is positive (>0)

isNegative

public static void isNegative(long argument,
                              String name)
Check that the argument is negative (<0).

Parameters:
argument - The argument
name - The name of the argument
Throws:
IllegalArgumentException - If argument is non-negative (>=0)

isPositive

public static void isPositive(long argument,
                              String name)
Check that the argument is positive (>0).

Parameters:
argument - The argument
name - The name of the argument
Throws:
IllegalArgumentException - If argument is non-positive (<=0)

isNonNegative

public static void isNonNegative(double argument,
                                 String name)
Check that the argument is non-negative (>=0).

Parameters:
argument - The argument
name - The name of the argument
Throws:
IllegalArgumentException - If argument is negative (<0)

isNonPositive

public static void isNonPositive(double argument,
                                 String name)
Check that the argument is non-positive (<=0).

Parameters:
argument - The argument
name - The name of the argument
Throws:
IllegalArgumentException - If argument is positive (>0)

isNegative

public static void isNegative(double argument,
                              String name)
Check that the argument is negative (<0).

Parameters:
argument - The argument
name - The name of the argument
Throws:
IllegalArgumentException - If argument is non-negative (>=0)

isPositive

public static void isPositive(double argument,
                              String name)
Check that the argument is positive (>0).

Parameters:
argument - The argument
name - The name of the argument
Throws:
IllegalArgumentException - If argument is non-positive (<=0)

isNotNan

public static void isNotNan(double argument,
                            String name)
Check that the argument is not NaN.

Parameters:
argument - The argument
name - The name of the argument
Throws:
IllegalArgumentException - If argument is NaN

isNotZeroLength

public static void isNotZeroLength(String argument,
                                   String name)
Check that the string is non-null and has length > 0

Parameters:
argument - The argument
name - The name of the argument
Throws:
IllegalArgumentException - If value is null or length == 0

isNotEmpty

public static void isNotEmpty(String argument,
                              String name)
Check that the string is not empty, is not null, and does not contain only whitespace.

Parameters:
argument - String
name - The name of the argument
Throws:
IllegalArgumentException - If string is null or empty

isNotNull

public static void isNotNull(Object argument,
                             String name)
Check that the specified argument is non-null

Parameters:
argument - The argument
name - The name of the argument
Throws:
IllegalArgumentException - If argument is null

getNotNull

public static <T> T getNotNull(T argument,
                               String name)
Returns the specified argument if it is not null.

Type Parameters:
T -
Parameters:
argument - The argument
name - The name of the argument
Returns:
The argument
Throws:
IllegalArgumentException - If argument is null

isNull

public static void isNull(Object argument,
                          String name)
Check that the argument is null

Parameters:
argument - The argument
name - The name of the argument
Throws:
IllegalArgumentException - If value is non-null

isInstanceOf

public static void isInstanceOf(Object argument,
                                Class<?> expectedClass,
                                String name)
Check that the object is an instance of the specified Class

Parameters:
argument - Value
expectedClass - Class
name - The name of the argument
Throws:
IllegalArgumentException - If value is null

getInstanceOf

public static <C> C getInstanceOf(Object argument,
                                  Class<C> expectedClass,
                                  String name)
Checks that the object is an instance of the specified Class and then returns the object cast to the specified Class

Type Parameters:
C - the class type
Parameters:
argument - Value
expectedClass - Class
name - The name of the argument
Returns:
value cast to the specified Class
Throws:
IllegalArgumentException - If value is not an instance of theClass.

isSame

public static <T> void isSame(T argument,
                              String argumentName,
                              T object,
                              String objectName)
Asserts that the specified first object is the same as (==) the specified second object.

Type Parameters:
T -
Parameters:
argument - The argument to assert as the same as object.
argumentName - The name that will be used within the exception message for the argument should an exception be thrown
object - The object to assert as the same as argument.
objectName - The name that will be used within the exception message for object should an exception be thrown; if null and object is not null, object.toString() will be used.
Throws:
IllegalArgumentException - If the specified objects are not the same.

isNotSame

public static <T> void isNotSame(T argument,
                                 String argumentName,
                                 T object,
                                 String objectName)
Asserts that the specified first object is not the same as (==) the specified second object.

Type Parameters:
T -
Parameters:
argument - The argument to assert as not the same as object.
argumentName - The name that will be used within the exception message for the argument should an exception be thrown
object - The object to assert as not the same as argument.
objectName - The name that will be used within the exception message for object should an exception be thrown; if null and object is not null, object.toString() will be used.
Throws:
IllegalArgumentException - If the specified objects are the same.

isEquals

public static <T> void isEquals(T argument,
                                String argumentName,
                                T object,
                                String objectName)
Asserts that the specified first object is equal to the specified second object. This method does take null references into consideration.

Type Parameters:
T -
Parameters:
argument - The argument to assert equal to object.
argumentName - The name that will be used within the exception message for the argument should an exception be thrown
object - The object to assert as equal to argument.
objectName - The name that will be used within the exception message for object should an exception be thrown; if null and object is not null, object.toString() will be used.
Throws:
IllegalArgumentException - If the specified objects are not equal.

isNotEquals

public static <T> void isNotEquals(T argument,
                                   String argumentName,
                                   T object,
                                   String objectName)
Asserts that the specified first object is not equal to the specified second object. This method does take null references into consideration.

Type Parameters:
T -
Parameters:
argument - The argument to assert equal to object.
argumentName - The name that will be used within the exception message for the argument should an exception be thrown
object - The object to assert as equal to argument.
objectName - The name that will be used within the exception message for object should an exception be thrown; if null and object is not null, object.toString() will be used.
Throws:
IllegalArgumentException - If the specified objects are equals.

isNotEmpty

public static void isNotEmpty(Iterator<?> argument,
                              String name)
Checks that the iterator is not empty, and throws an exception if it is.

Parameters:
argument - the iterator to check
name - The name of the argument
Throws:
IllegalArgumentException - If iterator is empty (i.e., iterator.hasNext() returns false)

isNotEmpty

public static void isNotEmpty(Collection<?> argument,
                              String name)
Check that the collection is not empty

Parameters:
argument - Collection
name - The name of the argument
Throws:
IllegalArgumentException - If collection is null or empty

isNotEmpty

public static void isNotEmpty(Map<?,?> argument,
                              String name)
Check that the map is not empty

Parameters:
argument - Map
name - The name of the argument
Throws:
IllegalArgumentException - If map is null or empty

isEmpty

public static void isEmpty(Object[] argument,
                           String name)
Check that the array is empty

Parameters:
argument - Array
name - The name of the argument
Throws:
IllegalArgumentException - If array is not empty

isNotEmpty

public static void isNotEmpty(Object[] argument,
                              String name)
Check that the array is not empty

Parameters:
argument - Array
name - The name of the argument
Throws:
IllegalArgumentException - If array is null or empty

contains

public static void contains(Collection<?> argument,
                            Object value,
                            String name)
Check that the collection contains the value

Parameters:
argument - Collection to check
value - Value to check for, may be null
name - The name of the argument
Throws:
IllegalArgumentException - If collection is null or doesn't contain value

containsKey

public static void containsKey(Map<?,?> argument,
                               Object key,
                               String name)
Check that the map contains the key

Parameters:
argument - Map to check
key - Key to check for, may be null
name - The name of the argument
Throws:
IllegalArgumentException - If map is null or doesn't contain key

containsNoNulls

public static void containsNoNulls(Iterable<?> argument,
                                   String name)
Check that the collection is not null and contains no nulls

Parameters:
argument - Array
name - The name of the argument
Throws:
IllegalArgumentException - If array is null or has null values

containsNoNulls

public static void containsNoNulls(Object[] argument,
                                   String name)
Check that the array is not null and contains no nulls

Parameters:
argument - Array
name - The name of the argument
Throws:
IllegalArgumentException - If array is null or has null values

hasSizeOfAtLeast

public static void hasSizeOfAtLeast(Collection<?> argument,
                                    int minimumSize,
                                    String name)
Check that the collection contains at least the supplied number of elements

Parameters:
argument - Collection
minimumSize - the minimum size
name - The name of the argument
Throws:
IllegalArgumentException - If collection has a size smaller than the supplied value

hasSizeOfAtMost

public static void hasSizeOfAtMost(Collection<?> argument,
                                   int maximumSize,
                                   String name)
Check that the collection contains no more than the supplied number of elements

Parameters:
argument - Collection
maximumSize - the maximum size
name - The name of the argument
Throws:
IllegalArgumentException - If collection has a size smaller than the supplied value

hasSizeOfAtLeast

public static void hasSizeOfAtLeast(Map<?,?> argument,
                                    int minimumSize,
                                    String name)
Check that the map contains at least the supplied number of entries

Parameters:
argument - the map
minimumSize - the minimum size
name - The name of the argument
Throws:
IllegalArgumentException - If the map has a size smaller than the supplied value

hasSizeOfAtMost

public static void hasSizeOfAtMost(Map<?,?> argument,
                                   int maximumSize,
                                   String name)
Check that the map contains no more than the supplied number of entries

Parameters:
argument - the map
maximumSize - the maximum size
name - The name of the argument
Throws:
IllegalArgumentException - If the map has a size smaller than the supplied value

hasSizeOfAtLeast

public static void hasSizeOfAtLeast(Object[] argument,
                                    int minimumSize,
                                    String name)
Check that the array contains at least the supplied number of elements

Parameters:
argument - the array
minimumSize - the minimum size
name - The name of the argument
Throws:
IllegalArgumentException - If the array has a size smaller than the supplied value

hasSizeOfAtMost

public static void hasSizeOfAtMost(Object[] argument,
                                   int maximumSize,
                                   String name)
Check that the array contains no more than the supplied number of elements

Parameters:
argument - the array
maximumSize - the maximum size
name - The name of the argument
Throws:
IllegalArgumentException - If the array has a size smaller than the supplied value


Copyright © 2008-2009 JBoss, a division of Red Hat. All Rights Reserved.