Class ValidationUtils

java.lang.Object
dev.langchain4j.internal.ValidationUtils

public class ValidationUtils extends Object
Utility class for validating method arguments.
  • Method Details

    • ensureEq

      public static void ensureEq(Object lhs, Object rhs, String format, Object... args)
      Ensure that the two values are equal.
      Parameters:
      lhs - the left hand side value.
      rhs - the right hand side value.
      format - the format string for the exception message.
      args - the format arguments for the exception message.
    • ensureNotNull

      public static <T> T ensureNotNull(T object, String name)
      Ensures that the given object is not null.
      Type Parameters:
      T - The type of the object.
      Parameters:
      object - The object to check.
      name - The name of the object to be used in the exception message.
      Returns:
      The object if it is not null.
      Throws:
      IllegalArgumentException - if the object is null.
    • ensureNotNull

      public static <T> T ensureNotNull(T object, String format, Object... args)
      Ensures that the given object is not null.
      Type Parameters:
      T - The type of the object.
      Parameters:
      object - The object to check.
      format - The format of the exception message.
      args - The arguments for the exception message.
      Returns:
      The object if it is not null.
    • ensureNotEmpty

      public static <T extends Collection<?>> T ensureNotEmpty(T collection, String name)
      Ensures that the given collection is not null and not empty.
      Type Parameters:
      T - The type of the collection.
      Parameters:
      collection - The collection to check.
      name - The name of the collection to be used in the exception message.
      Returns:
      The collection if it is not null and not empty.
      Throws:
      IllegalArgumentException - if the collection is null or empty.
    • ensureNotEmpty

      public static <K, V> Map<K,V> ensureNotEmpty(Map<K,V> map, String name)
      Ensures that the given map is not null and not empty.
      Type Parameters:
      K - The type of the key.
      V - The type of the value.
      Parameters:
      map - The map to check.
      name - The name of the map to be used in the exception message.
      Returns:
      The map if it is not null and not empty.
      Throws:
      IllegalArgumentException - if the collection is null or empty.
    • ensureNotBlank

      public static String ensureNotBlank(String string, String name)
      Ensures that the given string is not null and not blank.
      Parameters:
      string - The string to check.
      name - The name of the string to be used in the exception message.
      Returns:
      The string if it is not null and not blank.
      Throws:
      IllegalArgumentException - if the string is null or blank.
    • ensureTrue

      public static void ensureTrue(boolean expression, String msg)
      Ensures that the given expression is true.
      Parameters:
      expression - The expression to check.
      msg - The message to be used in the exception.
      Throws:
      IllegalArgumentException - if the expression is false.
    • ensureGreaterThanZero

      public static int ensureGreaterThanZero(Integer i, String name)
      Ensures that the given expression is true.
      Parameters:
      i - The expression to check.
      name - The message to be used in the exception.
      Returns:
      The value if it is greater than zero.
      Throws:
      IllegalArgumentException - if the expression is false.
    • ensureBetween

      public static double ensureBetween(Double d, double min, double max, String name)
      Ensures that the given Double value is in [min, max].
      Parameters:
      d - The value to check.
      min - The minimum value.
      max - The maximum value.
      name - The value name to be used in the exception.
      Returns:
      The value if it is in [min, max].
      Throws:
      IllegalArgumentException - if the value is not in [min, max].
    • ensureBetween

      public static int ensureBetween(Integer i, int min, int max, String name)
      Ensures that the given Integer value is in [min, max].
      Parameters:
      i - The value to check.
      min - The minimum value.
      max - The maximum value.
      name - The value name to be used in the exception.
      Returns:
      The value if it is in [min, max].
      Throws:
      IllegalArgumentException - if the value is not in [min, max].
    • ensureBetween

      public static long ensureBetween(Long i, long min, long max, String name)
      Ensures that the given Long value is in [min, max].
      Parameters:
      i - The value to check.
      min - The minimum value.
      max - The maximum value.
      name - The value name to be used in the exception.
      Returns:
      The value if it is in [min, max].
      Throws:
      IllegalArgumentException - if the value is not in [min, max].