public class Preconditions
extends java.lang.Object
| 构造器和说明 |
|---|
Preconditions() |
| 限定符和类型 | 方法和说明 |
|---|---|
static void |
checkArgument(boolean expression) |
static void |
checkArgument(boolean expression,
java.lang.Object errorMessage)
Ensures that an expression checking an argument is true.
|
static float |
checkArgumentFinite(float value,
java.lang.String valueName)
Ensures that the argument floating point value is a finite number.
|
static float |
checkArgumentInRange(float value,
float lower,
float upper,
java.lang.String valueName)
Ensures that the argument floating point value is within the inclusive range.
|
static int |
checkArgumentInRange(int value,
int lower,
int upper,
java.lang.String valueName)
Ensures that the argument int value is within the inclusive range.
|
static long |
checkArgumentInRange(long value,
long lower,
long upper,
java.lang.String valueName)
Ensures that the argument long value is within the inclusive range.
|
static int |
checkArgumentNonnegative(int value)
Ensures that that the argument numeric value is non-negative.
|
static int |
checkArgumentNonnegative(int value,
java.lang.String errorMessage)
Ensures that that the argument numeric value is non-negative.
|
static long |
checkArgumentNonnegative(long value)
Ensures that that the argument numeric value is non-negative.
|
static long |
checkArgumentNonnegative(long value,
java.lang.String errorMessage)
Ensures that that the argument numeric value is non-negative.
|
static int |
checkArgumentPositive(int value,
java.lang.String errorMessage)
Ensures that that the argument numeric value is positive.
|
static float[] |
checkArrayElementsInRange(float[] value,
float lower,
float upper,
java.lang.String valueName)
Ensures that all elements in the argument floating point array are within the inclusive range
While this can be used to range check against +/- infinity, note that all NaN numbers
will always be out of range.
|
static <T> T[] |
checkArrayElementsNotNull(T[] value,
java.lang.String valueName)
Ensures that the array is not
null, and none of its elements are null. |
static <C extends java.util.Collection<T>,T> |
checkCollectionElementsNotNull(C value,
java.lang.String valueName)
Ensures that the
Collection is not null, and none of its elements are
null. |
static <T> java.util.Collection<T> |
checkCollectionNotEmpty(java.util.Collection<T> value,
java.lang.String valueName)
Ensures that the
Collection is not null, and contains at least one element. |
static int |
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)
Ensures that an object reference passed as a parameter to the calling
method is not null.
|
static <T> T |
checkNotNull(T reference,
java.lang.Object errorMessage)
Ensures that an object reference passed as a parameter to 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 to the calling method.
|
static void |
checkState(boolean expression,
java.lang.String message)
Ensures the truth of an expression involving the state of the calling
instance, but not involving any parameters to the calling method.
|
static <T extends java.lang.CharSequence> |
checkStringNotEmpty(T string)
Ensures that an string reference passed as a parameter to the calling
method is not empty.
|
static <T extends java.lang.CharSequence> |
checkStringNotEmpty(T string,
java.lang.Object errorMessage)
Ensures that an string reference passed as a parameter to the calling
method is not empty.
|
public static void checkArgument(boolean expression)
public static void checkArgument(boolean expression,
java.lang.Object errorMessage)
expression - the expression to checkerrorMessage - the exception message to use if the check fails; will
be converted to a string using String.valueOf(Object)java.lang.IllegalArgumentException - if expression is false@NonNull public static <T extends java.lang.CharSequence> T checkStringNotEmpty(T string)
string - an string referencejava.lang.IllegalArgumentException - if string is empty@NonNull
public static <T extends java.lang.CharSequence> T checkStringNotEmpty(T string,
java.lang.Object errorMessage)
string - an string referenceerrorMessage - the exception message to use if the check fails; will
be converted to a string using String.valueOf(Object)java.lang.IllegalArgumentException - if string is empty@NonNull public static <T> T checkNotNull(T reference)
reference - an object referencejava.lang.NullPointerException - if reference is null@NonNull
public static <T> T checkNotNull(T reference,
java.lang.Object errorMessage)
reference - an object referenceerrorMessage - the exception message to use if the check fails; will
be converted to a string using String.valueOf(Object)java.lang.NullPointerException - if reference is nullpublic static void checkState(boolean expression,
java.lang.String message)
expression - a boolean expressionmessage - exception messagejava.lang.IllegalStateException - if expression is falsepublic static void checkState(boolean expression)
expression - a boolean expressionjava.lang.IllegalStateException - if expression is falsepublic static int checkFlagsArgument(int requestedFlags,
int allowedFlags)
public static int checkArgumentNonnegative(int value,
java.lang.String errorMessage)
value - a numeric int valueerrorMessage - the exception message to use if the check failsjava.lang.IllegalArgumentException - if value was negativepublic static int checkArgumentNonnegative(int value)
value - a numeric int valuejava.lang.IllegalArgumentException - if value was negativepublic static long checkArgumentNonnegative(long value)
value - a numeric long valuejava.lang.IllegalArgumentException - if value was negativepublic static long checkArgumentNonnegative(long value,
java.lang.String errorMessage)
value - a numeric long valueerrorMessage - the exception message to use if the check failsjava.lang.IllegalArgumentException - if value was negativepublic static int checkArgumentPositive(int value,
java.lang.String errorMessage)
value - a numeric int valueerrorMessage - the exception message to use if the check failsjava.lang.IllegalArgumentException - if value was not positivepublic static float checkArgumentFinite(float value,
java.lang.String valueName)
A finite number is defined to be both representable (that is, not NaN) and not infinite (that is neither positive or negative infinity).
value - a floating point valuevalueName - the name of the argument to use if the check failsjava.lang.IllegalArgumentException - if value was not finitepublic static float checkArgumentInRange(float value,
float lower,
float upper,
java.lang.String valueName)
While this can be used to range check against +/- infinity, note that all NaN numbers will always be out of range.
value - a floating point valuelower - the lower endpoint of the inclusive rangeupper - the upper endpoint of the inclusive rangevalueName - the name of the argument to use if the check failsjava.lang.IllegalArgumentException - if value was not within the rangepublic static int checkArgumentInRange(int value,
int lower,
int upper,
java.lang.String valueName)
value - a int valuelower - the lower endpoint of the inclusive rangeupper - the upper endpoint of the inclusive rangevalueName - the name of the argument to use if the check failsjava.lang.IllegalArgumentException - if value was not within the rangepublic static long checkArgumentInRange(long value,
long lower,
long upper,
java.lang.String valueName)
value - a long valuelower - the lower endpoint of the inclusive rangeupper - the upper endpoint of the inclusive rangevalueName - the name of the argument to use if the check failsjava.lang.IllegalArgumentException - if value was not within the rangepublic static <T> T[] checkArrayElementsNotNull(T[] value,
java.lang.String valueName)
null, and none of its elements are null.value - an array of boxed objectsvalueName - the name of the argument to use if the check failsjava.lang.NullPointerException - if the value or any of its elements were null@NonNull
public static <C extends java.util.Collection<T>,T> C checkCollectionElementsNotNull(C value,
java.lang.String valueName)
Collection is not null, and none of its elements are
null.value - a Collection of boxed objectsvalueName - the name of the argument to use if the check failsCollectionjava.lang.NullPointerException - if the value or any of its elements were nullpublic static <T> java.util.Collection<T> checkCollectionNotEmpty(java.util.Collection<T> value,
java.lang.String valueName)
Collection is not null, and contains at least one element.value - a Collection of boxed elements.valueName - the name of the argument to use if the check fails.Collectionjava.lang.NullPointerException - if the value was nulljava.lang.IllegalArgumentException - if the value was emptypublic static float[] checkArrayElementsInRange(float[] value,
float lower,
float upper,
java.lang.String valueName)
While this can be used to range check against +/- infinity, note that all NaN numbers will always be out of range.
value - a floating point array of valueslower - the lower endpoint of the inclusive rangeupper - the upper endpoint of the inclusive rangevalueName - the name of the argument to use if the check failsjava.lang.IllegalArgumentException - if any of the elements in value were out of rangejava.lang.NullPointerException - if the value was null