Class RequestValidations

java.lang.Object
org.apache.cassandra.cql3.statements.RequestValidations

public final class RequestValidations extends Object
Utility methods use to perform request validation.

This class use overloaded methods to allow to specify different numbers of message arguments. While this introduces some clutter in the API, it avoids array allocation, initialization, and garbage collection overhead that is incurred by varargs calls.

Warning about performance

The goal of this class is to improve readability of code, but in some circumstances this may come at a significant performance cost. Remember that argument values for message construction must all be computed eagerly, and autoboxing may happen as well, even when the check succeeds. If the message arguments are expensive to create you should use the customary form:

      if (value < 0.0)
          throw RequestValidations.invalidRequest("negative value: %s", toReadableText(value));
  

  • Method Details

    • checkTrue

      public static void checkTrue(boolean expression, String message) throws InvalidRequestException
      Checks that the specified expression is true. If not an InvalidRequestException will be thrown.
      Parameters:
      expression - the expression to test
      message - the error message
      Throws:
      InvalidRequestException - if the specified expression is false.
    • checkTrue

      public static void checkTrue(boolean expression, String messageTemplate, Object messageArg) throws InvalidRequestException
      Checks that the specified expression is true. If not an InvalidRequestException will be thrown.
      Parameters:
      expression - the expression to test
      messageTemplate - the template used to build the error message
      messageArg - the message argument
      Throws:
      InvalidRequestException - if the specified expression is false.
    • checkTrue

      public static void checkTrue(boolean expression, String messageTemplate, Object arg1, Object arg2) throws InvalidRequestException
      Checks that the specified expression is true. If not an InvalidRequestException will be thrown.
      Parameters:
      expression - the expression to test
      messageTemplate - the template used to build the error message
      arg1 - the first message argument
      arg2 - the second message argument
      Throws:
      InvalidRequestException - if the specified expression is false.
    • checkTrue

      public static void checkTrue(boolean expression, String messageTemplate, Object arg1, Object arg2, Object arg3) throws InvalidRequestException
      Checks that the specified expression is true. If not an InvalidRequestException will be thrown.
      Parameters:
      expression - the expression to test
      messageTemplate - the template used to build the error message
      arg1 - the first message argument
      arg2 - the second message argument
      arg3 - the third message argument
      Throws:
      InvalidRequestException - if the specified expression is false.
    • checkNotEmpty

      public static <T extends Collection<E>, E> T checkNotEmpty(T collection, String messageTemplate, Object messageArg) throws InvalidRequestException
      Checks that the specified collections is NOT empty. If it is an InvalidRequestException will be thrown.
      Parameters:
      collection - the collection to test
      messageTemplate - the template used to build the error message
      messageArg - the message argument
      Returns:
      the collection
      Throws:
      InvalidRequestException - if the specified collection is empty.
    • checkNotEmpty

      public static <T extends Collection<E>, E> T checkNotEmpty(T collection, String messageTemplate, Object arg1, Object arg2) throws InvalidRequestException
      Checks that the specified collections is NOT empty. If it is an InvalidRequestException will be thrown.
      Parameters:
      collection - the collection to test
      messageTemplate - the template used to build the error message
      arg1 - the first message argument
      arg2 - the second message argument
      Returns:
      the collection
      Throws:
      InvalidRequestException - if the specified collection is empty.
    • checkContainsNoDuplicates

      public static void checkContainsNoDuplicates(List<?> list, String message) throws InvalidRequestException
      Checks that the specified list does not contains duplicates.
      Parameters:
      list - the list to test
      message - the error message
      Throws:
      InvalidRequestException - if the specified list contains duplicates.
    • checkContainsOnly

      public static <E> void checkContainsOnly(List<E> list, List<E> expectedElements, String message) throws InvalidRequestException
      Checks that the specified list contains only the specified elements.
      Parameters:
      list - the list to test
      expectedElements - the expected elements
      message - the error message
      Throws:
      InvalidRequestException - if the specified list contains duplicates.
    • checkFalse

      public static void checkFalse(boolean expression, String messageTemplate, Object messageArg) throws InvalidRequestException
      Checks that the specified expression is false. If not an InvalidRequestException will be thrown.
      Parameters:
      expression - the expression to test
      messageTemplate - the template used to build the error message
      messageArg - the message argument
      Throws:
      InvalidRequestException - if the specified expression is true.
    • checkFalse

      public static void checkFalse(boolean expression, String messageTemplate, Object arg1, Object arg2) throws InvalidRequestException
      Checks that the specified expression is false. If not an InvalidRequestException will be thrown.
      Parameters:
      expression - the expression to test
      messageTemplate - the template used to build the error message
      arg1 - the first message argument
      arg2 - the second message argument
      Throws:
      InvalidRequestException - if the specified expression is true.
    • checkFalse

      public static void checkFalse(boolean expression, String messageTemplate, Object arg1, Object arg2, Object arg3) throws InvalidRequestException
      Checks that the specified expression is false. If not an InvalidRequestException will be thrown.
      Parameters:
      expression - the expression to test
      messageTemplate - the template used to build the error message
      arg1 - the first message argument
      arg2 - the second message argument
      arg3 - the third message argument
      Throws:
      InvalidRequestException - if the specified expression is true.
    • checkFalse

      public static void checkFalse(boolean expression, String message) throws InvalidRequestException
      Checks that the specified expression is false. If not an InvalidRequestException will be thrown.
      Parameters:
      expression - the expression to test
      message - the error message
      Throws:
      InvalidRequestException - if the specified expression is true.
    • checkNotNull

      public static <T> T checkNotNull(T object, String message) throws InvalidRequestException
      Checks that the specified object is NOT null. If it is an InvalidRequestException will be thrown.
      Parameters:
      object - the object to test
      message - the error message
      Returns:
      the object
      Throws:
      InvalidRequestException - if the specified object is null.
    • checkNotNull

      public static <T> T checkNotNull(T object, String messageTemplate, Object messageArg) throws InvalidRequestException
      Checks that the specified object is NOT null. If it is an InvalidRequestException will be thrown.
      Parameters:
      object - the object to test
      messageTemplate - the template used to build the error message
      messageArg - the message argument
      Returns:
      the object
      Throws:
      InvalidRequestException - if the specified object is null.
    • checkNotNull

      public static <T> T checkNotNull(T object, String messageTemplate, Object arg1, Object arg2) throws InvalidRequestException
      Checks that the specified object is NOT null. If it is an InvalidRequestException will be thrown.
      Parameters:
      object - the object to test
      messageTemplate - the template used to build the error message
      arg1 - the first message argument
      arg2 - the second message argument
      Returns:
      the object
      Throws:
      InvalidRequestException - if the specified object is null.
    • checkBindValueSet

      public static void checkBindValueSet(ByteBuffer b, String messageTemplate, Object messageArg) throws InvalidRequestException
      Checks that the specified bind marker value is set to a meaningful value. If it is not a InvalidRequestException will be thrown.
      Parameters:
      b - the ByteBuffer to test
      messageTemplate - the template used to build the error message
      messageArg - the message argument
      Throws:
      InvalidRequestException - if the specified bind marker value is not set to a meaningful value.
    • checkNull

      public static void checkNull(Object object, String messageTemplate, Object messageArg) throws InvalidRequestException
      Checks that the specified object is null. If it is not an InvalidRequestException will be thrown.
      Parameters:
      object - the object to test
      messageTemplate - the template used to build the error message
      messageArg - the message argument
      Throws:
      InvalidRequestException - if the specified object is not null.
    • checkNull

      public static void checkNull(Object object, String message) throws InvalidRequestException
      Checks that the specified object is null. If it is not an InvalidRequestException will be thrown.
      Parameters:
      object - the object to test
      message - the error message
      Throws:
      InvalidRequestException - if the specified object is not null.
    • invalidRequest

      public static InvalidRequestException invalidRequest(String message)
      Returns an InvalidRequestException with the specified message.
      Parameters:
      message - the error message
      Returns:
      an InvalidRequestException with the specified message.
    • invalidRequest

      public static InvalidRequestException invalidRequest(String messageTemplate, Object... messageArgs)
      Returns an InvalidRequestException with the specified message.
      Parameters:
      messageTemplate - the template used to build the error message
      messageArgs - the message arguments
      Returns:
      an InvalidRequestException with the specified message.