Class Utils


  • public final class Utils
    extends java.lang.Object
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean allowIntrospection​(java.lang.Class<?> cls)  
      static <T> T asType​(EventStreamMessage x, com.fasterxml.jackson.databind.ObjectMapper mapper, com.fasterxml.jackson.core.type.TypeReference<T> typeReference)  
      static Utils.HttpResponseCached cache​(java.net.http.HttpResponse<java.io.InputStream> response)
      Fully reads the body of the given response and caches it in memory.
      static <N> N castLong​(long value, java.lang.Class<N> targetType)  
      static void checkArgument​(boolean expression, java.lang.String message)  
      static <T> T checkNotNull​(T object, java.lang.String name)  
      static HTTPRequest configureSecurity​(HTTPRequest request, java.lang.Object security)  
      static boolean contentTypeMatches​(java.lang.String contentType, java.lang.String pattern)  
      static java.lang.Object convertToShape​(java.lang.Object o, Utils.JsonShape shape, com.fasterxml.jackson.core.type.TypeReference<?> typeReference)  
      static com.fasterxml.jackson.databind.JavaType convertToStringShape​(com.fasterxml.jackson.databind.type.TypeFactory f, com.fasterxml.jackson.databind.JavaType a)  
      static java.net.http.HttpRequest.Builder copy​(java.net.http.HttpRequest request)
      Returns an HttpRequest.Builder which is initialized with the state of the given HttpRequest.
      static java.net.http.HttpRequest.Builder copy​(java.net.http.HttpRequest request, java.util.function.BiPredicate<java.lang.String,​java.lang.String> filter)
      Returns an HttpRequest.Builder which is initialized with the state of the given HttpRequest.
      static <T> java.util.concurrent.CompletableFuture<T> createAsyncApiError​(java.net.http.HttpResponse<Blob> response, java.lang.String reason)
      Creates a failed CompletableFuture with an async API exception.
      static HTTPClient createTestHTTPClient​(java.lang.String testName)  
      static HTTPClient createTestHTTPClient​(SpeakeasyHTTPClient client, java.lang.String testName)  
      static java.lang.String discriminatorToString​(java.lang.Object o)  
      static <K,​V>
      java.util.Map<K,​V>
      emptyMapIfNull​(java.util.Map<K,​V> map)  
      static boolean enhancedDeepEquals​(java.lang.Object a, java.lang.Object b)
      Returns true if and only if the two objects are deeply equal, uses mathematical equivalence for Number subclasses (2 == 2.0) instead of Number.equals.
      static int enhancedHash​(java.lang.Object... objects)
      Returns a combined hash code (applying enhancedHashCode(java.lang.Object)) for the given objects (usually the fields of an object whose hashCode we want to be calculated).
      static int enhancedHashCode​(java.lang.Object o)
      Returns a hash code that complies with the equals/hashCode contract when equals is implemented by enhancedDeepEquals(Object, Object).
      static java.lang.String environmentVariable​(java.lang.String name, java.lang.String defaultValue)
      Internal use.
      static byte[] extractByteArrayFromBody​(java.net.http.HttpResponse<java.io.InputStream> response)  
      static <T> java.lang.String generateURL​(java.lang.Class<T> type, java.lang.String baseURL, java.lang.String path, java.util.Optional<? extends T> params, Globals globals)  
      static <T> java.lang.String generateURL​(java.lang.Class<T> type, java.lang.String baseURL, java.lang.String path, org.openapitools.jackson.nullable.JsonNullable<? extends T> params, Globals globals)  
      static <T> java.lang.String generateURL​(java.lang.Class<T> type, java.lang.String baseURL, java.lang.String path, T params, Globals globals)  
      static java.lang.String generateURL​(java.lang.String baseURL, java.lang.String path)  
      static java.util.Map<java.lang.String,​java.util.List<java.lang.String>> getHeadersFromMetadata​(java.lang.Object headers, Globals globals)  
      static <T> java.util.List<QueryParameter> getQueryParams​(java.lang.Class<T> type, java.util.Optional<? extends T> params, Globals globals)  
      static <T> java.util.List<QueryParameter> getQueryParams​(java.lang.Class<T> type, org.openapitools.jackson.nullable.JsonNullable<? extends T> params, Globals globals)  
      static <T> java.util.List<QueryParameter> getQueryParams​(java.lang.Class<T> type, T params, Globals globals)  
      static boolean isPresentAndNotNull​(java.util.Optional<?> x)  
      static boolean isPresentAndNotNull​(org.openapitools.jackson.nullable.JsonNullable<?> x)  
      static java.lang.String json​(EventStreamMessage m, com.fasterxml.jackson.databind.ObjectMapper mapper, boolean dataIsPlainText)  
      static com.fasterxml.jackson.databind.ObjectMapper mapper()  
      static java.lang.Object populateGlobal​(java.lang.Object value, java.lang.String fieldName, java.lang.String paramType, Globals globals)  
      static java.lang.String prefixBearer​(java.lang.String authHeaderValue)  
      static byte[] readBytes​(java.io.File file)  
      static byte[] readBytes​(java.io.InputStream in)  
      static byte[] readBytes​(java.lang.String filename)  
      static byte[] readBytesAndClose​(java.io.InputStream in)  
      static <T> T readDefaultOrConstValue​(java.lang.String name, java.lang.String json, com.fasterxml.jackson.core.type.TypeReference<T> typeReference)  
      static java.lang.String readString​(java.io.File file)  
      static java.lang.String readString​(java.lang.String filename)  
      static void recordTest​(java.lang.String id)  
      static boolean referenceEquals​(java.lang.Object a, java.lang.Object b)  
      static java.lang.Object resolveOptionals​(java.lang.Object o)  
      static SerializedBody serializeRequestBody​(java.lang.Object request, java.lang.String requestField, java.lang.String serializationMethod, boolean nullable)  
      static java.lang.String sessionKey​(java.lang.String... items)  
      static void setSseSentinel​(java.lang.Object o, java.lang.String value)  
      static java.lang.String sortJSONObjectKeys​(java.lang.String json, java.lang.String... fields)  
      static java.lang.String sortQueryParameters​(java.lang.String url)
      Returns an equivalent url with query parameters sorted by name.
      static java.lang.Object sortSerializedMaps​(java.lang.Object input, java.lang.String regex, java.lang.String delim)  
      static boolean statusCodeMatches​(int statusCode, java.lang.String... expectedStatusCodes)  
      static boolean statusCodeMatchesOne​(int statusCode, java.lang.String expectedStatusCode)  
      static <T> java.util.stream.Stream<T> stream​(java.util.concurrent.Callable<java.util.Optional<T>> first, Utils.Function<T,​java.util.Optional<T>> next)  
      static java.lang.String templateUrl​(java.lang.String url, java.util.Map<java.lang.String,​java.lang.String> params)  
      static byte[] toByteArrayAndClose​(java.io.InputStream in)  
      static java.lang.String toHex​(byte[] bytes)  
      static java.util.List<?> toList​(java.lang.Object o)  
      static <T> java.util.Optional<T> toOptional​(org.openapitools.jackson.nullable.JsonNullable<T> a)  
      static <T> java.util.stream.Stream<T> toStream​(java.lang.Iterable<T> iterable)  
      static java.lang.String toString​(java.lang.Class<?> cls, java.lang.Object... items)  
      static java.lang.String toUtf8AndClose​(java.io.InputStream in)  
      static <I,​O>
      java.util.Iterator<O>
      transform​(java.util.Iterator<I> iterator, Utils.Function<I,​O> mapper)  
      static java.lang.String valToString​(java.lang.Object value)  
      static <T> T valueOrElse​(java.util.Optional<T> value, T valueIfNotPresent)  
      static <T> T valueOrElse​(org.openapitools.jackson.nullable.JsonNullable<T> value, T valueIfNotPresent)  
      static <T> T valueOrElse​(T value, T valueIfNotPresent)  
      static <T> T valueOrNull​(java.util.Optional<T> value)  
      static <T> T valueOrNull​(org.openapitools.jackson.nullable.JsonNullable<T> value)  
      static <T> T valueOrNull​(T value)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • referenceEquals

        public static boolean referenceEquals​(java.lang.Object a,
                                              java.lang.Object b)
      • generateURL

        public static java.lang.String generateURL​(java.lang.String baseURL,
                                                   java.lang.String path)
                                            throws java.lang.IllegalArgumentException
        Throws:
        java.lang.IllegalArgumentException
      • generateURL

        public static <T> java.lang.String generateURL​(java.lang.Class<T> type,
                                                       java.lang.String baseURL,
                                                       java.lang.String path,
                                                       org.openapitools.jackson.nullable.JsonNullable<? extends T> params,
                                                       Globals globals)
                                                throws com.fasterxml.jackson.core.JsonProcessingException,
                                                       java.lang.IllegalArgumentException,
                                                       java.lang.IllegalAccessException
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException
        java.lang.IllegalArgumentException
        java.lang.IllegalAccessException
      • generateURL

        public static <T> java.lang.String generateURL​(java.lang.Class<T> type,
                                                       java.lang.String baseURL,
                                                       java.lang.String path,
                                                       java.util.Optional<? extends T> params,
                                                       Globals globals)
                                                throws com.fasterxml.jackson.core.JsonProcessingException,
                                                       java.lang.IllegalArgumentException,
                                                       java.lang.IllegalAccessException
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException
        java.lang.IllegalArgumentException
        java.lang.IllegalAccessException
      • generateURL

        public static <T> java.lang.String generateURL​(java.lang.Class<T> type,
                                                       java.lang.String baseURL,
                                                       java.lang.String path,
                                                       T params,
                                                       Globals globals)
                                                throws java.lang.IllegalArgumentException,
                                                       java.lang.IllegalAccessException,
                                                       com.fasterxml.jackson.core.JsonProcessingException
        Throws:
        java.lang.IllegalArgumentException
        java.lang.IllegalAccessException
        com.fasterxml.jackson.core.JsonProcessingException
      • contentTypeMatches

        public static boolean contentTypeMatches​(java.lang.String contentType,
                                                 java.lang.String pattern)
      • allowIntrospection

        public static boolean allowIntrospection​(java.lang.Class<?> cls)
      • serializeRequestBody

        public static SerializedBody serializeRequestBody​(java.lang.Object request,
                                                          java.lang.String requestField,
                                                          java.lang.String serializationMethod,
                                                          boolean nullable)
                                                   throws java.lang.NoSuchFieldException,
                                                          java.lang.IllegalArgumentException,
                                                          java.lang.IllegalAccessException,
                                                          java.lang.UnsupportedOperationException,
                                                          java.io.IOException
        Throws:
        java.lang.NoSuchFieldException
        java.lang.IllegalArgumentException
        java.lang.IllegalAccessException
        java.lang.UnsupportedOperationException
        java.io.IOException
      • getQueryParams

        public static <T> java.util.List<QueryParameter> getQueryParams​(java.lang.Class<T> type,
                                                                        java.util.Optional<? extends T> params,
                                                                        Globals globals)
                                                                 throws java.lang.Exception
        Throws:
        java.lang.Exception
      • getQueryParams

        public static <T> java.util.List<QueryParameter> getQueryParams​(java.lang.Class<T> type,
                                                                        org.openapitools.jackson.nullable.JsonNullable<? extends T> params,
                                                                        Globals globals)
                                                                 throws java.lang.Exception
        Throws:
        java.lang.Exception
      • getQueryParams

        public static <T> java.util.List<QueryParameter> getQueryParams​(java.lang.Class<T> type,
                                                                        T params,
                                                                        Globals globals)
                                                                 throws java.lang.Exception
        Throws:
        java.lang.Exception
      • configureSecurity

        public static HTTPRequest configureSecurity​(HTTPRequest request,
                                                    java.lang.Object security)
                                             throws java.lang.Exception
        Throws:
        java.lang.Exception
      • templateUrl

        public static java.lang.String templateUrl​(java.lang.String url,
                                                   java.util.Map<java.lang.String,​java.lang.String> params)
      • getHeadersFromMetadata

        public static java.util.Map<java.lang.String,​java.util.List<java.lang.String>> getHeadersFromMetadata​(java.lang.Object headers,
                                                                                                                    Globals globals)
                                                                                                             throws java.lang.Exception
        Throws:
        java.lang.Exception
      • valToString

        public static java.lang.String valToString​(java.lang.Object value)
      • prefixBearer

        public static java.lang.String prefixBearer​(java.lang.String authHeaderValue)
      • populateGlobal

        public static java.lang.Object populateGlobal​(java.lang.Object value,
                                                      java.lang.String fieldName,
                                                      java.lang.String paramType,
                                                      Globals globals)
      • checkNotNull

        public static <T> T checkNotNull​(T object,
                                         java.lang.String name)
      • checkArgument

        public static void checkArgument​(boolean expression,
                                         java.lang.String message)
      • emptyMapIfNull

        public static <K,​V> java.util.Map<K,​V> emptyMapIfNull​(java.util.Map<K,​V> map)
      • toString

        public static java.lang.String toString​(java.lang.Class<?> cls,
                                                java.lang.Object... items)
      • resolveOptionals

        public static java.lang.Object resolveOptionals​(java.lang.Object o)
      • toList

        public static java.util.List<?> toList​(java.lang.Object o)
      • readDefaultOrConstValue

        public static <T> T readDefaultOrConstValue​(java.lang.String name,
                                                    java.lang.String json,
                                                    com.fasterxml.jackson.core.type.TypeReference<T> typeReference)
      • extractByteArrayFromBody

        public static byte[] extractByteArrayFromBody​(java.net.http.HttpResponse<java.io.InputStream> response)
                                               throws java.io.IOException
        Throws:
        java.io.IOException
      • toByteArrayAndClose

        public static byte[] toByteArrayAndClose​(java.io.InputStream in)
                                          throws java.io.IOException
        Throws:
        java.io.IOException
      • toUtf8AndClose

        public static java.lang.String toUtf8AndClose​(java.io.InputStream in)
                                               throws java.io.IOException
        Throws:
        java.io.IOException
      • convertToShape

        public static java.lang.Object convertToShape​(java.lang.Object o,
                                                      Utils.JsonShape shape,
                                                      com.fasterxml.jackson.core.type.TypeReference<?> typeReference)
      • convertToStringShape

        public static com.fasterxml.jackson.databind.JavaType convertToStringShape​(com.fasterxml.jackson.databind.type.TypeFactory f,
                                                                                   com.fasterxml.jackson.databind.JavaType a)
      • stream

        public static <T> java.util.stream.Stream<T> stream​(java.util.concurrent.Callable<java.util.Optional<T>> first,
                                                            Utils.Function<T,​java.util.Optional<T>> next)
      • toStream

        public static <T> java.util.stream.Stream<T> toStream​(java.lang.Iterable<T> iterable)
      • statusCodeMatches

        public static boolean statusCodeMatches​(int statusCode,
                                                java.lang.String... expectedStatusCodes)
      • statusCodeMatchesOne

        public static boolean statusCodeMatchesOne​(int statusCode,
                                                   java.lang.String expectedStatusCode)
      • copy

        public static java.net.http.HttpRequest.Builder copy​(java.net.http.HttpRequest request)
        Returns an HttpRequest.Builder which is initialized with the state of the given HttpRequest.
        Parameters:
        request - request to copy
        Returns:
        a builder initialized with values from request
      • copy

        public static java.net.http.HttpRequest.Builder copy​(java.net.http.HttpRequest request,
                                                             java.util.function.BiPredicate<java.lang.String,​java.lang.String> filter)
        Returns an HttpRequest.Builder which is initialized with the state of the given HttpRequest.
        Parameters:
        request - request to copy
        filter - selects which header key-values to include in the copied request
        Returns:
        a builder initialized with values from request
      • mapper

        public static com.fasterxml.jackson.databind.ObjectMapper mapper()
      • asType

        public static <T> T asType​(EventStreamMessage x,
                                   com.fasterxml.jackson.databind.ObjectMapper mapper,
                                   com.fasterxml.jackson.core.type.TypeReference<T> typeReference)
      • json

        public static java.lang.String json​(EventStreamMessage m,
                                            com.fasterxml.jackson.databind.ObjectMapper mapper,
                                            boolean dataIsPlainText)
                                     throws com.fasterxml.jackson.core.JsonProcessingException
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException
      • cache

        public static Utils.HttpResponseCached cache​(java.net.http.HttpResponse<java.io.InputStream> response)
                                              throws java.io.IOException
        Fully reads the body of the given response and caches it in memory. The returned response has utility methods to view the body (bodyAsUtf8(), bodyAsBytes() and the body() method can be called multiple times, each returning a fresh InputStream that will read from the cached byte array.

        This method is most likely to be used in a diagnostic/logging situtation so that the contents of a response can be viewed without affecting processing. Using this method with a very large body may be problematic in terms of memory use.

        Parameters:
        response - response to cache
        Returns:
        response with a cached body
        Throws:
        java.io.IOException
      • readBytes

        public static byte[] readBytes​(java.lang.String filename)
      • readString

        public static java.lang.String readString​(java.lang.String filename)
      • readBytes

        public static byte[] readBytes​(java.io.File file)
      • readString

        public static java.lang.String readString​(java.io.File file)
      • readBytesAndClose

        public static byte[] readBytesAndClose​(java.io.InputStream in)
      • readBytes

        public static byte[] readBytes​(java.io.InputStream in)
      • toHex

        public static java.lang.String toHex​(byte[] bytes)
      • discriminatorToString

        public static java.lang.String discriminatorToString​(java.lang.Object o)
      • recordTest

        public static void recordTest​(java.lang.String id)
      • sortQueryParameters

        public static java.lang.String sortQueryParameters​(java.lang.String url)
        Returns an equivalent url with query parameters sorted by name. Sort is stable in that parameters with the same name will not be reordered.
        Parameters:
        url - input
        Returns:
        url with query parameters sorted by name
      • sortSerializedMaps

        public static java.lang.Object sortSerializedMaps​(java.lang.Object input,
                                                          java.lang.String regex,
                                                          java.lang.String delim)
      • isPresentAndNotNull

        public static boolean isPresentAndNotNull​(java.util.Optional<?> x)
      • isPresentAndNotNull

        public static boolean isPresentAndNotNull​(org.openapitools.jackson.nullable.JsonNullable<?> x)
      • setSseSentinel

        public static void setSseSentinel​(java.lang.Object o,
                                          java.lang.String value)
      • sessionKey

        public static java.lang.String sessionKey​(java.lang.String... items)
      • createTestHTTPClient

        public static HTTPClient createTestHTTPClient​(java.lang.String testName)
      • environmentVariable

        public static java.lang.String environmentVariable​(java.lang.String name,
                                                           java.lang.String defaultValue)
        Internal use. Returns the system property with key = "env." + name and if doesn't exist returns the value of the environment variable with the given name of if it doesn't exist returns defaultValue.
        Parameters:
        name - variable name
        defaultValue - default value if system property and environment variable don't exist
        Returns:
        system property with name prepended with ".env" or environment variable of given name or default value
      • toOptional

        public static <T> java.util.Optional<T> toOptional​(org.openapitools.jackson.nullable.JsonNullable<T> a)
      • sortJSONObjectKeys

        public static java.lang.String sortJSONObjectKeys​(java.lang.String json,
                                                          java.lang.String... fields)
      • valueOrElse

        public static <T> T valueOrElse​(T value,
                                        T valueIfNotPresent)
      • valueOrElse

        public static <T> T valueOrElse​(java.util.Optional<T> value,
                                        T valueIfNotPresent)
      • valueOrElse

        public static <T> T valueOrElse​(org.openapitools.jackson.nullable.JsonNullable<T> value,
                                        T valueIfNotPresent)
      • valueOrNull

        public static <T> T valueOrNull​(T value)
      • valueOrNull

        public static <T> T valueOrNull​(java.util.Optional<T> value)
      • valueOrNull

        public static <T> T valueOrNull​(org.openapitools.jackson.nullable.JsonNullable<T> value)
      • castLong

        public static <N> N castLong​(long value,
                                     java.lang.Class<N> targetType)
      • transform

        public static <I,​O> java.util.Iterator<O> transform​(java.util.Iterator<I> iterator,
                                                                  Utils.Function<I,​O> mapper)
      • enhancedDeepEquals

        public static boolean enhancedDeepEquals​(java.lang.Object a,
                                                 java.lang.Object b)
        Returns true if and only if the two objects are deeply equal, uses mathematical equivalence for Number subclasses (2 == 2.0) instead of Number.equals.

        Should be paired with enhancedHashCode(Object) to ensure the equals/hashCode contract.

        Parameters:
        a - the first object to compare
        b - the second object to compare
        Returns:
        true if the objects are deeply equal bearing in mind mathematical equivalence, false otherwise
      • enhancedHash

        public static int enhancedHash​(java.lang.Object... objects)
        Returns a combined hash code (applying enhancedHashCode(java.lang.Object)) for the given objects (usually the fields of an object whose hashCode we want to be calculated).
        Parameters:
        objects -
        Returns:
        combined hash code for the objects, 0 if the objects are null
      • enhancedHashCode

        public static int enhancedHashCode​(java.lang.Object o)
        Returns a hash code that complies with the equals/hashCode contract when equals is implemented by enhancedDeepEquals(Object, Object).
        Parameters:
        o - object to calculate the hash code for (can be null)
        Returns:
        hash code for the object, 0 if the object is null
      • createAsyncApiError

        public static <T> java.util.concurrent.CompletableFuture<T> createAsyncApiError​(java.net.http.HttpResponse<Blob> response,
                                                                                        java.lang.String reason)
        Creates a failed CompletableFuture with an async API exception. Uses the Blob to read the response body asynchronously.