Skip to content

Commit bb8edec

Browse files
committed
add javadoc comments to new message utility class.
1 parent 6555b02 commit bb8edec

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

core/src/main/java/net/j4c0b3y/api/config/message/Message.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,104 @@
88
import java.util.function.Predicate;
99

1010
/**
11+
* A utility class to easily parse and send translated messages to users.
12+
*
1113
* @author J4C0B3Y
1214
* @version ConfigAPI
1315
* @since 9/03/2025
1416
*/
1517
@Getter
1618
public class Message {
19+
/**
20+
* The messages current lines.
21+
*/
1722
private final List<String> lines;
23+
24+
/**
25+
* If the message is the original.
26+
*/
1827
private final boolean initial;
1928

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+
*/
2035
private Message(Collection<String> lines, boolean initial) {
2136
this.lines = new ArrayList<>(lines);
2237
this.initial = initial;
2338
}
2439

40+
/**
41+
* Creates a new message from a collection.
42+
*
43+
* @param lines The collection containing the lines.
44+
* @return The message.
45+
*/
2546
public static Message of(Collection<String> lines) {
2647
return new Message(lines, true);
2748
}
2849

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+
*/
2956
public static Message of(String... lines) {
3057
return new Message(Arrays.asList(lines), true);
3158
}
3259

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+
*/
3367
private Message supply(Consumer<List<String>> consumer) {
3468
Message message = initial ? new Message(lines, false) : this;
3569
consumer.accept(message.lines);
3670
return message;
3771
}
3872

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+
*/
3979
public Message map(Function<String, String> mapper) {
4080
return supply(lines -> lines.replaceAll(mapper::apply));
4181
}
4282

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+
*/
4389
public Message filter(Predicate<String> filter) {
4490
return supply(lines -> lines.removeIf(filter));
4591
}
4692

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+
*/
47100
public Message replace(String target, String replacement) {
48101
return map(line -> line.replaceAll(target, replacement));
49102
}
50103

104+
/**
105+
* Sends all lines to a user using the provided consumer,
106+
*
107+
* @param consumer The send message consumer.
108+
*/
51109
public void send(Consumer<String> consumer) {
52110
this.lines.forEach(consumer);
53111
}

core/src/main/java/net/j4c0b3y/api/config/provider/impl/MessageProvider.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import java.util.List;
1111

1212
/**
13+
* Used for providing the message utility class.
14+
*
1315
* @author J4C0B3Y
1416
* @version ConfigAPI
1517
* @since 9/03/2025
@@ -18,27 +20,34 @@ public class MessageProvider implements TypeProvider<Message> {
1820

1921
@Override
2022
public Message load(LoadContext context) {
23+
// If the object is a collection.
2124
if (context.getObject() instanceof Collection) {
2225
List<String> parsed = new ArrayList<>();
2326

27+
// Parse each item and add it to the parsed list.
2428
for (Object object : (Collection<?>) context.getObject()) {
2529
parsed.add(String.valueOf(object));
2630
}
2731

32+
// Return the message based off the parsed list.
2833
return Message.of(parsed);
2934
}
3035

36+
// If the object is anything else, including a string,
37+
// parse it then return a 1 line message.
3138
return Message.of(String.valueOf(context.getObject()));
3239
}
3340

3441
@Override
3542
public Object save(SaveContext<Message> context) {
3643
Message message = context.getObject();
3744

45+
// If the message is one line, return the first line as a string.
3846
if (message.getLines().size() == 1) {
3947
return message.getLines().get(0);
4048
}
4149

50+
// If the message is multi-line, return all lines as a list.
4251
return message.getLines();
4352
}
4453
}

0 commit comments

Comments
 (0)