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 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

      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 immutable Map of properties defined in propertiesFileName.
      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.

      1. Find the byte order mark in the byte array.
      2. Find the charset in the Content-Type header.
      3. 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

      public static String getApplicationId(ClientOptions clientOptions, HttpLogOptions logOptions)
      Retrieves the application ID from either a 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.

      Parameters:
      clientOptions - The ClientOptions.
      logOptions - The HttpLogOptions.
      Returns:
      The application ID from either clientOptions or logOptions, if neither are set null.
    • createHttpHeadersFromClientOptions

      public static HttpHeaders createHttpHeadersFromClientOptions(ClientOptions clientOptions)
      Creates HttpHeaders from the provided ClientOptions.

      If clientOptions is null or ClientOptions.getHeaders() doesn't return any Header values null will be returned.

      Parameters:
      clientOptions - The ClientOptions used to create the HttpHeaders.
      Returns:
      HttpHeaders containing the Header values from ClientOptions.getHeaders() if clientOptions isn't null and contains Header 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 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.

      Parameters:
      configuration - The environment configurations.
      timeoutPropertyName - The default timeout property name.
      defaultTimeout - The fallback timeout to be used.
      logger - A ClientLogger to log exceptions.
      Returns:
      Either the environment configured default timeout, defaultTimeoutMillis, or 0.
    • mergeContexts

      public static Context mergeContexts(Context into, Context from)
      Merges two Contexts into a new Context.
      Parameters:
      into - Context being merged into.
      from - Context being merged.
      Returns:
      A new Context that is the merged Contexts.
      Throws:
      NullPointerException - If either into or from is null.