java.lang.Object
org.sentrysoftware.metricshub.engine.common.helpers.StringHelper

public class StringHelper extends Object
Helper class for working with strings.
  • Constructor Details

    • StringHelper

      public StringHelper()
  • Method Details

    • getValue

      public static String getValue(Callable<Object> call, String defaultValue)
      Execute the given callable to get the resulting Object as String value.
      Parameters:
      call - Callable providing a value.
      defaultValue - The default value to return if the callable returns null or empty.
      Returns:
      String value.
    • getStackMessages

      public static String getStackMessages(Throwable throwable)
      Iterates over all the throwable causes and extract all the messages.
      Parameters:
      throwable - The Throwable instance we wish to process
      Returns:
      String value
    • prettyHttpHeaders

      public static String prettyHttpHeaders(Map<String,String> headers)
      Format the given HTTP headers map as the following example:
      Content-Type: application/json
      Connection: keep-alive
      Parameters:
      headers - Key-Value collection
      Returns:
      String value
    • addNonNull

      public static <T> void addNonNull(@NonNull @NonNull StringJoiner stringJoiner, @NonNull @NonNull String prefix, T value)
      Add the given prefix and value to the StringJoiner instance. null value is not added.
      Type Parameters:
      T - the type of the value to be added
      Parameters:
      stringJoiner - StringJoiner instance used to append the prefix and the value.
      prefix - The value prefix.
      value - The value to add.
    • replace

      public static String replace(@NonNull @NonNull String macro, @NonNull @NonNull Supplier<String> replacementSupplier, @NonNull @NonNull String template)
      Replace each substring of the template that matches the literal macro sequence with the value specified by the replacementSupplier.
      Parameters:
      macro - The sequence of char values to be replaced
      replacementSupplier - The supplier of the replacement sequence
      template - The template to replace
      Returns:
      String value
    • replace

      public static String replace(@NonNull @NonNull String macro, String replacement, @NonNull @NonNull String template)
      Replace each substring of the template that matches the literal macro sequence with the specified literal replacement sequence.
      Parameters:
      macro - The sequence of char values to be replaced
      replacement - The replacement sequence
      template - The template to replace
      Returns:
      String value
    • stringify

      public static String stringify(Object value)
      Convert the given value to a string representation. If the value is a collection or an array, it is transformed into a CSV (Comma-Separated Values) string.
      Parameters:
      value - The value to be converted to a string.
      Returns:
      The string representation of the value, or a CSV string if the value is a collection or an array.
    • nonNullNonBlank

      public static boolean nonNullNonBlank(String value)
      Checks if the specified string is non-null and non-blank.
      Parameters:
      value - The string to be checked.
      Returns:
      true if the string is non-null and contains at least one non-whitespace character, false otherwise.
    • validateConfigurationAttribute

      public static <T> void validateConfigurationAttribute(T attribute, Predicate<T> errorChecker, Supplier<String> messageSupplier) throws InvalidConfigurationException
      Validate the attribute against a predetermined test
      Parameters:
      attribute - Value getting compared
      errorChecker - Logic test comparing our value
      messageSupplier - error message supplier
      Throws:
      InvalidConfigurationException - in case the validation fail