public final class CoreUtils extends Object
Modifier and Type | Method and Description |
---|---|
static <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)
Creates
HttpHeaders from the provided ClientOptions . |
static <T> org.reactivestreams.Publisher<T> |
extractAndFetch(PagedResponse<T> page,
Context context,
BiFunction<String,Context,org.reactivestreams.Publisher<T>> content)
Extracts and combines the generic items from all the pages linked together.
|
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 a
ClientOptions or HttpLogOptions . |
static Duration |
getDefaultTimeoutFromEnvironment(Configuration configuration,
String timeoutPropertyName,
Duration defaultTimeout,
ClientLogger logger)
Attempts to load an environment configured default timeout.
|
static Map<String,String> |
getProperties(String propertiesFileName)
Helper method that returns an immutable
Map of properties defined in propertiesFileName . |
static boolean |
isNullOrEmpty(CharSequence charSequence)
Checks if the character sequence 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 boolean |
isNullOrEmpty(Object[] array)
Checks if the array is null or empty.
|
public static byte[] clone(byte[] source)
source
- Array to make copy ofpublic static int[] clone(int[] source)
source
- Array to make copy ofpublic static <T> T[] clone(T[] source)
T
- Generic representing the type of the source array.source
- Array being copied.public static boolean isNullOrEmpty(Object[] array)
array
- Array being checked for nullness or emptiness.public static boolean isNullOrEmpty(Collection<?> collection)
collection
- Collection being checked for nullness or emptiness.public static boolean isNullOrEmpty(Map<?,?> map)
map
- Map being checked for nullness or emptiness.public static boolean isNullOrEmpty(CharSequence charSequence)
charSequence
- Character sequence being checked for nullness or emptiness.public static <T> String arrayToString(T[] array, Function<T,String> mapper)
T
- Generic representing the type of the array.array
- Array being formatted to a string.mapper
- Function that maps each element to a string.public static <T> T findFirstOfType(Object[] args, Class<T> clazz)
T
- Generic typeargs
- Array of objects to search through to find the first instance of the given `clazz` type.clazz
- The type trying to be found.public static <T> org.reactivestreams.Publisher<T> extractAndFetch(PagedResponse<T> page, Context context, BiFunction<String,Context,org.reactivestreams.Publisher<T>> content)
T
- The type of the item being returned by the paged response.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.public static Map<String,String> getProperties(String propertiesFileName)
Map
of properties defined in propertiesFileName
.propertiesFileName
- The file name defining the properties.Map
.public static String bomAwareToString(byte[] bytes, String contentType)
This utility method will attempt to find the encoding for the String in this order.
charset
in the Content-Type
header.UTF-8
.bytes
- Byte array.contentType
- Content-Type
header value.public static String getApplicationId(ClientOptions clientOptions, HttpLogOptions logOptions)
ClientOptions
or HttpLogOptions
.
This method first checks clientOptions
for having an application ID then logOptions
, finally
returning null if neither are set.
clientOptions
is checked first as logOptions
application ID is deprecated.
clientOptions
- The ClientOptions
.logOptions
- The HttpLogOptions
.clientOptions
or logOptions
, if neither are set null.public static HttpHeaders createHttpHeadersFromClientOptions(ClientOptions clientOptions)
HttpHeaders
from the provided ClientOptions
.
If clientOptions
is null or ClientOptions.getHeaders()
doesn't return any Header
values
null will be returned.
clientOptions
- The ClientOptions
used to create the HttpHeaders
.HttpHeaders
containing the Header
values from ClientOptions.getHeaders()
if
clientOptions
isn't null and contains Header
values, otherwise null.public static Duration getDefaultTimeoutFromEnvironment(Configuration configuration, String timeoutPropertyName, Duration defaultTimeout, ClientLogger logger)
If the environment default timeout isn't configured, defaultTimeout
will be returned. If the environment
default timeout is a string that isn't parseable by Long.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.
configuration
- The environment configurations.timeoutPropertyName
- The default timeout property name.defaultTimeout
- The fallback timeout to be used.logger
- A ClientLogger
to log exceptions.defaultTimeoutMillis
, or 0.Copyright © 2021 Microsoft Corporation. All rights reserved.