net.kano.joscar
Class DefensiveTools

java.lang.Object
  extended bynet.kano.joscar.DefensiveTools

public final class DefensiveTools
extends java.lang.Object

A set of utilities for ensuring the validity (and non-nullness) of arguments passed into methods or constructors.


Method Summary
static void checkNull(java.lang.Object val, java.lang.String name)
          Ensures that the given value is not null.
static void checkNullElements(java.lang.Object[] array, java.lang.String arrayName)
          Ensures that no element of the given array is null.
static void checkNullElements(java.lang.Object[] array, java.lang.String arrayName, int offset, int len)
          Ensures that each of the given number of elements (starting at the given index) of the given array are not null.
static void checkRange(int val, java.lang.String name, int min)
          Ensures that the given value is greater than or equal to the given minimum value.
static void checkRange(int val, java.lang.String name, int min, int max)
          Ensures that the given value is inclusively between the given minumum and maximum values.
static void checkRange(long val, java.lang.String name, int min)
          Ensures that the given value is greater than or equal to the given minimum value.
static void checkRange(long val, java.lang.String name, long min, long max)
          Ensures that the given value is inclusively between the given minumum and maximum values.
static java.lang.Object[] getNonnullArray(java.lang.Object[] array, java.lang.String name)
          Returns a copy of the given array, ensuring that no element of the returned array is null.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

checkRange

public static final void checkRange(int val,
                                    java.lang.String name,
                                    int min)
                             throws java.lang.IllegalArgumentException
Ensures that the given value is greater than or equal to the given minimum value.

Parameters:
val - the value to check
name - the name of this variable, for debugging purposes
min - the minimum value of the given value
Throws:
java.lang.IllegalArgumentException - if the given value is less than the given minimum

checkRange

public static final void checkRange(int val,
                                    java.lang.String name,
                                    int min,
                                    int max)
                             throws java.lang.IllegalArgumentException
Ensures that the given value is inclusively between the given minumum and maximum values.

Parameters:
val - the value to check
name - the name of this variable, for debugging purposes
min - the minimum value of the given value
max - the maximum value of the given value
Throws:
java.lang.IllegalArgumentException - if the given value is less than the given minimum or greater than the given maximum

checkRange

public static void checkRange(long val,
                              java.lang.String name,
                              int min)
                       throws java.lang.IllegalArgumentException
Ensures that the given value is greater than or equal to the given minimum value.

Parameters:
val - the value to check
name - the name of this variable, for debugging purposes
min - the minimum value of the given value
Throws:
java.lang.IllegalArgumentException - if the given value is less than the given minimum

checkRange

public static void checkRange(long val,
                              java.lang.String name,
                              long min,
                              long max)
Ensures that the given value is inclusively between the given minumum and maximum values.

Parameters:
val - the value to check
name - the name of this variable, for debugging purposes
min - the minimum value of the given value
max - the maximum value of the given value
Throws:
java.lang.IllegalArgumentException - if the given value is less than the given minimum or greater than the given maximum

checkNull

public static final void checkNull(java.lang.Object val,
                                   java.lang.String name)
                            throws java.lang.IllegalArgumentException
Ensures that the given value is not null.

Parameters:
val - the value to check
name - the name of this variable, for debugging purposes
Throws:
java.lang.IllegalArgumentException - if the given value is null

checkNullElements

public static void checkNullElements(java.lang.Object[] array,
                                     java.lang.String arrayName)
                              throws java.lang.IllegalArgumentException
Ensures that no element of the given array is null.

Parameters:
array - an array of objects
arrayName - the name of this array, for debugging purposes
Throws:
java.lang.IllegalArgumentException - if an element of the given array is null

checkNullElements

public static void checkNullElements(java.lang.Object[] array,
                                     java.lang.String arrayName,
                                     int offset,
                                     int len)
                              throws java.lang.IllegalArgumentException
Ensures that each of the given number of elements (starting at the given index) of the given array are not null.

Parameters:
array - an array of objects
arrayName - the name of this array, for debugging purposes
offset - the index in the given array at which len elements must not be null
len - the number of elements, starting at the given index, which must not be null
Throws:
java.lang.IllegalArgumentException - if an element of the given array is null

getNonnullArray

public static java.lang.Object[] getNonnullArray(java.lang.Object[] array,
                                                 java.lang.String name)
                                          throws java.lang.IllegalArgumentException
Returns a copy of the given array, ensuring that no element of the returned array is null. If any elements of the given array are null, an IllegalArgumentException is thrown. Note that if the given array is null, this method will simply return null

Parameters:
array - the array to clone and check for null elements
name - the name of the array variable, for debugging purposes
Returns:
a copy of the given array without any null elements, or null if the given array is null
Throws:
java.lang.IllegalArgumentException - if any elements of the given array are null