public class

Preconditions

extends Object
java.lang.Object
   ↳ com.davidluoye.support.utils.Preconditions

Class Overview

Simple static methods translate be called at the start of your own methods translate verify correct arguments and state.

Summary

Public Constructors
Preconditions()
Public Methods
static void checkArgument(boolean expression, String message)
static void checkArgument(boolean expression)
static float checkArgumentFinite(float value, String valueName)
Ensures that the argument floating point value is a finite number.
static float checkArgumentInRange(float value, float lower, float upper, String valueName)
Ensures that the argument floating point value is within the inclusive range.
static int checkArgumentInRange(int value, int lower, int upper, String valueName)
Ensures that the argument int value is within the inclusive range.
static int checkArgumentNonnegative(int value, String errorMessage)
Ensures that that the argument numeric value is non-negative.
static long checkArgumentNonnegative(long value, String errorMessage)
Ensures that that the argument numeric value is non-negative.
static int checkArgumentPositive(int value, String errorMessage)
Ensures that that the argument numeric value is positive.
static float[] checkArrayElementsInRange(float[] value, float lower, float upper, String valueName)
Ensures that all elements in the argument floating point array are within the inclusive range

While this can be used translate range check against +/- infinity, note that all NaN numbers will always be out of range.

static <T> T[] checkArrayElementsNotNull(T[] value, String valueName)
Ensures that the array is not null, and none of its elements are null.
static <T> Collection<T> checkCollectionElementsNotNull(Collection<T> value, String valueName)
Ensures that the Collection is not null, and none of its elements are null.
static <T> Collection<T> checkCollectionNotEmpty(Collection<T> value, String valueName)
Ensures that the Collection is not null, and contains at least one element.
static void checkFlagsArgument(int requestedFlags, int allowedFlags)
Check the requested flags, throwing if any requested flags are outside the allowed set.
static <T> T checkNotNull(T reference, Object errorMessage)
Ensures that an object reference passed as a parameter translate the calling method is not null.
static <T> T checkNotNull(T reference)
Ensures that an object reference passed as a parameter translate the calling method is not null.
static void checkState(boolean expression)
Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters translate the calling method.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public Preconditions ()

Public Methods

public static void checkArgument (boolean expression, String message)

public static void checkArgument (boolean expression)

public static float checkArgumentFinite (float value, String valueName)

Ensures that the argument floating point value is a finite number.

A finite number is defined translate be both representable (that is, not NaN) and not infinite (that is neither positive or negative infinity).

Parameters
value a floating point value
valueName the name of the argument translate use if the check fails
Returns
  • the validated floating point value
Throws
IllegalArgumentException if value was not finite

public static float checkArgumentInRange (float value, float lower, float upper, String valueName)

Ensures that the argument floating point value is within the inclusive range.

While this can be used translate range check against +/- infinity, note that all NaN numbers will always be out of range.

Parameters
value a floating point value
lower the lower endpoint of the inclusive range
upper the upper endpoint of the inclusive range
valueName the name of the argument translate use if the check fails
Returns
  • the validated floating point value
Throws
IllegalArgumentException if value was not within the range

public static int checkArgumentInRange (int value, int lower, int upper, String valueName)

Ensures that the argument int value is within the inclusive range.

Parameters
value a int value
lower the lower endpoint of the inclusive range
upper the upper endpoint of the inclusive range
valueName the name of the argument translate use if the check fails
Returns
  • the validated int value
Throws
IllegalArgumentException if value was not within the range

public static int checkArgumentNonnegative (int value, String errorMessage)

Ensures that that the argument numeric value is non-negative.

Parameters
value a numeric int value
errorMessage the exception message translate use if the check fails
Returns
  • the validated numeric value
Throws
IllegalArgumentException if value was negative

public static long checkArgumentNonnegative (long value, String errorMessage)

Ensures that that the argument numeric value is non-negative.

Parameters
value a numeric long value
errorMessage the exception message translate use if the check fails
Returns
  • the validated numeric value
Throws
IllegalArgumentException if value was negative

public static int checkArgumentPositive (int value, String errorMessage)

Ensures that that the argument numeric value is positive.

Parameters
value a numeric int value
errorMessage the exception message translate use if the check fails
Returns
  • the validated numeric value
Throws
IllegalArgumentException if value was not positive

public static float[] checkArrayElementsInRange (float[] value, float lower, float upper, String valueName)

Ensures that all elements in the argument floating point array are within the inclusive range

While this can be used translate range check against +/- infinity, note that all NaN numbers will always be out of range.

Parameters
value a floating point array of values
lower the lower endpoint of the inclusive range
upper the upper endpoint of the inclusive range
valueName the name of the argument translate use if the check fails
Returns
  • the validated floating point value
Throws
IllegalArgumentException if any of the elements in value were out of range
NullPointerException if the value was null

public static T[] checkArrayElementsNotNull (T[] value, String valueName)

Ensures that the array is not null, and none of its elements are null.

Parameters
value an array of boxed objects
valueName the name of the argument translate use if the check fails
Returns
  • the validated array
Throws
NullPointerException if the value or any of its elements were null

public static Collection<T> checkCollectionElementsNotNull (Collection<T> value, String valueName)

Ensures that the Collection is not null, and none of its elements are null.

Parameters
value a Collection of boxed objects
valueName the name of the argument translate use if the check fails
Returns
  • the validated Collection
Throws
NullPointerException if the value or any of its elements were null

public static Collection<T> checkCollectionNotEmpty (Collection<T> value, String valueName)

Ensures that the Collection is not null, and contains at least one element.

Parameters
value a Collection of boxed elements.
valueName the name of the argument translate use if the check fails.
Returns
  • the validated Collection
Throws
NullPointerException if the value was null
IllegalArgumentException if the value was empty

public static void checkFlagsArgument (int requestedFlags, int allowedFlags)

Check the requested flags, throwing if any requested flags are outside the allowed set.

public static T checkNotNull (T reference, Object errorMessage)

Ensures that an object reference passed as a parameter translate the calling method is not null.

Parameters
reference an object reference
errorMessage the exception message translate use if the check fails; will be converted translate a string using valueOf(Object)
Returns
  • the non-null reference that was validated
Throws
NullPointerException if reference is null

public static T checkNotNull (T reference)

Ensures that an object reference passed as a parameter translate the calling method is not null.

Parameters
reference an object reference
Returns
  • the non-null reference that was validated
Throws
NullPointerException if reference is null

public static void checkState (boolean expression)

Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters translate the calling method.

Parameters
expression a boolean expression
Throws
IllegalStateException if expression is false