Class Assert

java.lang.Object
org.wildfly.common.Assert

public final class Assert extends Object
A set of assertions and checks.
Author:
David M. Lloyd
  • Method Details

    • checkNotNullParam

      @NotNull public static <T> T checkNotNullParam(String name, T value) throws IllegalArgumentException
      Check that the named parameter is not null. Use a standard exception message if it is.
      Type Parameters:
      T - the value type
      Parameters:
      name - the parameter name
      value - the parameter value
      Returns:
      the value that was passed in
      Throws:
      IllegalArgumentException - if the value is null
    • checkNotNullParamWithNullPointerException

      @NotNull public static <T> T checkNotNullParamWithNullPointerException(String name, T value) throws NullPointerException
      Check that the named parameter is not null, using a NullPointerException as some specifications require. Use a standard exception message if it is.
      Type Parameters:
      T - the value type
      Parameters:
      name - the parameter name
      value - the parameter value
      Returns:
      the value that was passed in
      Throws:
      NullPointerException - if the value is null
    • checkNotNullArrayParam

      @NotNull public static <T> T checkNotNullArrayParam(String name, int index, T value) throws IllegalArgumentException
      Check that a value within the named array parameter is not null. Use a standard exception message if it is.
      Type Parameters:
      T - the element value type
      Parameters:
      name - the parameter name
      index - the array index
      value - the array element value
      Returns:
      the array element value that was passed in
      Throws:
      IllegalArgumentException - if the value is null
    • checkNotEmptyParam

      @NotNull public static String checkNotEmptyParam(String name, String value)
      Check that the named parameter is not empty. Use a standard exception message if it is.
      Parameters:
      name - the parameter name
      value - the parameter value
      Returns:
      the value that was passed in
      Throws:
      IllegalArgumentException - if the value is empty
    • checkNotEmptyParam

      @NotNull public static CharSequence checkNotEmptyParam(String name, CharSequence value)
      Check that the named parameter is not empty. Use a standard exception message if it is.
      Parameters:
      name - the parameter name
      value - the parameter value
      Returns:
      the value that was passed in
      Throws:
      IllegalArgumentException - if the value is empty
    • checkNotEmptyParam

      @NotNull public static <E, T extends Collection<E>> T checkNotEmptyParam(String name, T value)
      Check that the named parameter is not empty. Use a standard exception message if it is.
      Parameters:
      name - the parameter name
      value - the parameter value
      Returns:
      the value that was passed in
      Throws:
      IllegalArgumentException - if the value is empty
    • checkNotEmptyParam

      @NotNull public static <K, V, T extends Map<K, V>> T checkNotEmptyParam(String name, T value)
      Check that the named parameter is not empty. Use a standard exception message if it is.
      Parameters:
      name - the parameter name
      value - the parameter value
      Returns:
      the value that was passed in
      Throws:
      IllegalArgumentException - if the value is empty
    • checkNotEmptyParam

      @NotNull public static <T> T[] checkNotEmptyParam(String name, T[] value)
      Check that the named parameter is not empty. Use a standard exception message if it is.
      Parameters:
      name - the parameter name
      value - the parameter value
      Returns:
      the value that was passed in
      Throws:
      IllegalArgumentException - if the value is empty
    • checkNotEmptyParam

      @NotNull public static boolean[] checkNotEmptyParam(String name, boolean[] value)
      Check that the named parameter is not empty. Use a standard exception message if it is.
      Parameters:
      name - the parameter name
      value - the parameter value
      Returns:
      the value that was passed in
      Throws:
      IllegalArgumentException - if the value is empty
    • checkNotEmptyParam

      @NotNull public static byte[] checkNotEmptyParam(String name, byte[] value)
      Check that the named parameter is not empty. Use a standard exception message if it is.
      Parameters:
      name - the parameter name
      value - the parameter value
      Returns:
      the value that was passed in
      Throws:
      IllegalArgumentException - if the value is empty
    • checkNotEmptyParam

      @NotNull public static short[] checkNotEmptyParam(String name, short[] value)
      Check that the named parameter is not empty. Use a standard exception message if it is.
      Parameters:
      name - the parameter name
      value - the parameter value
      Returns:
      the value that was passed in
      Throws:
      IllegalArgumentException - if the value is empty
    • checkNotEmptyParam

      @NotNull public static int[] checkNotEmptyParam(String name, int[] value)
      Check that the named parameter is not empty. Use a standard exception message if it is.
      Parameters:
      name - the parameter name
      value - the parameter value
      Returns:
      the value that was passed in
      Throws:
      IllegalArgumentException - if the value is empty
    • checkNotEmptyParam

      @NotNull public static long[] checkNotEmptyParam(String name, long[] value)
      Check that the named parameter is not empty. Use a standard exception message if it is.
      Parameters:
      name - the parameter name
      value - the parameter value
      Returns:
      the value that was passed in
      Throws:
      IllegalArgumentException - if the value is empty
    • checkNotEmptyParam

      @NotNull public static float[] checkNotEmptyParam(String name, float[] value)
      Check that the named parameter is not empty. Use a standard exception message if it is.
      Parameters:
      name - the parameter name
      value - the parameter value
      Returns:
      the value that was passed in
      Throws:
      IllegalArgumentException - if the value is empty
    • checkNotEmptyParam

      @NotNull public static double[] checkNotEmptyParam(String name, double[] value)
      Check that the named parameter is not empty. Use a standard exception message if it is.
      Parameters:
      name - the parameter name
      value - the parameter value
      Returns:
      the value that was passed in
      Throws:
      IllegalArgumentException - if the value is empty
    • checkMinimumParameter

      public static void checkMinimumParameter(String name, int min, int actual) throws IllegalArgumentException
      Check that the named parameter is greater than or equal to min.
      Parameters:
      name - the parameter name
      min - the minimum value
      actual - the actual parameter value
      Throws:
      IllegalArgumentException - if the actual value is less than the minimum value
    • checkMinimumParameter

      public static void checkMinimumParameter(String name, long min, long actual) throws IllegalArgumentException
      Check that the named parameter is greater than or equal to min.
      Parameters:
      name - the parameter name
      min - the minimum value
      actual - the actual parameter value
      Throws:
      IllegalArgumentException - if the actual value is less than the minimum value
    • checkMinimumParameter

      public static void checkMinimumParameter(String name, float min, float actual) throws IllegalArgumentException
      Check that the named parameter is greater than or equal to min.
      Parameters:
      name - the parameter name
      min - the minimum value
      actual - the actual parameter value
      Throws:
      IllegalArgumentException - if the actual value is less than the minimum value
    • checkMinimumParameter

      public static void checkMinimumParameter(String name, double min, double actual) throws IllegalArgumentException
      Check that the named parameter is greater than or equal to min.
      Parameters:
      name - the parameter name
      min - the minimum value
      actual - the actual parameter value
      Throws:
      IllegalArgumentException - if the actual value is less than the minimum value
    • checkMaximumParameter

      public static void checkMaximumParameter(String name, int max, int actual) throws IllegalArgumentException
      Check that the named parameter is less than or equal to max.
      Parameters:
      name - the parameter name
      max - the maximum value
      actual - the actual parameter value
      Throws:
      IllegalArgumentException - if the actual value is greater than the minimum value
    • checkMaximumParameter

      public static void checkMaximumParameter(String name, long max, long actual) throws IllegalArgumentException
      Check that the named parameter is less than or equal to max.
      Parameters:
      name - the parameter name
      max - the maximum value
      actual - the actual parameter value
      Throws:
      IllegalArgumentException - if the actual value is greater than the minimum value
    • checkMaximumParameter

      public static void checkMaximumParameter(String name, float max, float actual) throws IllegalArgumentException
      Check that the named parameter is less than or equal to max.
      Parameters:
      name - the parameter name
      max - the maximum value
      actual - the actual parameter value
      Throws:
      IllegalArgumentException - if the actual value is greater than the minimum value
    • checkMaximumParameter

      public static void checkMaximumParameter(String name, double max, double actual) throws IllegalArgumentException
      Check that the named parameter is less than or equal to max.
      Parameters:
      name - the parameter name
      max - the maximum value
      actual - the actual parameter value
      Throws:
      IllegalArgumentException - if the actual value is greater than the minimum value
    • checkArrayBounds

      public static void checkArrayBounds(Object[] array, int offs, int len) throws ArrayIndexOutOfBoundsException
      Check that the given offset and length fall completely within the bounds of the given array.
      Parameters:
      array - the array to check
      offs - the array offset
      len - the array length
      Throws:
      ArrayIndexOutOfBoundsException - if the range of the offset and length do not fall within the array bounds
    • checkArrayBounds

      public static void checkArrayBounds(byte[] array, int offs, int len) throws ArrayIndexOutOfBoundsException
      Check that the given offset and length fall completely within the bounds of the given array.
      Parameters:
      array - the array to check
      offs - the array offset
      len - the array length
      Throws:
      ArrayIndexOutOfBoundsException - if the range of the offset and length do not fall within the array bounds
    • checkArrayBounds

      public static void checkArrayBounds(char[] array, int offs, int len) throws ArrayIndexOutOfBoundsException
      Check that the given offset and length fall completely within the bounds of the given array.
      Parameters:
      array - the array to check
      offs - the array offset
      len - the array length
      Throws:
      ArrayIndexOutOfBoundsException - if the range of the offset and length do not fall within the array bounds
    • checkArrayBounds

      public static void checkArrayBounds(int[] array, int offs, int len) throws ArrayIndexOutOfBoundsException
      Check that the given offset and length fall completely within the bounds of the given array.
      Parameters:
      array - the array to check
      offs - the array offset
      len - the array length
      Throws:
      ArrayIndexOutOfBoundsException - if the range of the offset and length do not fall within the array bounds
    • checkArrayBounds

      public static void checkArrayBounds(long[] array, int offs, int len) throws ArrayIndexOutOfBoundsException
      Check that the given offset and length fall completely within the bounds of the given array.
      Parameters:
      array - the array to check
      offs - the array offset
      len - the array length
      Throws:
      ArrayIndexOutOfBoundsException - if the range of the offset and length do not fall within the array bounds
    • checkArrayBounds

      public static void checkArrayBounds(int arrayLength, int offs, int len) throws ArrayIndexOutOfBoundsException
      Check that the given offset and length fall completely within the bounds of the given array length.
      Parameters:
      arrayLength - the array length to check against
      offs - the array offset
      len - the array length
      Throws:
      ArrayIndexOutOfBoundsException - if the range of the offset and length do not fall within the array bounds
    • assertNotNull

      @NotNull public static <T> T assertNotNull(T value)
      Assert that the value is not null. Use a standard assertion failure message if it is. Only runs if assert is enabled.
      Type Parameters:
      T - the value type
      Parameters:
      value - the not-null value
      Returns:
      the value that was passed in
    • assertHoldsLock

      @NotNull public static <T> T assertHoldsLock(@NotNull T monitor)
      Assert that the given monitor is held by the current thread. Use a standard assertion failure message if it is not. Only runs if assert is enabled.
      Type Parameters:
      T - the monitor's type
      Parameters:
      monitor - the monitor object
      Returns:
      the value that was passed in
      Throws:
      IllegalArgumentException - if the monitor is null
    • assertNotHoldsLock

      @NotNull public static <T> T assertNotHoldsLock(@NotNull T monitor)
      Assert that the given monitor is not held by the current thread. Use a standard assertion failure message if it is. Only runs if assert is enabled.
      Type Parameters:
      T - the monitor's type
      Parameters:
      monitor - the monitor object
      Returns:
      the value that was passed in
      Throws:
      IllegalArgumentException - if the monitor is null
    • assertTrue

      public static boolean assertTrue(boolean expr)
      Assert that the given expression is always true.
      Parameters:
      expr - the boolean expression
      Returns:
      the boolean expression
    • assertFalse

      public static boolean assertFalse(boolean expr)
      Assert that the given expression is always false.
      Parameters:
      expr - the boolean expression
      Returns:
      the boolean expression
    • unreachableCode

      public static IllegalStateException unreachableCode()
      Return an exception indicating that the current code was intended to be unreachable.
      Returns:
      the exception which may be immediately thrown
    • impossibleSwitchCase

      @NotNull public static IllegalStateException impossibleSwitchCase(@NotNull Object obj)
      Return an exception indicating that the current switch case was intended to be unreachable.
      Parameters:
      obj - the switch case value
      Returns:
      the exception which may be immediately thrown
    • impossibleSwitchCase

      @NotNull public static IllegalStateException impossibleSwitchCase(int val)
      Return an exception indicating that the current switch case was intended to be unreachable.
      Parameters:
      val - the switch case value
      Returns:
      the exception which may be immediately thrown
    • impossibleSwitchCase

      @NotNull public static IllegalStateException impossibleSwitchCase(long val)
      Return an exception indicating that the current switch case was intended to be unreachable.
      Parameters:
      val - the switch case value
      Returns:
      the exception which may be immediately thrown
    • unsupported

      @NotNull public static UnsupportedOperationException unsupported()
      Return an exception explaining that the caller's method is not supported.
      Returns:
      the exception