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> copyIfNotNull(List<T> list) Returns an (unmodifiable) copy of the provided list.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> 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 booleanisCollectionEmpty(Collection<?> collection) Deprecated.static booleanisNotNullOrBlank(String string) Is the given string notnulland not blank?static booleanisNullOrBlank(String string) Is the given stringnullor blank?static booleanisNullOrEmpty(Collection<?> collection) Is the collectionnullor 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.static StringReturns a string consisting of the given string repeatedtimestimes.
-
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 stringnullor blank?- Parameters:
string- The string to check.- Returns:
- true if the string is
nullor blank.
-
isNotNullOrBlank
Is the given string notnulland not blank?- Parameters:
string- The string to check.- Returns:
- true if there's something in the string.
-
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.
-
isCollectionEmpty
Deprecated.UseisNullOrEmpty(Collection)instead.- Parameters:
collection- The collection to check.- Returns:
trueif the collection isnullor empty,falseotherwise.
-
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.
-
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.- 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. 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.
-
isNullOrEmpty(Collection)instead.