Object Validate

  • All Implemented Interfaces:

    
    public class Validate
    
                        

    Static 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.

    • Constructor Detail

    • 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 expression
        errorMessage - 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 expression
        errorMessage - 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 reference
        errorMessage - 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 value
        lowerBoundExclusive - the exclusive lower bound
        errorMessage - 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 value
        lowerBoundExclusive - 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 value
        lowerBoundInclusive - the inclusive lower bound
        errorMessage - 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 value
        lowerBoundInclusive - 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 value
        upperBoundExclusive - the exclusive upper bound
        errorMessage - 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 value
        upperBoundExclusive - 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 value passed as a parameter to the calling method is less than or equal to upperBoundInclusive.

        Parameters:
        value - a comparable value
        upperBoundInclusive - the inclusive upper bound
        errorMessage - 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 value passed as a parameter to the calling method is less than or equal to upperBoundInclusive.

        Parameters:
        value - a comparable value
        upperBoundInclusive - the inclusive upper bound
        Returns:

        the reference that was validated

      • 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.

        Parameters:
        value - a Map.
        valueName - the name of the argument to use if the check fails.
        Returns:

        the validated Map

      • 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.

        Parameters:
        string - a String value
        errorMessage - the exception message to use if the check fails
        Returns:

        the validated String

      • 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.
      • 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.
      • 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 true if application have permission, false otherwise.