Class CoreUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> String
arrayToString
(T[] array, Function<T, String> mapper) Turns an array into a string mapping each element to a string and delimits them using a coma.static String
bomAwareToString
(byte[] bytes, String contentType) Attempts to convert a byte stream into the properly encoded String.static byte[]
clone
(byte[] source) Creates a copy of the source byte array.static int[]
clone
(int[] source) Creates a copy of the source int array.static <T> T[]
clone
(T[] source) Creates a copy of the source array.static HttpHeaders
createHttpHeadersFromClientOptions
(ClientOptions clientOptions) CreatesHttpHeaders
from the providedClientOptions
.static <T> org.reactivestreams.Publisher<T>
extractAndFetch
(PagedResponse<T> page, Context context, BiFunction<String, Context, org.reactivestreams.Publisher<T>> content) Deprecated.Use localized implementation.static <T> T
findFirstOfType
(Object[] args, Class<T> clazz) Returns the first instance of the given class from an array of Objects.static String
getApplicationId
(ClientOptions clientOptions, HttpLogOptions logOptions) Retrieves the application ID from either aClientOptions
orHttpLogOptions
.static Duration
getDefaultTimeoutFromEnvironment
(Configuration configuration, String timeoutPropertyName, Duration defaultTimeout, ClientLogger logger) Attempts to load an environment configured default timeout.getProperties
(String propertiesFileName) Helper method that returns an immutableMap
of properties defined inpropertiesFileName
.static boolean
isNullOrEmpty
(CharSequence charSequence) Checks if the character sequence is null or empty.static boolean
isNullOrEmpty
(Object[] array) Checks if the array is null or empty.static boolean
isNullOrEmpty
(Collection<?> collection) Checks if the collection is null or empty.static boolean
isNullOrEmpty
(Map<?, ?> map) Checks if the map is null or empty.static Context
mergeContexts
(Context into, Context from) static String
stringJoin
(String delimiter, List<String> values) Optimized version ofString.join(CharSequence, Iterable)
when thevalues
has a small set of object.
-
Method Details
-
clone
public static byte[] clone(byte[] source) Creates a copy of the source byte array.- Parameters:
source
- Array to make copy of- Returns:
- A copy of the array, or null if source was null.
-
clone
public static int[] clone(int[] source) Creates a copy of the source int array.- Parameters:
source
- Array to make copy of- Returns:
- A copy of the array, or null if source was null.
-
clone
public static <T> T[] clone(T[] source) Creates a copy of the source array.- Type Parameters:
T
- Generic representing the type of the source array.- Parameters:
source
- Array being copied.- Returns:
- A copy of the array or null if source was null.
-
isNullOrEmpty
Checks if the array is null or empty.- Parameters:
array
- Array being checked for nullness or emptiness.- Returns:
- True if the array is null or empty, false otherwise.
-
isNullOrEmpty
Checks if the collection is null or empty.- Parameters:
collection
- Collection being checked for nullness or emptiness.- Returns:
- True if the collection is null or empty, false otherwise.
-
isNullOrEmpty
Checks if the map is null or empty.- Parameters:
map
- Map being checked for nullness or emptiness.- Returns:
- True if the map is null or empty, false otherwise.
-
isNullOrEmpty
Checks if the character sequence is null or empty.- Parameters:
charSequence
- Character sequence being checked for nullness or emptiness.- Returns:
- True if the character sequence is null or empty, false otherwise.
-
arrayToString
Turns an array into a string mapping each element to a string and delimits them using a coma.- Type Parameters:
T
- Generic representing the type of the array.- Parameters:
array
- Array being formatted to a string.mapper
- Function that maps each element to a string.- Returns:
- Array with each element mapped and delimited, otherwise null if the array is empty or null.
-
findFirstOfType
Returns the first instance of the given class from an array of Objects.- Type Parameters:
T
- Generic type- Parameters:
args
- Array of objects to search through to find the first instance of the given `clazz` type.clazz
- The type trying to be found.- Returns:
- The first object of the desired type, otherwise null.
-
extractAndFetch
@Deprecated public static <T> org.reactivestreams.Publisher<T> extractAndFetch(PagedResponse<T> page, Context context, BiFunction<String, Context, org.reactivestreams.Publisher<T>> content) Deprecated.Use localized implementation.Extracts and combines the generic items from all the pages linked together.- Type Parameters:
T
- The type of the item being returned by the paged response.- Parameters:
page
- The paged response from server holding generic items.context
- Metadata that is passed into the function that fetches the items from the next page.content
- The function which fetches items from the next page.- Returns:
- The publisher holding all the generic items combined.
-
getProperties
Helper method that returns an immutableMap
of properties defined inpropertiesFileName
.- Parameters:
propertiesFileName
- The file name defining the properties.- Returns:
- an immutable
Map
.
-
bomAwareToString
Attempts to convert a byte stream into the properly encoded String.This utility method will attempt to find the encoding for the String in this order.
- Find the byte order mark in the byte array.
- Find the
charset
in theContent-Type
header. - Default to
UTF-8
.
- Parameters:
bytes
- Byte array.contentType
-Content-Type
header value.- Returns:
- A string representation of the byte array encoded to the found encoding.
-
getApplicationId
Retrieves the application ID from either aClientOptions
orHttpLogOptions
.This method first checks
clientOptions
for having an application ID thenlogOptions
, finally returning null if neither are set.clientOptions
is checked first aslogOptions
application ID is deprecated.- Parameters:
clientOptions
- TheClientOptions
.logOptions
- TheHttpLogOptions
.- Returns:
- The application ID from either
clientOptions
orlogOptions
, if neither are set null.
-
createHttpHeadersFromClientOptions
CreatesHttpHeaders
from the providedClientOptions
.If
clientOptions
is null orClientOptions.getHeaders()
doesn't return anyHeader
values null will be returned.- Parameters:
clientOptions
- TheClientOptions
used to create theHttpHeaders
.- Returns:
HttpHeaders
containing theHeader
values fromClientOptions.getHeaders()
ifclientOptions
isn't null and containsHeader
values, otherwise null.
-
getDefaultTimeoutFromEnvironment
public static Duration getDefaultTimeoutFromEnvironment(Configuration configuration, String timeoutPropertyName, Duration defaultTimeout, ClientLogger logger) Attempts to load an environment configured default timeout.If the environment default timeout isn't configured,
defaultTimeout
will be returned. If the environment default timeout is a string that isn't parseable byLong.parseLong(String)
,defaultTimeout
will be returned. If the environment default timeout is less than 0,Duration.ZERO
will be returned indicated that there is no timeout period.- Parameters:
configuration
- The environment configurations.timeoutPropertyName
- The default timeout property name.defaultTimeout
- The fallback timeout to be used.logger
- AClientLogger
to log exceptions.- Returns:
- Either the environment configured default timeout,
defaultTimeoutMillis
, or 0.
-
mergeContexts
- Parameters:
into
- Context being merged into.from
- Context being merged.- Returns:
- A new Context that is the merged Contexts.
- Throws:
NullPointerException
- If eitherinto
orfrom
is null.
-
stringJoin
Optimized version ofString.join(CharSequence, Iterable)
when thevalues
has a small set of object.- Parameters:
delimiter
- Delimiter between the values.values
- The values to join.- Returns:
- The
values
joined delimited by thedelimiter
. - Throws:
NullPointerException
- Ifdelimiter
orvalues
is null.
-