From 3432a33ae6d5ec82b9bb71dcb14bbaec7dd9c42b Mon Sep 17 00:00:00 2001 From: filoghost Date: Wed, 4 Aug 2021 21:32:36 +0200 Subject: [PATCH] Fix StringWithPlaceholders.containsPlaceholders() and update test --- .../plugin/hologram/tracking/DisplayText.java | 2 +- .../parsing/StringWithPlaceholders.java | 22 ++++++++++++++----- .../parsing/StringWithPlaceholdersTest.java | 5 ++--- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/DisplayText.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/DisplayText.java index 7a4b7b23..fecdad41 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/DisplayText.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/DisplayText.java @@ -60,7 +60,7 @@ class DisplayText { } boolean updateGlobalReplacements() { - if (!textWithoutReplacements.containsPlaceholders()) { + if (!textWithoutReplacements.containsUnreplacedPlaceholders()) { return false; } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/placeholder/parsing/StringWithPlaceholders.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/placeholder/parsing/StringWithPlaceholders.java index 491885eb..5ae26266 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/placeholder/parsing/StringWithPlaceholders.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/placeholder/parsing/StringWithPlaceholders.java @@ -40,12 +40,22 @@ public final class StringWithPlaceholders { return string; } - public boolean containsPlaceholders() { - return string != null && stringParts != null; + public boolean containsUnreplacedPlaceholders() { + if (string == null || stringParts == null) { + return false; + } + + for (StringPart stringPart : stringParts) { + if (stringPart instanceof PlaceholderPart) { + return true; + } + } + + return false; } public boolean anyPlaceholderMatch(Predicate filter) { - if (!containsPlaceholders()) { + if (stringParts == null) { return false; } @@ -62,7 +72,7 @@ public final class StringWithPlaceholders { } public boolean anyLiteralPartMatch(Predicate filter) { - if (!containsPlaceholders()) { + if (stringParts == null) { return filter.test(string); } @@ -78,7 +88,7 @@ public final class StringWithPlaceholders { } public @NotNull StringWithPlaceholders partiallyReplacePlaceholders(PlaceholderReplaceFunction replaceFunction) { - if (!containsPlaceholders()) { + if (stringParts == null) { return this; } @@ -110,7 +120,7 @@ public final class StringWithPlaceholders { } public String replaceParts(PlaceholderReplaceFunction placeholderReplaceFunction, UnaryOperator literalPartReplaceFunction) { - if (!containsPlaceholders()) { + if (string == null || stringParts == null) { return literalPartReplaceFunction.apply(string); } diff --git a/plugin/src/test/java/me/filoghost/holographicdisplays/plugin/placeholder/parsing/StringWithPlaceholdersTest.java b/plugin/src/test/java/me/filoghost/holographicdisplays/plugin/placeholder/parsing/StringWithPlaceholdersTest.java index 30917e0a..3ee2936b 100644 --- a/plugin/src/test/java/me/filoghost/holographicdisplays/plugin/placeholder/parsing/StringWithPlaceholdersTest.java +++ b/plugin/src/test/java/me/filoghost/holographicdisplays/plugin/placeholder/parsing/StringWithPlaceholdersTest.java @@ -25,18 +25,17 @@ class StringWithPlaceholdersTest { StringWithPlaceholders s = StringWithPlaceholders.of(input); assertThat(s.replacePlaceholders(occurrence -> "#")).isEqualTo(expectedOutput); - assertThat(s.containsPlaceholders()).isEqualTo(expectedContainsPlaceholders); + assertThat(s.containsUnreplacedPlaceholders()).isEqualTo(expectedContainsPlaceholders); } @ParameterizedTest(name = "[{index}] {0} -> {1}") @MethodSource("replacementsTestArguments") void partialReplacements(String input, String expectedOutput) { - boolean expectedContainsPlaceholders = expectedOutput.contains("#"); StringWithPlaceholders s = StringWithPlaceholders.of(input); assertThat(s.partiallyReplacePlaceholders(occurrence -> "#").getString()).isEqualTo(expectedOutput); assertThat(s.partiallyReplacePlaceholders(occurrence -> null).replacePlaceholders(occurrence -> "#")).isEqualTo(expectedOutput); - assertThat(s.containsPlaceholders()).isEqualTo(expectedContainsPlaceholders); + assertThat(s.partiallyReplacePlaceholders(occurrence -> "#").containsUnreplacedPlaceholders()).isFalse(); } static Stream replacementsTestArguments() {