From a46e066669a07892ccd3c4b9133fe5b22b2d08d1 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Tue, 20 Jun 2023 21:32:52 +0200 Subject: [PATCH] also new ChatUtil.join() method --- .../java/fr/pandacube/lib/chat/ChatUtil.java | 35 +++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatUtil.java b/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatUtil.java index 89f74ba..8f33dbb 100644 --- a/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatUtil.java +++ b/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatUtil.java @@ -180,15 +180,38 @@ public class ChatUtil { * @param elements the components to join. * @return a new {@link Chat} instance with all the provided {@code component} joined using the separators. */ - public static Chat joinGrammatically(ComponentLike regularSeparator, ComponentLike finalSeparator, List elements) { + public static FormatableChat joinGrammatically(ComponentLike regularSeparator, ComponentLike finalSeparator, List elements) { int size = elements == null ? 0 : elements.size(); int last = size - 1; - Chat c = chat(); - for (int i = 0; i < size; i++) { - if (i > 0) { - c.then(i < last ? regularSeparator : finalSeparator); + return switch (size) { + case 0, 1, 2 -> join(finalSeparator, elements); + default -> (FormatableChat) join(regularSeparator, elements.subList(0, last)) + .then(finalSeparator) + .then(elements.get(last)); + }; + } + + + + + + + /** + * Do like {@link String#join(CharSequence, Iterable)}, but for components. + * @param separator the separator used everywhere except between the two last components to join. + * @return a new {@link Chat} instance with all the provided {@code component} joined using the separators. + */ + public static FormatableChat join(ComponentLike separator, Iterable elements) { + FormatableChat c = chat(); + if (elements == null) + return c; + boolean first = true; + for (ComponentLike el : elements) { + if (!first) { + c.then(separator); } - c.then(elements.get(i)); + c.then(el); + first = false; } return c; }