Class Utils
-
- All Implemented Interfaces:
public class Utils
Utility methods.
-
-
Method Summary
Modifier and Type Method Description static <T> T
getOrDefault(T value, T defaultValue)
Returns the given value if it is not null
, otherwise returns the given default value.static <T> List<T>
getOrDefault(List<T> list, List<T> defaultList)
Returns the given list if it is not null
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 not null
and not empty, otherwise returns the given default map.static <T> T
getOrDefault(T value, Supplier<T> defaultValueSupplier)
Returns the given value if it is not null
, otherwise returns the value returned by the given supplier.static boolean
isNullOrBlank(String string)
Is the given string null
or blank?static boolean
isNullOrEmpty(String string)
Is the given string null
or empty ("")?static boolean
isNotNullOrBlank(String string)
Is the given string not null
and not blank?static boolean
isNotNullOrEmpty(String string)
Is the given string not null
and not empty ("")?static boolean
areNotNullOrBlank(Array<String> strings)
Are all the given strings not null
and not blank?static boolean
isNullOrEmpty(Collection<out Object> collection)
Is the collection null
or empty?static boolean
isNullOrEmpty(Iterable<out Object> iterable)
Is the iterable object null
or empty?static boolean
isNullOrEmpty(Map<out Object, out Object> map)
Is the map object null
or empty?static boolean
isCollectionEmpty(Collection<out Object> collection)
static String
repeat(String string, int times)
Returns a string consisting of the given string repeated times
times.static String
randomUUID()
Returns a random UUID. static String
generateUUIDFrom(String input)
Generates a UUID from a hash of the given input string. static String
ensureTrailingForwardSlash(String url)
Appends a trailing '/' if the provided URL does not end with '/' static String
quoted(Object object)
Returns the given object's toString()
surrounded by quotes.static String
firstChars(String string, int numberOfChars)
Returns the first numberOfChars
characters of the given string.static Array<byte>
readBytes(String url)
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 <T> Set<T>
copyIfNotNull(Set<T> set)
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. -
-
Method Detail
-
getOrDefault
static <T> T getOrDefault(T value, T defaultValue)
Returns the given value if it is not
null
, otherwise returns the given default 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
static <T> List<T> getOrDefault(List<T> list, List<T> defaultList)
Returns the given list if it is not
null
and not empty, otherwise returns the given default list.- 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
static <K, V> Map<K, V> getOrDefault(Map<K, V> map, Map<K, V> defaultMap)
Returns the given map if it is not
null
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
static <T> T getOrDefault(T value, Supplier<T> defaultValueSupplier)
Returns the given value if it is not
null
, otherwise returns the value returned by the given supplier.- 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
static boolean isNullOrBlank(String string)
Is the given string
null
or blank?- Parameters:
string
- The string to check.- Returns:
true if the string is
null
or blank.
-
isNullOrEmpty
static boolean isNullOrEmpty(String string)
Is the given string
null
or empty ("")?- Parameters:
string
- The string to check.- Returns:
true if the string is
null
or empty.
-
isNotNullOrBlank
static boolean isNotNullOrBlank(String string)
Is the given string not
null
and not blank?- Parameters:
string
- The string to check.- Returns:
true if there's something in the string.
-
isNotNullOrEmpty
static boolean isNotNullOrEmpty(String string)
Is the given string not
null
and not empty ("")?- Parameters:
string
- The string to check.- Returns:
true if the given string is not
null
and not empty ("")?
-
areNotNullOrBlank
static boolean areNotNullOrBlank(Array<String> strings)
Are all the given strings not
null
and not blank?- Parameters:
strings
- The strings to check.- Returns:
true
if every string is non-null
and non-empty.
-
isNullOrEmpty
static boolean isNullOrEmpty(Collection<out Object> collection)
Is the collection
null
or empty?- Parameters:
collection
- The collection to check.- Returns:
true
if the collection isnull
or isEmpty, otherwisefalse
.
-
isNullOrEmpty
static boolean isNullOrEmpty(Iterable<out Object> iterable)
Is the iterable object
null
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
static boolean isNullOrEmpty(Map<out Object, out Object> map)
Is the map object
null
or empty?- Parameters:
map
- The iterable object to check.- Returns:
true
if the map object isnull
or empty map, otherwisefalse
.
-
isCollectionEmpty
@Deprecated(forRemoval = true) static boolean isCollectionEmpty(Collection<out Object> collection)
- Parameters:
collection
- The collection to check.- Returns:
true
if the collection isnull
or empty,false
otherwise.
-
repeat
static String repeat(String string, int times)
Returns a string consisting of the given string repeated
times
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
static String randomUUID()
Returns a random UUID.
- Returns:
a UUID.
-
generateUUIDFrom
static String generateUUIDFrom(String input)
Generates a UUID from a hash of the given input string.
- Parameters:
input
- The input string.- Returns:
A UUID.
-
ensureTrailingForwardSlash
static String ensureTrailingForwardSlash(String url)
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
static String quoted(Object object)
Returns the given object's
toString()
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
static String firstChars(String string, int numberOfChars)
Returns the first
numberOfChars
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
static Array<byte> readBytes(String url)
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.
-
copyIfNotNull
static <T> Set<T> copyIfNotNull(Set<T> set)
Returns an (unmodifiable) copy of the provided set. Returns
null
if the provided set isnull
.- Parameters:
set
- The set to copy.- Returns:
The copy of the provided set.
-
copyIfNotNull
static <T> List<T> copyIfNotNull(List<T> list)
Returns an (unmodifiable) copy of the provided list. Returns
null
if the provided list isnull
.- Parameters:
list
- The list to copy.- Returns:
The copy of the provided list.
-
-
-
-