Class CoreUtils
- java.lang.Object
-
- com.azure.core.util.CoreUtils
-
public final class CoreUtils extends Object
This class contains utility methods useful for building client libraries.
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static <T> StringarrayToString(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 StringbomAwareToString(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 HttpHeaderscreateHttpHeadersFromClientOptions(ClientOptions clientOptions)CreatesHttpHeadersfrom 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> TfindFirstOfType(Object[] args, Class<T> clazz)Returns the first instance of the given class from an array of Objects.static StringgetApplicationId(ClientOptions clientOptions, HttpLogOptions logOptions)Retrieves the application ID from either aClientOptionsorHttpLogOptions.static DurationgetDefaultTimeoutFromEnvironment(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 immutableMapof properties defined inpropertiesFileName.static booleanisNullOrEmpty(CharSequence charSequence)Checks if the character sequence is null or empty.static booleanisNullOrEmpty(Object[] array)Checks if the array is null or empty.static booleanisNullOrEmpty(Collection<?> collection)Checks if the collection is null or empty.static booleanisNullOrEmpty(Map<?,?> map)Checks if the map is null or empty.static ContextmergeContexts(Context into, Context from)
-
-
-
Method Detail
-
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
public static boolean isNullOrEmpty(Object[] array)
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
public static boolean isNullOrEmpty(Collection<?> collection)
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
public static boolean isNullOrEmpty(Map<?,?> map)
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
public static boolean isNullOrEmpty(CharSequence charSequence)
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
public 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.- 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
public static <T> T findFirstOfType(Object[] args, Class<T> clazz)
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
public static Map<String,String> getProperties(String propertiesFileName)
Helper method that returns an immutableMapof properties defined inpropertiesFileName.- Parameters:
propertiesFileName- The file name defining the properties.- Returns:
- an immutable
Map.
-
bomAwareToString
public static String bomAwareToString(byte[] bytes, String contentType)
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
charsetin theContent-Typeheader. - Default to
UTF-8.
- Parameters:
bytes- Byte array.contentType-Content-Typeheader value.- Returns:
- A string representation of the byte array encoded to the found encoding.
-
getApplicationId
public static String getApplicationId(ClientOptions clientOptions, HttpLogOptions logOptions)
Retrieves the application ID from either aClientOptionsorHttpLogOptions.This method first checks
clientOptionsfor having an application ID thenlogOptions, finally returning null if neither are set.clientOptionsis checked first aslogOptionsapplication ID is deprecated.- Parameters:
clientOptions- TheClientOptions.logOptions- TheHttpLogOptions.- Returns:
- The application ID from either
clientOptionsorlogOptions, if neither are set null.
-
createHttpHeadersFromClientOptions
public static HttpHeaders createHttpHeadersFromClientOptions(ClientOptions clientOptions)
CreatesHttpHeadersfrom the providedClientOptions.If
clientOptionsis null orClientOptions.getHeaders()doesn't return anyHeadervalues null will be returned.- Parameters:
clientOptions- TheClientOptionsused to create theHttpHeaders.- Returns:
HttpHeaderscontaining theHeadervalues fromClientOptions.getHeaders()ifclientOptionsisn't null and containsHeadervalues, 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,
defaultTimeoutwill be returned. If the environment default timeout is a string that isn't parseable byLong.parseLong(String),defaultTimeoutwill be returned. If the environment default timeout is less than 0,Duration.ZEROwill 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- AClientLoggerto log exceptions.- Returns:
- Either the environment configured default timeout,
defaultTimeoutMillis, or 0.
-
mergeContexts
public static Context mergeContexts(Context into, Context from)
- Parameters:
into- Context being merged into.from- Context being merged.- Returns:
- A new Context that is the merged Contexts.
- Throws:
NullPointerException- If eitherintoorfromis null.
-
-