Class Utils

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

public class Utils extends Object
Utility methods.
  • Method Details

    • 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> T getOrDefault(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(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.
    • isCollectionEmpty

      @Deprecated(forRemoval=true) public static boolean isCollectionEmpty(Collection<?> collection)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Parameters:
      collection - The collection to check.
      Returns:
      true if the collection is null or empty, false otherwise.
    • 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> 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.
    • 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.