Class Utils

java.lang.Object
dev.langchain4j.internal.Utils

public class Utils extends Object
Utility methods.
  • Method Details

    • firstNotNull

      @SafeVarargs public static <T> @NonNull T firstNotNull(@NonNull String name, @Nullable T... values)
      Returns the first non-null value from the provided array of values. If all values are null, an IllegalArgumentException is thrown.
      Parameters:
      name - A non-null string representing the name associated with the values.
      values - An array of potentially nullable values to search through.
      Returns:
      The first non-null value in the array.
      Throws:
      IllegalArgumentException - If all values are null or if the array is empty.
    • getOrDefault

      public static <T> T getOrDefault(T value, T defaultValue)
      Returns the given value if it is not null, otherwise returns the given default value.
      Type Parameters:
      T - The type of the value.
      Parameters:
      value - The value to return if it is not null.
      defaultValue - The value to return if the value is null.
      Returns:
      the given value if it is not null, otherwise returns the given default value.
    • getOrDefault

      public static <T> List<T> getOrDefault(List<T> list, List<T> defaultList)
      Returns the given list if it is not null and not empty, otherwise returns the given default list.
      Type Parameters:
      T - The type of the value.
      Parameters:
      list - The list to return if it is not null and not empty.
      defaultList - The list to return if the list is null or empty.
      Returns:
      the given list if it is not null and not empty, otherwise returns the given default list.
    • getOrDefault

      public static <K,V> Map<K,V> getOrDefault(Map<K,V> map, Map<K,V> defaultMap)
      Returns the given map if it is not null and not empty, otherwise returns the given default map.
      Parameters:
      map - The map to return if it is not null and not empty.
      defaultMap - The map to return if the map is null or empty.
      Returns:
      the given map if it is not null and not empty, otherwise returns the given default map.
    • getOrDefault

      public static <T> T getOrDefault(@Nullable T value, Supplier<T> defaultValueSupplier)
      Returns the given value if it is not null, otherwise returns the value returned by the given supplier.
      Type Parameters:
      T - The type of the value.
      Parameters:
      value - The value to return if it is not null.
      defaultValueSupplier - The supplier to call if the value is null.
      Returns:
      the given value if it is not null, otherwise returns the value returned by the given supplier.
    • isNullOrBlank

      public static boolean isNullOrBlank(String string)
      Is the given string null or blank?
      Parameters:
      string - The string to check.
      Returns:
      true if the string is null or blank.
    • isNullOrEmpty

      public static boolean isNullOrEmpty(String string)
      Is the given string null or empty ("")?
      Parameters:
      string - The string to check.
      Returns:
      true if the string is null or empty.
    • isNotNullOrBlank

      public static boolean isNotNullOrBlank(String string)
      Is the given string not null and not blank?
      Parameters:
      string - The string to check.
      Returns:
      true if there's something in the string.
    • isNotNullOrEmpty

      public static boolean isNotNullOrEmpty(String string)
      Is the given string not null and not empty ("")?
      Parameters:
      string - The string to check.
      Returns:
      true if the given string is not null and not empty ("")?
    • areNotNullOrBlank

      public static boolean areNotNullOrBlank(String... strings)
      Are all the given strings not null and not blank?
      Parameters:
      strings - The strings to check.
      Returns:
      true if every string is non-null and non-empty.
    • isNullOrEmpty

      public static boolean isNullOrEmpty(Collection<?> collection)
      Is the collection null or empty?
      Parameters:
      collection - The collection to check.
      Returns:
      true if the collection is null or Collection.isEmpty(), otherwise false.
    • isNullOrEmpty

      public static boolean isNullOrEmpty(Iterable<?> iterable)
      Is the iterable object null or empty?
      Parameters:
      iterable - The iterable object to check.
      Returns:
      true if the iterable object is null or there are no objects to iterate over, otherwise false.
    • isNullOrEmpty

      public static boolean isNullOrEmpty(Object[] array)
      Utility method to check if an array is null or has no elements.
      Parameters:
      array - the array to check
      Returns:
      true if the array is null or has no elements, otherwise false
    • isNullOrEmpty

      public static boolean isNullOrEmpty(@Nullable Map<?,?> map)
      Is the map object null or empty?
      Parameters:
      map - The iterable object to check.
      Returns:
      true if the map object is null or empty map, otherwise false.
    • repeat

      public static String repeat(String string, int times)
      Returns a string consisting of the given string repeated times times.
      Parameters:
      string - The string to repeat.
      times - The number of times to repeat the string.
      Returns:
      A string consisting of the given string repeated times times.
    • randomUUID

      public static String randomUUID()
      Returns a random UUID.
      Returns:
      a UUID.
    • generateUUIDFrom

      public static String generateUUIDFrom(String input)
      Generates a UUID from a hash of the given input string.
      Parameters:
      input - The input string.
      Returns:
      A UUID.
    • ensureTrailingForwardSlash

      public static String ensureTrailingForwardSlash(String url)
      Appends a trailing '/' if the provided URL does not end with '/'
      Parameters:
      url - URL to check for trailing '/'
      Returns:
      Same URL if it already ends with '/' or a new URL with '/' appended
    • quoted

      public static String quoted(Object object)
      Returns the given object's toString() surrounded by quotes.

      If the given object is null, the string "null" is returned.

      Parameters:
      object - The object to quote.
      Returns:
      The given object surrounded by quotes.
    • firstChars

      public static String firstChars(String string, int numberOfChars)
      Returns the first numberOfChars characters of the given string. If the string is shorter than numberOfChars, the whole string is returned.
      Parameters:
      string - The string to get the first characters from.
      numberOfChars - The number of characters to return.
      Returns:
      The first numberOfChars characters of the given string.
    • readBytes

      public static byte[] readBytes(String url)
      Reads the content as bytes from the given URL as a GET request for HTTP/HTTPS resources, and from files stored on the local filesystem.
      Parameters:
      url - The URL to read from.
      Returns:
      The content as bytes.
      Throws:
      RuntimeException - if the request fails.
    • copyIfNotNull

      public static <T> Set<T> copyIfNotNull(Set<T> set)
      Returns an (unmodifiable) copy of the provided set. Returns null if the provided set is null.
      Type Parameters:
      T - Generic type of the set.
      Parameters:
      set - The set to copy.
      Returns:
      The copy of the provided set.
    • copy

      public static <T> Set<T> copy(Set<T> set)
      Returns an (unmodifiable) copy of the provided set. Returns an empty set if the provided set is null.
      Type Parameters:
      T - Generic type of the set.
      Parameters:
      set - The set to copy.
      Returns:
      The copy of the provided set or an empty set.
    • copyIfNotNull

      public static <T> List<T> copyIfNotNull(List<T> list)
      Returns an (unmodifiable) copy of the provided list. Returns null if the provided list is null.
      Type Parameters:
      T - Generic type of the list.
      Parameters:
      list - The list to copy.
      Returns:
      The copy of the provided list.
    • copy

      public static <T> List<T> copy(List<T> list)
      Returns an (unmodifiable) copy of the provided list. Returns an empty list if the provided list is null.
      Type Parameters:
      T - Generic type of the list.
      Parameters:
      list - The list to copy.
      Returns:
      The copy of the provided list or an empty list.
    • copyIfNotNull

      public static <K,V> Map<K,V> copyIfNotNull(Map<K,V> map)
      Returns an (unmodifiable) copy of the provided map. Returns null if the provided map is null.
      Parameters:
      map - The map to copy.
      Returns:
      The copy of the provided map.
    • copy

      public static <K,V> Map<K,V> copy(Map<K,V> map)
      Returns an (unmodifiable) copy of the provided map. Returns an empty map if the provided map is null.
      Parameters:
      map - The map to copy.
      Returns:
      The copy of the provided map or an empty map.
    • toStringValueMap

      public static Map<String,String> toStringValueMap(Map<String,Object> map)
    • getAnnotatedMethod

      public static Optional<Method> getAnnotatedMethod(Method method, Class<? extends Annotation> annotation)
      Returns the method eventually annotated with the given annotation. It could be the method itself or, if the method belongs to a proxy, a method from one of the interfaces implemented by the proxy.
      Parameters:
      method - The method to check for the annotation.
      annotation - The annotation to look for.
      Returns:
      An Optional containing the method having the given annotation, or an empty Optional if there isn't any.
    • warnIfNullOrBlank

      public static String warnIfNullOrBlank(String value, String fieldName, Class<?> clazz)
      Logs a warning if the given string value is null or blank.

      This method is typically used in constructors or validation routines to highlight missing or incomplete field values without throwing an exception. It returns the original value unchanged so it can be assigned directly to a field or variable.

      Example usage:

      this.name = Utils.warnIfNullOrBlank(name, "name", McpResource.class);
      
      Which will log:
      McpResource: 'name' is null or blank
      
      Parameters:
      value - the string value to check (may be null or blank)
      fieldName - the name of the field being validated (used in the log message)
      clazz - the class where the validation occurs (used to identify context in the log)
      Returns:
      the original value (may be null or blank)