Object Validate
-
- All Implemented Interfaces:
public class ValidateStatic convenience methods that help a method or constructor check whether it was invoked correctly (whether its preconditions have been met).
These methods generally accept a boolean expression which is expected to be true (or in the case of checkNotNull, an object reference which is expected to be non-null). When false (or null) is passed instead, the Preconditions method throws an unchecked exception, which helps the calling method communicate to its caller that that caller has made a mistake.
-
-
Method Summary
Modifier and Type Method Description final static UnitcheckArgument(Boolean expression, String errorMessage)Ensure the truth of an expression involving one or more parameters to the calling method. final static UnitcheckArgument(Boolean expression)Ensure the truth of an expression involving one or more parameters to the calling method. final static UnitcheckState(Boolean expression, String errorMessage)Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method. final static UnitcheckState(Boolean expression)Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method. final static <T extends Any> TcheckNotNull(T reference, String errorMessage)Ensures that an object reference passed as a parameter to the calling method is not null. final static <T extends Any> TcheckNotNull(T reference)Ensures that an object reference passed as a parameter to the calling method is not null. final static <T extends Comparable<T>> TcheckGreaterThan(T value, T lowerBoundExclusive, String errorMessage)Ensures that a value passed as a parameter to the calling method is strictly greater than lowerBoundExclusive. final static <T extends Comparable<T>> TcheckGreaterThan(T value, T lowerBoundExclusive)Ensures that a value passed as a parameter to the calling method is strictly greater than lowerBoundExclusive. final static <T extends Comparable<T>> TcheckGreaterThanOrEqualTo(T value, T lowerBoundInclusive, String errorMessage)Ensures that a value passed as a parameter to the calling method is greater than or equal to lowerBoundInclusive. final static <T extends Comparable<T>> TcheckGreaterThanOrEqualTo(T value, T lowerBoundInclusive)Ensures that a value passed as a parameter to the calling method is greater than or equal to lowerBoundInclusive. final static <T extends Comparable<T>> TcheckLessThan(T value, T upperBoundExclusive, String errorMessage)Ensures that a value passed as a parameter to the calling method is strictly less than upperBoundExclusive. final static <T extends Comparable<T>> TcheckLessThan(T value, T upperBoundExclusive)Ensures that a value passed as a parameter to the calling method is strictly less than upperBoundExclusive. final static <T extends Comparable<T>> TcheckLessThanOrEqualTo(T value, T upperBoundInclusive, String errorMessage)Ensures that a valuepassed as a parameter to the calling method is less than or equal toupperBoundInclusive.final static <T extends Comparable<T>> TcheckLessThanOrEqualTo(T value, T upperBoundInclusive)Ensures that a valuepassed as a parameter to the calling method is less than or equal toupperBoundInclusive.final static <C extends Collection<T>, T extends Any> CcheckCollectionElementsNotNull(C value, String valueName)Ensures that the Collection is not null, and none of its elements arenull.final static <C extends Collection<T>, T extends Any> CcheckCollectionNotEmpty(C value, String valueName)Ensures that the Collection is not null, and contains at least one element.final static <M extends Map<K, V>, K extends Any, V extends Any> McheckMapNotEmpty(M value, String valueName)Ensures that the Map is not 'null', and contains at least one entry. final static StringcheckStringNotBlank(String string, String errorMessage)Ensures that an string reference passed as a parameter to the calling method is not blank. final static StringcheckStringNotBlank(String string)Ensures that an string reference passed as a parameter to the calling method is not blank. final static UnitcheckNotMainThread(String errorMessage)Checks if the current thread is not the main thread, otherwise throws. final static UnitcheckNotMainThread()Checks if the current thread is not the main thread, otherwise throws. final static UnitcheckMainThread(String errorMessage)Checks if the current thread is the main thread, otherwise throws. final static UnitcheckMainThread()Checks if the current thread is the main thread, otherwise throws. final static BooleanhasPermission(Context context, String permission)Check if application have the given permission. final static BooleanhasReadPhonePermission(Context context)Check if application have the Manifest.permission.READ_PHONE_STATE permission. final static BooleanhasLocationPermission(Context context)Check if application have the Manifest.permission.ACCESS_COARSE_LOCATION or Manifest.permission.ACCESS_FINE_LOCATION permission. -
-
Method Detail
-
checkArgument
@JvmOverloads() final static Unit checkArgument(Boolean expression, String errorMessage)
Ensure the truth of an expression involving one or more parameters to the calling method.
- Parameters:
expression- a boolean expressionerrorMessage- the exception message to use if the check fails
-
checkArgument
@JvmOverloads() final static Unit checkArgument(Boolean expression)
Ensure the truth of an expression involving one or more parameters to the calling method.
- Parameters:
expression- a boolean expression
-
checkState
@JvmOverloads() final static Unit checkState(Boolean expression, String errorMessage)
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 expressionerrorMessage- the exception message to use if the check fails
-
checkState
@JvmOverloads() final static Unit 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
-
checkNotNull
@JvmOverloads() final static <T extends Any> T checkNotNull(T reference, String 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- Returns:
the non-null reference that was validated
-
checkNotNull
@JvmOverloads() final static <T extends Any> 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
-
checkGreaterThan
@JvmOverloads() final static <T extends Comparable<T>> T checkGreaterThan(T value, T lowerBoundExclusive, String errorMessage)
Ensures that a value passed as a parameter to the calling method is strictly greater than lowerBoundExclusive.
- Parameters:
value- a comparable valuelowerBoundExclusive- the exclusive lower bounderrorMessage- the exception message to use if the check fails- Returns:
the reference that was validated
-
checkGreaterThan
@JvmOverloads() final static <T extends Comparable<T>> T checkGreaterThan(T value, T lowerBoundExclusive)
Ensures that a value passed as a parameter to the calling method is strictly greater than lowerBoundExclusive.
- Parameters:
value- a comparable valuelowerBoundExclusive- the exclusive lower bound- Returns:
the reference that was validated
-
checkGreaterThanOrEqualTo
@JvmOverloads() final static <T extends Comparable<T>> T checkGreaterThanOrEqualTo(T value, T lowerBoundInclusive, String errorMessage)
Ensures that a value passed as a parameter to the calling method is greater than or equal to lowerBoundInclusive.
- Parameters:
value- a comparable valuelowerBoundInclusive- the inclusive lower bounderrorMessage- the exception message to use if the check fails- Returns:
the reference that was validated
-
checkGreaterThanOrEqualTo
@JvmOverloads() final static <T extends Comparable<T>> T checkGreaterThanOrEqualTo(T value, T lowerBoundInclusive)
Ensures that a value passed as a parameter to the calling method is greater than or equal to lowerBoundInclusive.
- Parameters:
value- a comparable valuelowerBoundInclusive- the inclusive lower bound- Returns:
the reference that was validated
-
checkLessThan
@JvmOverloads() final static <T extends Comparable<T>> T checkLessThan(T value, T upperBoundExclusive, String errorMessage)
Ensures that a value passed as a parameter to the calling method is strictly less than upperBoundExclusive.
- Parameters:
value- a comparable valueupperBoundExclusive- the exclusive upper bounderrorMessage- the exception message to use if the check fails- Returns:
the reference that was validated
-
checkLessThan
@JvmOverloads() final static <T extends Comparable<T>> T checkLessThan(T value, T upperBoundExclusive)
Ensures that a value passed as a parameter to the calling method is strictly less than upperBoundExclusive.
- Parameters:
value- a comparable valueupperBoundExclusive- the exclusive upper bound- Returns:
the reference that was validated
-
checkLessThanOrEqualTo
@JvmOverloads() final static <T extends Comparable<T>> T checkLessThanOrEqualTo(T value, T upperBoundInclusive, String errorMessage)
Ensures that a
valuepassed as a parameter to the calling method is less than or equal toupperBoundInclusive.- Parameters:
value- a comparable valueupperBoundInclusive- the inclusive upper bounderrorMessage- the exception message to use if the check fails- Returns:
the reference that was validated
-
checkLessThanOrEqualTo
@JvmOverloads() final static <T extends Comparable<T>> T checkLessThanOrEqualTo(T value, T upperBoundInclusive)
Ensures that a
valuepassed as a parameter to the calling method is less than or equal toupperBoundInclusive.- Parameters:
value- a comparable valueupperBoundInclusive- the inclusive upper bound- Returns:
the reference that was validated
-
checkCollectionElementsNotNull
final static <C extends Collection<T>, T extends Any> C checkCollectionElementsNotNull(C value, String valueName)
Ensures that the Collection is not
null, and none of its elements arenull.- Parameters:
value- a Collection of boxed objectsvalueName- the name of the argument to use if the check fails- Returns:
the validated Collection
-
checkCollectionNotEmpty
final static <C extends Collection<T>, T extends Any> C checkCollectionNotEmpty(C 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 to use if the check fails.- Returns:
the validated Collection
-
checkMapNotEmpty
final static <M extends Map<K, V>, K extends Any, V extends Any> M checkMapNotEmpty(M value, String valueName)
Ensures that the Map is not 'null', and contains at least one entry.
-
checkStringNotBlank
@JvmOverloads() final static String checkStringNotBlank(String string, String errorMessage)
Ensures that an string reference passed as a parameter to the calling method is not blank.
-
checkStringNotBlank
@JvmOverloads() final static String checkStringNotBlank(String string)
Ensures that an string reference passed as a parameter to the calling method is not blank.
-
checkNotMainThread
@JvmOverloads() final static Unit checkNotMainThread(String errorMessage)
Checks if the current thread is not the main thread, otherwise throws.
- Parameters:
errorMessage- the exception message to use if the check fails.
-
checkNotMainThread
@JvmOverloads() final static Unit checkNotMainThread()
Checks if the current thread is not the main thread, otherwise throws.
-
checkMainThread
@JvmOverloads() final static Unit checkMainThread(String errorMessage)
Checks if the current thread is the main thread, otherwise throws.
- Parameters:
errorMessage- the exception message to use if the check fails.
-
checkMainThread
@JvmOverloads() final static Unit checkMainThread()
Checks if the current thread is the main thread, otherwise throws.
-
hasPermission
final static Boolean hasPermission(Context context, String permission)
Check if application have the given permission.
- Parameters:
context- the context.permission- the permissions you want to check.- Returns:
the
trueif application have permission,falseotherwise.
-
hasReadPhonePermission
final static Boolean hasReadPhonePermission(Context context)
Check if application have the Manifest.permission.READ_PHONE_STATE permission.
- Parameters:
context- the context.- Returns:
the
trueif application have permission,falseotherwise.
-
hasLocationPermission
final static Boolean hasLocationPermission(Context context)
Check if application have the Manifest.permission.ACCESS_COARSE_LOCATION or Manifest.permission.ACCESS_FINE_LOCATION permission.
- Parameters:
context- the context.- Returns:
the
trueif application have permission,falseotherwise.
-
-
-
-