Class Checks

java.lang.Object
com.mastfrog.util.preconditions.Checks

public final class Checks extends Object
Code sanity checks, to simplify things like null checks. Use these where applicable instead of directly throwing exceptions. That way, expensive tests can be switched to use assertions for production.
Author:
Tim Boudreau
  • Method Details

    • notSame

      public static void notSame(String a, String b, Object oa, Object ob)
    • isDigits

      public static String isDigits(String name, String value)
      Determine if a string value contains only digits.
      Parameters:
      name -
      value -
      Returns:
    • canCastToInt

      public static long canCastToInt(String name, long value)
    • atLeastOneNotNull

      public static void atLeastOneNotNull(String msg, Object... objects)
    • notNull

      public static <T> T notNull(String name, T val)
      Determine that the passed parameter is not null
      Parameters:
      name - The name of the parameter
      val - The value of the parameter
      Throws:
      NullPointerException - if the name is null
      NullArgumentException - if the value is null
    • nonNegative

      public static <T extends Number> T nonNegative(String name, T val)
      Determine that the passed argument is not a negative number
      Parameters:
      name - The name of the argument
      val - The value
      Throws:
      InvalidArgumentException - if the number is negative
    • nonNegative

      public static int nonNegative(String name, int val)
      Determine that the passed argument is not a negative number
      Parameters:
      name - The name of the argument
      val - The value
      Throws:
      InvalidArgumentException - if the number is negative
    • nonNegative

      public static long nonNegative(String name, long val)
      Determine that the passed argument is not a negative number.
      Parameters:
      name - The name of the argument
      val - The value
      Throws:
      InvalidArgumentException - if the number is negative
    • greaterThanOne

      public static <T extends Number> T greaterThanOne(String name, T val)
      Determine that the passed argument is greater than one.
      Parameters:
      name - The name of the argument
      val - The value
      Throws:
      InvalidArgumentException - if the number is negative
    • greaterThanOne

      public static int greaterThanOne(String name, int val)
      Determine that the passed argument is greater than one.
      Parameters:
      name - The name of the argument
      val - The value
      Throws:
      InvalidArgumentException - if the number is negative
    • greaterThanOne

      public static long greaterThanOne(String name, long val)
      Determine that the passed argument is greater than one.
      Parameters:
      name - The name of the argument
      val - The value
      Throws:
      InvalidArgumentException - if the number is negative
    • greaterThanZero

      public static <T extends Number> T greaterThanZero(String name, T val)
      Determine that the passed argument >=0.
      Parameters:
      name - The name of the argument
      val - The value
      Throws:
      InvalidArgumentException - if the number is negative
    • greaterThanZero

      public static int greaterThanZero(String name, int val)
      Determine that the passed argument >=0.
      Parameters:
      name - The name of the argument
      val - The value
      Throws:
      InvalidArgumentException - if the number is negative
    • greaterThanZero

      public static long greaterThanZero(String name, long val)
      Determine that the passed argument >=1.
      Parameters:
      name - The name of the argument
      val - The value
      Throws:
      InvalidArgumentException - if the number is negative
    • notEmptyOrNull

      public static <T> T notEmptyOrNull(String name, T array)
      Determine that the passed array argument
      • is an array
      • is not null
      • has a length > 0
      Parameters:
      name - The parameter name for use in an exception
      array - An array
      Throws:
      InvalidArgumentException - if array is not an array
      InvalidArgumentException
    • noNullElements

      @SafeVarargs public static <T> T[] noNullElements(String name, T... arr)
      Verify that an array does not contain null elements, throwing an exception if it does. Do not use for primitive array types - they cannot contain nulls anyway
      Parameters:
      name - The name of the parameter
      arr - An array of some sort
      Throws:
      InvalidArgumentException - if the array has null elements, or if array is an array of primitive types (these cannot contain null values and do not need such checking)
      NullArgumentException - if the passed array is null
    • nonZero

      public static <T extends Number> T nonZero(String name, T val)
      Throws an exception if a parameter is equal to zero.
      Parameters:
      name - The name of the parameter
      val - A number
      Throws:
      InvalidArgumentException - if the value of the number equals 0
    • nonZero

      public static int nonZero(String name, int val)
      Throws an exception if a parameter is equal to zero.
      Parameters:
      name - The name of the parameter
      val - A number
      Throws:
      InvalidArgumentException - if the value of the number equals 0
    • nonZero

      public static long nonZero(String name, long val)
      Throws an exception if a parameter is equal to zero.
      Parameters:
      name - The name of the parameter
      val - A number
      Throws:
      InvalidArgumentException - if the value of the number equals 0
    • notEmpty

      public static <T extends CharSequence> T notEmpty(String name, T value)
      Throws an exception if the passed string is empty
      Parameters:
      name - The name of the string used in the exception
      value - A string value
      Throws:
      InvalidArgumentException - if the String is empty, but not if it is null
    • notEmpty

      public static void notEmpty(String name, Collection<?> collection)
    • notNullOrEmpty

      public static <T extends CharSequence> T notNullOrEmpty(String name, T value)
      Throws an exception if the passed string is null or empty
      Parameters:
      name - The parameter name for use in the exception message
      value - The value
      Throws:
      InvalidArgumentException - if the value is null or "".equals(value)
      NullArgumentException - if the passed value is null
    • mayNotContain

      public static void mayNotContain(String name, CharSequence value, char... chars)
      Throws an exception if the passed string contains any of the passed characters
      Parameters:
      name - The parameter name for use in the exception message
      value - The value (may be null - use notNull(name, value) for null checks
      chars - An array of characters
      Throws:
      InvalidArgumentException - if the value contains any of the passed characters, or if the passed character array length is 0
      NullArgumentException - if the passed char array is null
    • mustContain

      public static void mustContain(String name, CharSequence value, char c)
      Throws an exception if the passed value does not contain at least one of the passed character
      Parameters:
      name - The parameter name for constructing the exception message
      value - A value
      c - A character which must be present
    • noDuplicates

      public static void noDuplicates(String name, Collection<?> collection)
      Throws an exception if the passed set contains duplicate elements. Note: do not use this method on very large collections or lazily resolved collections, as it will trigger resolving and iterating the entire collection.
      Parameters:
      name - The parameter name for constructing the exception message
      collection - A collection of something
    • mayNotStartWith

      public static void mayNotStartWith(String name, CharSequence value, char c)
      Throws an exception if the passed string starts with the passed character
      Parameters:
      name - The parameter name for constructing the exception message
      value - a string or equivalent. May be null, use notNull() for null checks.
      Throws:
      InvalidArgumentException - if the passed value starts with the passed character
    • isInstance

      public static void isInstance(String name, Class<?> type, Object value)
      Determine if an object is an instance of a given type
      Parameters:
      name - The parameter name for constructing the exception message
      type - The type sought
      value - The object
      Throws:
      ClassCastException - if the passed object is of the wrong type
    • isOfLength

      public static void isOfLength(String name, int length, Object[] value)
      Check that the given argument is of the given length, throw an InvalidArgumentException if that is not the case.
      Parameters:
      name - The parameter name for constructing the exception message
      length - The required length of the given value
      value - The value to check the length of
      Throws:
      InvalidArgumentException - if the length of the passed in value is not equal to the specified required length
      NullPointerException - if the name is null
      NullArgumentException - if the value is null
    • fileExists

      public static void fileExists(File file)
      Test that file exists and is a file not a folder.
      Parameters:
      file - The file
    • exists

      public static Path exists(Path file)
      Asserts that a file must exist.
      Parameters:
      file - The file
    • exists

      public static Path exists(String paramName, Path file)
      Asserts that a file must exist.
      Parameters:
      paramName - the parameter name
      file - The file
    • readable

      public static Path readable(Path file)
      Test that file.
      Parameters:
      file - The file
      Returns:
      the file
    • readable

      public static Path readable(String paramName, Path file)
      Test that a file exists and is readable.
      Parameters:
      paramName - The parameter name
      file - The file
      Returns:
      the path
    • regularFile

      public static Path regularFile(Path file)
      Assert that a file exists and is a regular file.
      Parameters:
      file - The path
      Returns:
      The file
    • regularFile

      public static Path regularFile(Path file, LinkOption... opts)
      Assert that a file exists and is a regular file.
      Parameters:
      file - The path
      opts - link options for Files.isRegularFile()
      Returns:
      The file
    • regularFile

      public static Path regularFile(String paramName, Path file, LinkOption... opts)
      Assert that a file exists and is a regular file.
      Parameters:
      paramName - the parameter name
      file - The path
      opts - link options for Files.isRegularFile()
      Returns:
      The file
    • writable

      public static Path writable(Path file)
      Test that file.
      Parameters:
      file - The file
    • writable

      public static Path writable(String paramName, Path file)
    • executable

      public static Path executable(Path file)
      Test that file.
      Parameters:
      file - The file
    • executable

      public static Path executable(String paramName, Path file)
    • fileExists

      public static Path fileExists(Path file)
    • fileExists

      public static Path fileExists(String paramName, Path file)
      Test that file exists and is a file not a folder.
      Parameters:
      file - The file
    • folderExists

      public static Path folderExists(String paramName, Path file)
      Test that file exists and is a file not a folder.
      Parameters:
      file - The file
    • fileExists

      public static File fileExists(String paramName, File file)
      Test that file exists and is a file not a folder.
      Parameters:
      file - The file
      paramName - The name of the method parameter
    • folderExists

      public static void folderExists(File file)
      Test that a file exists and is a folder.
      Parameters:
      file - The file
    • folderExists

      public static File folderExists(String paramName, File file)
      Test that a file exists and is a folder.
      Parameters:
      file - The file
    • readable

      public static void readable(File file)
      Test that a file exists and the current user has read permission.
      Parameters:
      file - A file
    • readable

      public static File readable(String paramName, File file)
      Test that a file exists and the current user has read permission.
      Parameters:
      file - A file
    • readableAndNonZeroLength

      public static File readableAndNonZeroLength(String paramName, File file)
      Test that a file exists and the current user has read permission.
      Parameters:
      file - A file
    • encodable

      public static void encodable(CharSequence seq, Charset charset)
      Test that a character sequence is encodable in the passe charset.
      Parameters:
      seq - A string
      charset - A character set