|
8 | 8 | import java.util.function.Predicate; |
9 | 9 |
|
10 | 10 | /** |
| 11 | + * A utility class to easily parse and send translated messages to users. |
| 12 | + * |
11 | 13 | * @author J4C0B3Y |
12 | 14 | * @version ConfigAPI |
13 | 15 | * @since 9/03/2025 |
14 | 16 | */ |
15 | 17 | @Getter |
16 | 18 | public class Message { |
| 19 | + /** |
| 20 | + * The messages current lines. |
| 21 | + */ |
17 | 22 | private final List<String> lines; |
| 23 | + |
| 24 | + /** |
| 25 | + * If the message is the original. |
| 26 | + */ |
18 | 27 | private final boolean initial; |
19 | 28 |
|
| 29 | + /** |
| 30 | + * Creates a new message with the specified lines. |
| 31 | + * |
| 32 | + * @param lines The messages current lines. |
| 33 | + * @param initial If the message is the original. |
| 34 | + */ |
20 | 35 | private Message(Collection<String> lines, boolean initial) { |
21 | 36 | this.lines = new ArrayList<>(lines); |
22 | 37 | this.initial = initial; |
23 | 38 | } |
24 | 39 |
|
| 40 | + /** |
| 41 | + * Creates a new message from a collection. |
| 42 | + * |
| 43 | + * @param lines The collection containing the lines. |
| 44 | + * @return The message. |
| 45 | + */ |
25 | 46 | public static Message of(Collection<String> lines) { |
26 | 47 | return new Message(lines, true); |
27 | 48 | } |
28 | 49 |
|
| 50 | + /** |
| 51 | + * Creates a new message from a string or multiple strings. |
| 52 | + * |
| 53 | + * @param lines The string or multiple strings. |
| 54 | + * @return The message. |
| 55 | + */ |
29 | 56 | public static Message of(String... lines) { |
30 | 57 | return new Message(Arrays.asList(lines), true); |
31 | 58 | } |
32 | 59 |
|
| 60 | + /** |
| 61 | + * Supplies the consumer with the lines of the current message if it isn't initial |
| 62 | + * or the lines of a new message with lines copied over, if it is the initial message. |
| 63 | + * |
| 64 | + * @param consumer The consumer to supply. |
| 65 | + * @return The non-initial message. |
| 66 | + */ |
33 | 67 | private Message supply(Consumer<List<String>> consumer) { |
34 | 68 | Message message = initial ? new Message(lines, false) : this; |
35 | 69 | consumer.accept(message.lines); |
36 | 70 | return message; |
37 | 71 | } |
38 | 72 |
|
| 73 | + /** |
| 74 | + * Replaces all the lines in the message using the mapping function. |
| 75 | + * |
| 76 | + * @param mapper The mapping function. |
| 77 | + * @return The mapped message. |
| 78 | + */ |
39 | 79 | public Message map(Function<String, String> mapper) { |
40 | 80 | return supply(lines -> lines.replaceAll(mapper::apply)); |
41 | 81 | } |
42 | 82 |
|
| 83 | + /** |
| 84 | + * Removes lines that match the filter predicate from a message. |
| 85 | + * |
| 86 | + * @param filter The filter predicate. |
| 87 | + * @return The filtered message. |
| 88 | + */ |
43 | 89 | public Message filter(Predicate<String> filter) { |
44 | 90 | return supply(lines -> lines.removeIf(filter)); |
45 | 91 | } |
46 | 92 |
|
| 93 | + /** |
| 94 | + * Replaces all instances of a string in the message. |
| 95 | + * |
| 96 | + * @param target The string to replace. |
| 97 | + * @param replacement The replacement value. |
| 98 | + * @return The replaced message. |
| 99 | + */ |
47 | 100 | public Message replace(String target, String replacement) { |
48 | 101 | return map(line -> line.replaceAll(target, replacement)); |
49 | 102 | } |
50 | 103 |
|
| 104 | + /** |
| 105 | + * Sends all lines to a user using the provided consumer, |
| 106 | + * |
| 107 | + * @param consumer The send message consumer. |
| 108 | + */ |
51 | 109 | public void send(Consumer<String> consumer) { |
52 | 110 | this.lines.forEach(consumer); |
53 | 111 | } |
|
0 commit comments