Package dev.langchain4j.internal
Class Utils
java.lang.Object
dev.langchain4j.internal.Utils
Utility methods.
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
areNotNullOrBlank
(String... strings) Are all the given strings notnull
and not blank?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 String
Appends a trailing '/' if the provided URL does not end with '/'static String
firstChars
(String string, int numberOfChars) Returns the firstnumberOfChars
characters of the given string.static String
generateUUIDFrom
(String input) Generates a UUID from a hash of the given input string.static <T> T
getOrDefault
(T value, Supplier<T> defaultValueSupplier) Returns the given value if it is notnull
, otherwise returns the value returned by the given supplier.static <T> T
getOrDefault
(T value, T defaultValue) Returns the given value if it is notnull
, otherwise returns the given default value.static boolean
isCollectionEmpty
(Collection<?> collection) Deprecated.static boolean
isNotNullOrBlank
(String string) Is the given string notnull
and not blank?static boolean
isNotNullOrEmpty
(String string) Is the given string notnull
and not empty ("")?static boolean
isNullOrBlank
(String string) Is the given stringnull
or blank?static boolean
isNullOrEmpty
(Iterable<?> iterable) Is the iterable objectnull
or empty?static boolean
isNullOrEmpty
(String string) Is the given stringnull
or empty ("")?static boolean
isNullOrEmpty
(Collection<?> collection) Is the collectionnull
or empty?static String
Returns the given object'stoString()
surrounded by quotes.static String
Returns 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 String
Returns a string consisting of the given string repeatedtimes
times.
-
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 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 stringnull
or blank?- Parameters:
string
- The string to check.- Returns:
- true if the string is
null
or blank.
-
isNullOrEmpty
Is the given stringnull
or empty ("")?- Parameters:
string
- The string to check.- Returns:
- true if the string is
null
or empty.
-
isNotNullOrBlank
Is the given string notnull
and not blank?- Parameters:
string
- The string to check.- Returns:
- true if there's something in the string.
-
isNotNullOrEmpty
Is the given string notnull
and not empty ("")?- Parameters:
string
- The string to check.- Returns:
- true if the given string is not
null
and not empty ("")?
-
areNotNullOrBlank
Are all the given strings notnull
and not blank?- Parameters:
strings
- The strings to check.- Returns:
true
if every string is non-null
and non-empty.
-
isNullOrEmpty
Is the collectionnull
or empty?- Parameters:
collection
- The collection to check.- Returns:
true
if the collection isnull
orCollection.isEmpty()
, otherwisefalse
.
-
isNullOrEmpty
Is the iterable objectnull
or empty?- Parameters:
iterable
- The iterable object to check.- Returns:
true
if the iterable object isnull
or there are no objects to iterate over, otherwisefalse
.
-
isCollectionEmpty
Deprecated.UseisNullOrEmpty(Collection)
instead.- Parameters:
collection
- The collection to check.- Returns:
true
if the collection isnull
or empty,false
otherwise.
-
repeat
Returns a string consisting of the given string repeatedtimes
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
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 firstnumberOfChars
characters 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
numberOfChars
characters 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 list. Returnsnull
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.
-
copyIfNotNull
Returns an (unmodifiable) copy of the provided map. Returnsnull
if the provided map isnull
.- Parameters:
map
- The map to copy.- Returns:
- The copy of the provided map.
-
isNullOrEmpty(Collection)
instead.