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 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
isNullOrBlank
(String string) Is the given stringnull
or blank?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.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.
-
isNotNullOrBlank
Is the given string notnull
and not blank?- Parameters:
string
- The string to check.- Returns:
- true if there's something in the string.
-
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
.
-
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.
-
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.- 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.
-
isNullOrEmpty(Collection)
instead.