Package dev.langchain4j.internal
Class Utils
java.lang.Object
dev.langchain4j.internal.Utils
Utility methods.
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanareNotNullOrBlank(String... strings) Are all the given strings notnulland not blank?static <T> List<T> Returns an (unmodifiable) copy of the provided list.static <K,V> Map <K, V> Returns an (unmodifiable) copy of the provided map.static <T> Set<T> Returns an (unmodifiable) copy of the provided set.static <T> List<T> copyIfNotNull(List<T> list) Returns an (unmodifiable) copy of the provided list.static <K,V> Map <K, V> copyIfNotNull(Map<K, V> map) Returns an (unmodifiable) copy of the provided map.static <T> Set<T> copyIfNotNull(Set<T> set) Returns an (unmodifiable) copy of the provided set.static StringAppends a trailing '/' if the provided URL does not end with '/'static StringfirstChars(String string, int numberOfChars) Returns the firstnumberOfCharscharacters of the given string.static StringgenerateUUIDFrom(String input) Generates a UUID from a hash of the given input string.static <T> List<T> getOrDefault(List<T> list, List<T> defaultList) Returns the given list if it is notnulland not empty, otherwise returns the given default list.static <K,V> Map <K, V> getOrDefault(Map<K, V> map, Map<K, V> defaultMap) Returns the given map if it is notnulland not empty, otherwise returns the given default map.static <T> TgetOrDefault(T value, Supplier<T> defaultValueSupplier) Returns the given value if it is notnull, otherwise returns the value returned by the given supplier.static <T> TgetOrDefault(T value, T defaultValue) Returns the given value if it is notnull, otherwise returns the given default value.static booleanisNotNullOrBlank(String string) Is the given string notnulland not blank?static booleanisNotNullOrEmpty(String string) Is the given string notnulland not empty ("")?static booleanisNullOrBlank(String string) Is the given stringnullor blank?static booleanisNullOrEmpty(Iterable<?> iterable) Is the iterable objectnullor empty?static booleanisNullOrEmpty(String string) Is the given stringnullor empty ("")?static booleanisNullOrEmpty(Collection<?> collection) Is the collectionnullor empty?static booleanisNullOrEmpty(Map<?, ?> map) Is the map objectnullor empty?static StringReturns the given object'stoString()surrounded by quotes.static StringReturns a random UUID.static byte[]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.static StringReturns a string consisting of the given string repeatedtimestimes.toStringValueMap(Map<String, Object> map)
-
Method Details
-
getOrDefault
public static <T> T getOrDefault(T value, T defaultValue) Returns the given value if it is notnull, otherwise returns the given default value.- Type Parameters:
T- The type of the value.- Parameters:
value- The value to return if it is notnull.defaultValue- The value to return if the value isnull.- Returns:
- the given value if it is not
null, otherwise returns the given default value.
-
getOrDefault
Returns the given list if it is notnulland 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 notnulland not empty.defaultList- The list to return if the list isnullor empty.- Returns:
- the given list if it is not
nulland not empty, otherwise returns the given default list.
-
getOrDefault
Returns the given map if it is notnulland not empty, otherwise returns the given default map.- Parameters:
map- The map to return if it is notnulland not empty.defaultMap- The map to return if the map isnullor empty.- Returns:
- the given map if it is not
nulland not empty, otherwise returns the given default map.
-
getOrDefault
Returns the given value if it is notnull, 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 notnull.defaultValueSupplier- The supplier to call if the value isnull.- Returns:
- the given value if it is not
null, otherwise returns the value returned by the given supplier.
-
isNullOrBlank
Is the given stringnullor blank?- Parameters:
string- The string to check.- Returns:
- true if the string is
nullor blank.
-
isNullOrEmpty
Is the given stringnullor empty ("")?- Parameters:
string- The string to check.- Returns:
- true if the string is
nullor empty.
-
isNotNullOrBlank
Is the given string notnulland not blank?- Parameters:
string- The string to check.- Returns:
- true if there's something in the string.
-
isNotNullOrEmpty
Is the given string notnulland not empty ("")?- Parameters:
string- The string to check.- Returns:
- true if the given string is not
nulland not empty ("")?
-
areNotNullOrBlank
Are all the given strings notnulland not blank?- Parameters:
strings- The strings to check.- Returns:
trueif every string is non-nulland non-empty.
-
isNullOrEmpty
Is the collectionnullor empty?- Parameters:
collection- The collection to check.- Returns:
trueif the collection isnullorCollection.isEmpty(), otherwisefalse.
-
isNullOrEmpty
Is the iterable objectnullor empty?- Parameters:
iterable- The iterable object to check.- Returns:
trueif the iterable object isnullor there are no objects to iterate over, otherwisefalse.
-
isNullOrEmpty
Is the map objectnullor empty?- Parameters:
map- The iterable object to check.- Returns:
trueif the map object isnullor empty map, otherwisefalse.
-
repeat
Returns a string consisting of the given string repeatedtimestimes.- Parameters:
string- The string to repeat.times- The number of times to repeat the string.- Returns:
- A string consisting of the given string repeated
timestimes.
-
randomUUID
Returns a random UUID.- Returns:
- a UUID.
-
generateUUIDFrom
Generates a UUID from a hash of the given input string.- Parameters:
input- The input string.- Returns:
- A UUID.
-
ensureTrailingForwardSlash
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
Returns the given object'stoString()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
Returns the firstnumberOfCharscharacters of the given string. If the string is shorter thannumberOfChars, 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
numberOfCharscharacters of the given string.
-
readBytes
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
Returns an (unmodifiable) copy of the provided set. Returnsnullif the provided set isnull.- Type Parameters:
T- Generic type of the set.- Parameters:
set- The set to copy.- Returns:
- The copy of the provided set.
-
copy
Returns an (unmodifiable) copy of the provided set. Returns an empty set if the provided set isnull.- 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
Returns an (unmodifiable) copy of the provided list. Returnsnullif the provided list isnull.- Type Parameters:
T- Generic type of the list.- Parameters:
list- The list to copy.- Returns:
- The copy of the provided list.
-
copy
Returns an (unmodifiable) copy of the provided list. Returns an empty list if the provided list isnull.- 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
Returns an (unmodifiable) copy of the provided map. Returnsnullif the provided map isnull.- Parameters:
map- The map to copy.- Returns:
- The copy of the provided map.
-
copy
Returns an (unmodifiable) copy of the provided map. Returns an empty map if the provided map isnull.- Parameters:
map- The map to copy.- Returns:
- The copy of the provided map or an empty map.
-
toStringValueMap
-