Class StringHelper
java.lang.Object
org.sentrysoftware.metricshub.engine.common.helpers.StringHelper
Helper class for working with strings.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> voidaddNonNull(@NonNull StringJoiner stringJoiner, @NonNull String prefix, T value) Add the given prefix and value to theStringJoinerinstance.static StringgetStackMessages(Throwable throwable) Iterates over all the throwable causes and extract all the messages.static StringExecute the given callable to get the resulting Object as String value.static booleannonNullNonBlank(String value) Checks if the specified string is non-null and non-blank.static StringprettyHttpHeaders(Map<String, String> headers) Format the given HTTP headers map as the following example:
Content-Type: application/json
Connection: keep-alivestatic StringReplace each substring of thetemplatethat matches the literalmacrosequence with the specified literalreplacementsequence.static Stringreplace(@NonNull String macro, @NonNull Supplier<String> replacementSupplier, @NonNull String template) Replace each substring of thetemplatethat matches the literalmacrosequence with the value specified by thereplacementSupplier.static StringConvert the given value to a string representation.static <T> voidvalidateConfigurationAttribute(T attribute, Predicate<T> errorChecker, Supplier<String> messageSupplier) Validate the attribute against a predetermined test
-
Constructor Details
-
StringHelper
public StringHelper()
-
-
Method Details
-
getValue
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
Iterates over all the throwable causes and extract all the messages.- Parameters:
throwable- TheThrowableinstance we wish to process- Returns:
- String value
-
prettyHttpHeaders
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 theStringJoinerinstance.nullvalue is not added.- Type Parameters:
T- the type of the value to be added- Parameters:
stringJoiner-StringJoinerinstance 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 thetemplatethat matches the literalmacrosequence with the value specified by thereplacementSupplier.- Parameters:
macro- The sequence of char values to be replacedreplacementSupplier- The supplier of the replacement sequencetemplate- 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 thetemplatethat matches the literalmacrosequence with the specified literalreplacementsequence.- Parameters:
macro- The sequence of char values to be replacedreplacement- The replacement sequencetemplate- The template to replace- Returns:
- String value
-
stringify
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
Checks if the specified string is non-null and non-blank.- Parameters:
value- The string to be checked.- Returns:
trueif the string is non-null and contains at least one non-whitespace character,falseotherwise.
-
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 comparederrorChecker- Logic test comparing our valuemessageSupplier- error message supplier- Throws:
InvalidConfigurationException- in case the validation fail
-