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 <T> @NonNull TfirstNotNull(@NonNull String name, @Nullable T... values) Returns the first non-null value from the provided array of values.static StringgenerateUUIDFrom(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> TgetOrDefault(@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 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, 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(Object[] array) Utility method to check if an array is null or has no elements.static booleanisNullOrEmpty(String string) Is the given stringnullor empty ("")?static booleanisNullOrEmpty(@Nullable Map<?, ?> map) Is the map objectnullor empty?static booleanisNullOrEmpty(Collection<?> collection) Is the collectionnullor empty?static <K,V> Map <K, V> mutableCopy(Map<K, V> map) Returns a mutable copy of the provided map.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) static StringwarnIfNullOrBlank(String value, String fieldName, Class<?> clazz) Logs a warning if the given string value isnullor 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 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
Utility method to check if an array is null or has no elements.- Parameters:
array- the array to check- Returns:
trueif the array is null or has no elements, otherwisefalse
-
isNullOrEmpty
Is the map objectnullor empty?- Parameters:
map- The iterable object to check.- Returns:
trueif the map object isnullor empty map, otherwisefalse.
-
repeat
-
randomUUID
-
generateUUIDFrom
-
ensureTrailingForwardSlash
-
quoted
-
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
-
copy
-
copyIfNotNull
-
copy
-
copyIfNotNull
-
copy
-
mutableCopy
-
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 isnullor 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 benullor 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
nullor blank)
-