Skip to content

Commit a730c53

Browse files
authored
Prefer uncommon categories first (#731)
1 parent 2524271 commit a730c53

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@
3232
import java.io.InputStream;
3333
import java.util.*;
3434
import java.util.function.Consumer;
35+
import java.util.function.Function;
3536
import java.util.function.Predicate;
3637
import java.util.regex.Pattern;
3738
import java.util.stream.Collectors;
39+
import java.util.stream.IntStream;
3840

3941
/**
4042
* Helper class offering certain methods used by the help system.
@@ -48,6 +50,11 @@ public final class HelpSystemHelper {
4850

4951
private final Predicate<String> isHelpForumName;
5052
private final String helpForumPattern;
53+
/**
54+
* Compares categories by how common they are, ascending. I.e., the most uncommon or specific
55+
* category comes first.
56+
*/
57+
private final Comparator<ForumTag> byCategoryCommonnessAsc;
5158
private final Set<String> categories;
5259
private final Set<String> threadActivityTagNames;
5360
private final String categoryRoleSuffix;
@@ -66,9 +73,18 @@ public HelpSystemHelper(Config config, Database database) {
6673
helpForumPattern = helpConfig.getHelpForumPattern();
6774
isHelpForumName = Pattern.compile(helpForumPattern).asMatchPredicate();
6875

69-
categories = new HashSet<>(helpConfig.getCategories());
76+
List<String> categoriesList = helpConfig.getCategories();
77+
categories = new HashSet<>(categoriesList);
7078
categoryRoleSuffix = helpConfig.getCategoryRoleSuffix();
7179

80+
Map<String, Integer> categoryToCommonDesc = IntStream.range(0, categoriesList.size())
81+
.boxed()
82+
.collect(Collectors.toMap(categoriesList::get, Function.identity()));
83+
byCategoryCommonnessAsc = Comparator
84+
.<ForumTag>comparingInt(
85+
tag -> categoryToCommonDesc.getOrDefault(tag.getName(), categories.size()))
86+
.reversed();
87+
7288
threadActivityTagNames = Arrays.stream(ThreadActivity.values())
7389
.map(ThreadActivity::getTagName)
7490
.collect(Collectors.toSet());
@@ -167,12 +183,12 @@ Optional<ForumTag> getActivityTagOfChannel(ThreadChannel channel) {
167183
return getFirstMatchingTagOfChannel(threadActivityTagNames, channel);
168184
}
169185

170-
private static Optional<ForumTag> getFirstMatchingTagOfChannel(Set<String> tagNamesToMatch,
186+
private Optional<ForumTag> getFirstMatchingTagOfChannel(Set<String> tagNamesToMatch,
171187
ThreadChannel channel) {
172188
return channel.getAppliedTags()
173189
.stream()
174190
.filter(tag -> tagNamesToMatch.contains(tag.getName()))
175-
.findFirst();
191+
.min(byCategoryCommonnessAsc);
176192
}
177193

178194
RestAction<Void> changeChannelCategory(ThreadChannel channel, String category) {
@@ -183,8 +199,8 @@ RestAction<Void> changeChannelActivity(ThreadChannel channel, ThreadActivity act
183199
return changeMatchingTagOfChannel(activity.getTagName(), threadActivityTagNames, channel);
184200
}
185201

186-
private static RestAction<Void> changeMatchingTagOfChannel(String tagName,
187-
Set<String> tagNamesToMatch, ThreadChannel channel) {
202+
private RestAction<Void> changeMatchingTagOfChannel(String tagName, Set<String> tagNamesToMatch,
203+
ThreadChannel channel) {
188204
List<ForumTag> tags = new ArrayList<>(channel.getAppliedTags());
189205

190206
Optional<ForumTag> currentTag = getFirstMatchingTagOfChannel(tagNamesToMatch, channel);

0 commit comments

Comments
 (0)