From f1287698dbb417727b3e26973b3f8bc9caf5ea36 Mon Sep 17 00:00:00 2001 From: Maxim Date: Tue, 10 Oct 2023 15:36:30 +0200 Subject: [PATCH] v0.1.2 Typos fixes String utils alteration: Allow empty maps for named format argument Blanks and dashes as default values support for named format --- README.md | 2 +- pom.xml | 2 +- .../java/com/hijackermax/utils/lang/FunctionalUtils.java | 6 +++--- src/main/java/com/hijackermax/utils/lang/StringUtils.java | 6 +++--- .../java/com/hijackermax/utils/lang/StringUtilsTest.java | 8 +++++--- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 27d9658..cd8918c 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,6 @@ Just add this to your **pom.xml** com.hijackermax utils - 0.1.0 + 0.1.2 ``` \ No newline at end of file diff --git a/pom.xml b/pom.xml index 5a75032..73231e0 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.hijackermax utils - 0.1.0 + 0.1.2 utils A set of utils that can help in app development diff --git a/src/main/java/com/hijackermax/utils/lang/FunctionalUtils.java b/src/main/java/com/hijackermax/utils/lang/FunctionalUtils.java index c215503..375c5f5 100644 --- a/src/main/java/com/hijackermax/utils/lang/FunctionalUtils.java +++ b/src/main/java/com/hijackermax/utils/lang/FunctionalUtils.java @@ -16,7 +16,7 @@ private FunctionalUtils() { } /** - * Provides wrapper {@link Consumer} which cathes {@link Exception} thrown by the input value {@link Consumer} + * Provides wrapper {@link Consumer} which catches {@link Exception} thrown by the input value {@link Consumer} * and supplies it to provided exception{@link BiConsumer} * * @param consumer value {@link Consumer} that can throw {@link Exception} @@ -36,7 +36,7 @@ public static Consumer failSafeStrategy(Consumer consumer, BiConsumer< } /** - * Cathes {@link Exception} thrown by provided {@link Runnable} and supplies it to provided exception{@link Consumer} + * Catches {@link Exception} thrown by provided {@link Runnable} and supplies it to provided exception{@link Consumer} * * @param runnable {@link Runnable} that can throw {@link Exception} * @param exceptionConsumer thrown {@link Exception} {@link Consumer} @@ -52,7 +52,7 @@ public static void failSafeStrategy(Runnable runnable, } /** - * Provides wrapper {@link Callable} which cathes {@link Exception} thrown by the provided {@link Callable} + * Provides wrapper {@link Callable} which catches {@link Exception} thrown by the provided {@link Callable} * and supplies it to provided exception{@link Consumer}, * in case of failure return invoke result of fallback {@link Supplier} * diff --git a/src/main/java/com/hijackermax/utils/lang/StringUtils.java b/src/main/java/com/hijackermax/utils/lang/StringUtils.java index 8c25f23..18ccdc5 100644 --- a/src/main/java/com/hijackermax/utils/lang/StringUtils.java +++ b/src/main/java/com/hijackermax/utils/lang/StringUtils.java @@ -78,7 +78,7 @@ public final class StringUtils { private static final Pattern WHITESPACES_PATTERN = Pattern.compile("\\s+"); private static final Pattern NON_DIGITS_PATTERN = Pattern.compile("[^\\d.]"); private static final Pattern DIGITS_PATTERN = Pattern.compile("[^\\D.]"); - private static final Pattern NAMED_FORMAT_PATTERN = Pattern.compile("(?iu)(?:\\$\\{(?:(\\w+)(?:\\?`([^`]+[\\p{Alnum}\\p{IsAlnum}\\p{Punct}\\p{IsPunct}']+[\\s]?[^`]+)`)?(%\\w+)?)\\})"); + private static final Pattern NAMED_FORMAT_PATTERN = Pattern.compile("(?iu)(?:\\$\\{(?:(\\w+)(?:\\?`([^``]*|-|[^`]+[\\p{Alnum}\\p{IsAlnum}\\p{Punct}\\p{IsPunct}']+[\\s]?[^`]+)`)?(%\\w+)?)\\})"); private StringUtils() { } @@ -653,14 +653,14 @@ public static String notEmptyOrElse(String value, String defaultValue) { * @param values {@link Map} of values that should replace tokens in provided template * @return formatted string with provided values map, * or empty string if provided template is empty or null, - * or template if it is blank or values map is empty or null + * or template if it is blank or values map is null * @since 0.1.0 */ public static String namedFormat(String template, Map values) { if (isEmpty(template)) { return EMPTY; } - if (isBlank(template) || CollectionUtils.isEmpty(values)) { + if (isBlank(template) || Objects.isNull(values)) { return template; } Transformer transformer = Transformer.of(template); diff --git a/src/test/java/com/hijackermax/utils/lang/StringUtilsTest.java b/src/test/java/com/hijackermax/utils/lang/StringUtilsTest.java index 3a9f162..db1f10c 100644 --- a/src/test/java/com/hijackermax/utils/lang/StringUtilsTest.java +++ b/src/test/java/com/hijackermax/utils/lang/StringUtilsTest.java @@ -441,12 +441,12 @@ void testNamedFormat() { ); assertEquals("Foo bar: ff = 255 128 % Test", StringUtils.namedFormat(template, values)); - String spacesInDefault = "Foo ${text_default?``}: ${numeric%x} = ${numeric} ${numeric_default?`@[ 128]$^`%x} % ${text} ${missing}"; + String spacesInDefault = "Foo ${text_default?``}: ${numeric%x} = ${numeric} ${numeric_default?`@[ 128]$^`%x} % ${text} ${missing}"; Map values2 = Map.of( "numeric", 255, "text", "Test" ); - assertEquals("Foo : ff = 255 @[ 128]$^ % Test ${missing}", StringUtils.namedFormat(spacesInDefault, values2)); + assertEquals("Foo : ff = 255 @[ 128]$^ % Test ${missing}", StringUtils.namedFormat(spacesInDefault, values2)); } @Test @@ -454,7 +454,9 @@ void testTransferValues() { Pattern pattern = Pattern.compile("(?iu)(?:(\\$\\{(?\\w+)?\\})|(#\\{(?\\w+)?\\})|(%\\{(?\\w+)?\\}))"); assertEquals(StringUtils.EMPTY, StringUtils.transferValues(null, null, null)); assertEquals(StringUtils.EMPTY, StringUtils.transferValues(pattern, StringUtils.BLANK, StringUtils.BLANK)); - assertEquals("Foo ${value1} bar ${value2} ${value3} ${value4?`Test Test`}", StringUtils.transferValues(Pattern.compile(""), "Foo ${value_one} %{value_three}", "Foo ${value1} bar ${value2} ${value3} ${value4?`Test Test`}")); + assertEquals("Foo ${value1} bar ${value2} ${value3} Test Test", StringUtils.transferValues(Pattern.compile(""), "Foo ${value_one} %{value_three}", "Foo ${value1} bar ${value2} ${value3} ${value4?`Test Test`}")); + assertEquals("Foo ${value1} bar ${value2} ${value3} -", StringUtils.transferValues(Pattern.compile(""), "Foo ${value_one} %{value_three}", "Foo ${value1} bar ${value2} ${value3} ${value4?`-`}")); + assertEquals("Foo bar ${value2} ${value3} -", StringUtils.transferValues(Pattern.compile(""), "Foo ${value_one} %{value_three}", "Foo ${value1?``} bar ${value2} ${value3} ${value4?`-`}")); assertEquals("Foo value_one bar value_two default", StringUtils.transferValues(pattern, "Foo ${value_one} #{value_two}", "Foo ${value1} bar ${value2} ${value4?`default`}")); assertEquals("Foo value_one bar ${value2} value_three Test Test", StringUtils.transferValues(pattern, "Foo ${value_one} %{value_three}", "Foo ${value1} bar ${value2} ${value3} ${value4?`Test Test`}")); }