From 0421cc6d16317a15e221299510a41313b5ee2b5a Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Wed, 4 Mar 2020 23:07:56 +0100 Subject: [PATCH] Small formatter fixes, docs --- .../formatter/formatting/ChatFormatter.java | 15 ++++++++------- .../formatter/formatting/FormatSettings.java | 3 +++ .../formatter/formatting/MatchProvider.java | 3 +++ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/main/java/buttondevteam/chat/components/formatter/formatting/ChatFormatter.java b/src/main/java/buttondevteam/chat/components/formatter/formatting/ChatFormatter.java index 4606097..bee0798 100644 --- a/src/main/java/buttondevteam/chat/components/formatter/formatting/ChatFormatter.java +++ b/src/main/java/buttondevteam/chat/components/formatter/formatting/ChatFormatter.java @@ -10,12 +10,12 @@ import java.util.function.Predicate; import java.util.stream.Collectors; /** - * A {@link ChatFormatter} shows what formatting to use based on regular expressions. {@link ChatFormatter#Combine(List, String, TellrawPart, IHaveConfig, FormatSettings)}} is used to turn it into a {@link TellrawPart}, combining + * A {@link MatchProvider} finds where the given {@link FormatSettings} need to be applied. {@link ChatFormatter#Combine(List, String, TellrawPart, IHaveConfig, FormatSettings)}} is used to turn it into a {@link TellrawPart}, combining * intersecting parts found, for example when {@code _abc*def*ghi_} is said in chat, it'll turn it into an underlined part, then an underlined and italics part, finally an underlined part * again. * * @author NorbiPeti - */ //TODO: Update doc + */ public final class ChatFormatter { private ChatFormatter() { } @@ -25,7 +25,8 @@ public final class ChatFormatter { R apply(T1 x1, T2 x2, T3 x3); } - public static void Combine(List formatters, String str, TellrawPart tp, IHaveConfig config, FormatSettings defaults) { + //synchronized: Some of the formatters are reused, see createSections(...) + public static synchronized void Combine(List formatters, String str, TellrawPart tp, IHaveConfig config, FormatSettings defaults) { /* * A global formatter is no longer needed */ @@ -44,11 +45,11 @@ public final class ChatFormatter { sections.add(new FormattedSection(defaults, 0, str.length() - 1, Collections.emptyList())); //Add entire message var providers = formatters.stream().filter(mp -> mp instanceof RegexMatchProvider).collect(Collectors.toList()); - createSections(providers, str, sections, excluded, remchars, defaults); + createSections(providers, str, sections, excluded, remchars); providers = formatters.stream().filter(mp -> mp instanceof StringMatchProvider).collect(Collectors.toList()); - createSections(providers, str, sections, excluded, remchars, defaults); + createSections(providers, str, sections, excluded, remchars); providers = formatters.stream().filter(mp -> mp instanceof RangeMatchProvider).collect(Collectors.toList()); - createSections(providers, str, sections, excluded, remchars, defaults); + createSections(providers, str, sections, excluded, remchars); sortSections(sections); header("Section combining"); @@ -71,7 +72,7 @@ public final class ChatFormatter { } private static void createSections(List formatters, String str, ArrayList sections, - ArrayList excludedAreas, ArrayList removedCharacters, FormatSettings defaults) { + ArrayList excludedAreas, ArrayList removedCharacters) { formatters.forEach(MatchProviderBase::reset); //Reset state information, as we aren't doing deep cloning while (formatters.size() > 0) { for (var iterator = formatters.iterator(); iterator.hasNext(); ) { diff --git a/src/main/java/buttondevteam/chat/components/formatter/formatting/FormatSettings.java b/src/main/java/buttondevteam/chat/components/formatter/formatting/FormatSettings.java index f4fe9c7..e0bfa4c 100644 --- a/src/main/java/buttondevteam/chat/components/formatter/formatting/FormatSettings.java +++ b/src/main/java/buttondevteam/chat/components/formatter/formatting/FormatSettings.java @@ -4,6 +4,9 @@ import buttondevteam.lib.chat.Color; import lombok.Builder; import lombok.Data; +/** + * Describes how a matched section of the message should look. May be combined with other format settings. + */ @Data @Builder public class FormatSettings { diff --git a/src/main/java/buttondevteam/chat/components/formatter/formatting/MatchProvider.java b/src/main/java/buttondevteam/chat/components/formatter/formatting/MatchProvider.java index 13dafca..c1d2d46 100644 --- a/src/main/java/buttondevteam/chat/components/formatter/formatting/MatchProvider.java +++ b/src/main/java/buttondevteam/chat/components/formatter/formatting/MatchProvider.java @@ -3,6 +3,9 @@ package buttondevteam.chat.components.formatter.formatting; import javax.annotation.Nullable; import java.util.ArrayList; +/** + * Attempts to find a match for the provided message, returning null if none was found. + */ public interface MatchProvider { @Nullable FormattedSection getNextSection(String message, ArrayList ignoredAreas, ArrayList removedCharacters);