Package com.android.internal.util
Class Preconditions
- java.lang.Object
-
- com.android.internal.util.Preconditions
-
public class Preconditions extends Object
Simple static methods to be called at the start of your own methods to verify correct arguments and state.
-
-
Constructor Summary
Constructors Constructor Description Preconditions()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidcheckArgument(boolean expression)static floatcheckArgumentFinite(float value, String valueName)Ensures that the argument floating point value is a finite number.static floatcheckArgumentInRange(float value, float lower, float upper, String valueName)Ensures that the argument floating point value is within the inclusive range.static intcheckArgumentInRange(int value, int lower, int upper, String valueName)Ensures that the argument int value is within the inclusive range.static intcheckArgumentNonnegative(int value, String errorMessage)Ensures that that the argument numeric value is non-negative.static longcheckArgumentNonnegative(long value, String errorMessage)Ensures that that the argument numeric value is non-negative.static intcheckArgumentPositive(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 rangestatic <T> T[]checkArrayElementsNotNull(T[] value, String valueName)Ensures that the array is notnull, and none of its elements arenull.static <T> Collection<T>checkCollectionElementsNotNull(Collection<T> value, String valueName)static <T> Collection<T>checkCollectionNotEmpty(Collection<T> value, String valueName)Ensures that theCollectionis notnull, and contains at least one element.static voidcheckFlagsArgument(int requestedFlags, int allowedFlags)Check the requested flags, throwing if any requested flags are outside the allowed set.static <T> TcheckNotNull(T reference)Ensures that an object reference passed as a parameter to the calling method is not null.static <T> TcheckNotNull(T reference, Object errorMessage)Ensures that an object reference passed as a parameter to the calling method is not null.static voidcheckState(boolean expression)Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.
-
-
-
Method Detail
-
checkArgument
public static void checkArgument(boolean expression)
-
checkNotNull
public static <T> T checkNotNull(T reference)
Ensures that an object reference passed as a parameter to the calling method is not null.- Parameters:
reference- an object reference- Returns:
- the non-null reference that was validated
- Throws:
NullPointerException- ifreferenceis null
-
checkNotNull
public static <T> T checkNotNull(T reference, Object errorMessage)Ensures that an object reference passed as a parameter to the calling method is not null.- Parameters:
reference- an object referenceerrorMessage- the exception message to use if the check fails; will be converted to a string usingString.valueOf(Object)- Returns:
- the non-null reference that was validated
- Throws:
NullPointerException- ifreferenceis null
-
checkState
public 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.- Parameters:
expression- a boolean expression- Throws:
IllegalStateException- ifexpressionis false
-
checkFlagsArgument
public static void checkFlagsArgument(int requestedFlags, int allowedFlags)Check the requested flags, throwing if any requested flags are outside the allowed set.
-
checkArgumentNonnegative
public static int checkArgumentNonnegative(int value, String errorMessage)Ensures that that the argument numeric value is non-negative.- Parameters:
value- a numeric int valueerrorMessage- the exception message to use if the check fails- Returns:
- the validated numeric value
- Throws:
IllegalArgumentException- ifvaluewas negative
-
checkArgumentNonnegative
public static long checkArgumentNonnegative(long value, String errorMessage)Ensures that that the argument numeric value is non-negative.- Parameters:
value- a numeric long valueerrorMessage- the exception message to use if the check fails- Returns:
- the validated numeric value
- Throws:
IllegalArgumentException- ifvaluewas negative
-
checkArgumentPositive
public static int checkArgumentPositive(int value, String errorMessage)Ensures that that the argument numeric value is positive.- Parameters:
value- a numeric int valueerrorMessage- the exception message to use if the check fails- Returns:
- the validated numeric value
- Throws:
IllegalArgumentException- ifvaluewas not positive
-
checkArgumentFinite
public static float checkArgumentFinite(float value, String valueName)Ensures that the argument floating point value is a finite number.A finite number is defined to be both representable (that is, not NaN) and not infinite (that is neither positive or negative infinity).
- Parameters:
value- a floating point valuevalueName- the name of the argument to use if the check fails- Returns:
- the validated floating point value
- Throws:
IllegalArgumentException- ifvaluewas not finite
-
checkArgumentInRange
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 to range check against +/- infinity, note that all NaN numbers will always be out of range.
- Parameters:
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 fails- Returns:
- the validated floating point value
- Throws:
IllegalArgumentException- ifvaluewas not within the range
-
checkArgumentInRange
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 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 fails- Returns:
- the validated int value
- Throws:
IllegalArgumentException- ifvaluewas not within the range
-
checkArrayElementsNotNull
public static <T> T[] checkArrayElementsNotNull(T[] value, String valueName)Ensures that the array is notnull, and none of its elements arenull.- Parameters:
value- an array of boxed objectsvalueName- the name of the argument to use if the check fails- Returns:
- the validated array
- Throws:
NullPointerException- if thevalueor any of its elements werenull
-
checkCollectionElementsNotNull
public static <T> Collection<T> checkCollectionElementsNotNull(Collection<T> value, String valueName)
- Parameters:
value- aCollectionof boxed objectsvalueName- the name of the argument to use if the check fails- Returns:
- the validated
Collection - Throws:
NullPointerException- if thevalueor any of its elements werenull
-
checkCollectionNotEmpty
public static <T> Collection<T> checkCollectionNotEmpty(Collection<T> value, String valueName)
Ensures that theCollectionis notnull, and contains at least one element.- Parameters:
value- aCollectionof boxed elements.valueName- the name of the argument to use if the check fails.- Returns:
- the validated
Collection - Throws:
NullPointerException- if thevaluewasnullIllegalArgumentException- if thevaluewas empty
-
checkArrayElementsInRange
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 rangeWhile this can be used to range check against +/- infinity, note that all NaN numbers will always be out of range.
- Parameters:
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 fails- Returns:
- the validated floating point value
- Throws:
IllegalArgumentException- if any of the elements invaluewere out of rangeNullPointerException- if thevaluewasnull
-
-