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> 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 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 <T> @NonNull T
firstNotNull
(@NonNull String name, @Nullable T... values) Returns the first non-null value from the provided array of values.static String
generateUUIDFrom
(String input) Generates a UUID from a hash of the given input string.getAnnotatedMethod
(Method method, Class<? extends Annotation> annotation) Returns the method eventually annotated with the given annotation.static <T> T
getOrDefault
(@Nullable T value, Supplier<T> defaultValueSupplier) Returns the given value if it is notnull
, otherwise returns the value returned by the given supplier.static <T> List
<T> getOrDefault
(List<T> list, List<T> defaultList) Returns the given list if it is notnull
and 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 notnull
and not empty, otherwise returns the given default map.static <T> T
getOrDefault
(T value, T defaultValue) Returns the given value if it is notnull
, otherwise returns the given default value.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
(Object[] array) Utility method to check if an array is null or has no elements.static boolean
isNullOrEmpty
(String string) Is the given stringnull
or empty ("")?static boolean
isNullOrEmpty
(@Nullable Map<?, ?> map) Is the map objectnull
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.toStringValueMap
(Map<String, Object> map) static String
warnIfNullOrBlank
(String value, String fieldName, Class<?> clazz) Logs a warning if the given string value isnull
or blank.
-
Method Details
-
firstNotNull
Returns the first non-null value from the provided array of values. If all values are null, an IllegalArgumentException is thrown.- Parameters:
name
- A non-null string representing the name associated with the values.values
- An array of potentially nullable values to search through.- Returns:
- The first non-null value in the array.
- Throws:
IllegalArgumentException
- If all values are null or if the array is empty.
-
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 notnull
and 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 notnull
and not empty.defaultList
- The list to return if the list isnull
or empty.- Returns:
- the given list if it is not
null
and not empty, otherwise returns the given default list.
-
getOrDefault
Returns the given map if it is notnull
and not empty, otherwise returns the given default map.- Parameters:
map
- The map to return if it is notnull
and not empty.defaultMap
- The map to return if the map isnull
or empty.- Returns:
- the given map if it is not
null
and 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 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
.
-
isNullOrEmpty
Utility method to check if an array is null or has no elements.- Parameters:
array
- the array to check- Returns:
true
if the array is null or has no elements, otherwisefalse
-
isNullOrEmpty
Is the map objectnull
or empty?- Parameters:
map
- The iterable object to check.- Returns:
true
if the map object isnull
or empty map, otherwisefalse
.
-
repeat
-
randomUUID
-
generateUUIDFrom
-
ensureTrailingForwardSlash
-
quoted
-
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
-
copy
-
copyIfNotNull
-
copy
-
copyIfNotNull
-
copy
-
toStringValueMap
-
getAnnotatedMethod
public static Optional<Method> getAnnotatedMethod(Method method, Class<? extends Annotation> annotation) Returns the method eventually annotated with the given annotation. It could be the method itself or, if the method belongs to a proxy, a method from one of the interfaces implemented by the proxy. -
warnIfNullOrBlank
Logs a warning if the given string value isnull
or blank.This method is typically used in constructors or validation routines to highlight missing or incomplete field values without throwing an exception. It returns the original value unchanged so it can be assigned directly to a field or variable.
Example usage:
this.name = Utils.warnIfNullOrBlank(name, "name", McpResource.class);
Which will log:McpResource: 'name' is null or blank
- Parameters:
value
- the string value to check (may benull
or blank)fieldName
- the name of the field being validated (used in the log message)clazz
- the class where the validation occurs (used to identify context in the log)- Returns:
- the original value (may be
null
or blank)
-