Class Utils

java.lang.Object
com.google.genai.gaos.utils.Utils

public final class Utils extends Object
  • Method Details

    • referenceEquals

      public static boolean referenceEquals(Object a, Object b)
    • generateURL

      public static String generateURL(String baseURL, String path) throws IllegalArgumentException
      Throws:
      IllegalArgumentException
    • generateURL

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

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

      public static boolean contentTypeMatches(String contentType, String pattern)
    • allowIntrospection

      public static boolean allowIntrospection(Class<?> cls)
    • serializeRequestBody

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

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

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

      public static HTTPRequest configureSecurity(HTTPRequest request, Object security, String... allowedFields) throws Exception
      Throws:
      Exception
    • templateUrl

      public static String templateUrl(String url, Map<String,String> params)
    • getHeadersFromMetadata

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

      public static String valToString(Object value)
    • prefixBearer

      public static String prefixBearer(String authHeaderValue)
    • populateGlobal

      public static Object populateGlobal(Object value, String fieldName, String paramType, Globals globals)
    • checkNotNull

      public static <T> T checkNotNull(T object, String name)
    • checkArgument

      public static void checkArgument(boolean expression, String message)
    • emptyMapIfNull

      public static <K, V> Map<K,V> emptyMapIfNull(Map<K,V> map)
    • toString

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

      public static Object resolveOptionals(Object o)
    • toList

      public static List<?> toList(Object o)
    • readDefaultOrConstValue

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

      public static byte[] extractByteArrayFromBody(HttpResponse<InputStream> response) throws IOException
      Throws:
      IOException
    • toByteArrayAndClose

      public static byte[] toByteArrayAndClose(InputStream in) throws IOException
      Throws:
      IOException
    • closeQuietly

      public static void closeQuietly(InputStream in)
    • toUtf8AndClose

      public static String toUtf8AndClose(InputStream in) throws IOException
      Throws:
      IOException
    • convertToShape

      public static Object convertToShape(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> Stream<T> stream(Callable<Optional<T>> first, Utils.Function<T,Optional<T>> next)
    • toStream

      public static <T> Stream<T> toStream(Iterable<T> iterable)
    • statusCodeMatches

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

      public static boolean statusCodeMatchesOne(int statusCode, String expectedStatusCode)
    • 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 String json(EventStreamMessage m, com.fasterxml.jackson.databind.ObjectMapper mapper, boolean dataIsPlainText) throws com.fasterxml.jackson.core.JsonProcessingException
      Throws:
      com.fasterxml.jackson.core.JsonProcessingException
    • readBytes

      public static byte[] readBytes(String filename)
    • readString

      public static String readString(String filename)
    • readBytes

      public static byte[] readBytes(File file)
    • readString

      public static String readString(File file)
    • readBytesAndClose

      public static byte[] readBytesAndClose(InputStream in)
    • readBytes

      public static byte[] readBytes(InputStream in)
    • toHex

      public static String toHex(byte[] bytes)
    • discriminatorToString

      public static String discriminatorToString(Object o)
    • recordTest

      public static void recordTest(String id)
    • sortQueryParameters

      public static String sortQueryParameters(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 Object sortSerializedMaps(Object input, String regex, String delim)
    • isPresentAndNotNull

      public static boolean isPresentAndNotNull(Optional<?> x)
    • setSseSentinel

      public static void setSseSentinel(Object o, String value)
    • sessionKey

      public static String sessionKey(String... items)
    • createTestHTTPClient

      public static HTTPClient createTestHTTPClient(String testName)
    • createTestHTTPClient

      public static HTTPClient createTestHTTPClient(SpeakeasyHTTPClient client, String testName)
    • environmentVariable

      public static String environmentVariable(String name, 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
    • sortJSONObjectKeys

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

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

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

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

      public static <T> T valueOrNull(Optional<T> value)
    • castLong

      public static <N> N castLong(long value, Class<N> targetType)
    • transform

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

      public static boolean enhancedDeepEquals(Object a, 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(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(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
    • unmarshal

      public static <T> T unmarshal(HttpResponse<InputStream> response, com.fasterxml.jackson.core.type.TypeReference<T> typeReference)