32
32
import java .io .InputStream ;
33
33
import java .util .*;
34
34
import java .util .function .Consumer ;
35
+ import java .util .function .Function ;
35
36
import java .util .function .Predicate ;
36
37
import java .util .regex .Pattern ;
37
38
import java .util .stream .Collectors ;
39
+ import java .util .stream .IntStream ;
38
40
39
41
/**
40
42
* Helper class offering certain methods used by the help system.
@@ -48,6 +50,11 @@ public final class HelpSystemHelper {
48
50
49
51
private final Predicate <String > isHelpForumName ;
50
52
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 ;
51
58
private final Set <String > categories ;
52
59
private final Set <String > threadActivityTagNames ;
53
60
private final String categoryRoleSuffix ;
@@ -66,9 +73,18 @@ public HelpSystemHelper(Config config, Database database) {
66
73
helpForumPattern = helpConfig .getHelpForumPattern ();
67
74
isHelpForumName = Pattern .compile (helpForumPattern ).asMatchPredicate ();
68
75
69
- categories = new HashSet <>(helpConfig .getCategories ());
76
+ List <String > categoriesList = helpConfig .getCategories ();
77
+ categories = new HashSet <>(categoriesList );
70
78
categoryRoleSuffix = helpConfig .getCategoryRoleSuffix ();
71
79
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
+
72
88
threadActivityTagNames = Arrays .stream (ThreadActivity .values ())
73
89
.map (ThreadActivity ::getTagName )
74
90
.collect (Collectors .toSet ());
@@ -167,12 +183,12 @@ Optional<ForumTag> getActivityTagOfChannel(ThreadChannel channel) {
167
183
return getFirstMatchingTagOfChannel (threadActivityTagNames , channel );
168
184
}
169
185
170
- private static Optional <ForumTag > getFirstMatchingTagOfChannel (Set <String > tagNamesToMatch ,
186
+ private Optional <ForumTag > getFirstMatchingTagOfChannel (Set <String > tagNamesToMatch ,
171
187
ThreadChannel channel ) {
172
188
return channel .getAppliedTags ()
173
189
.stream ()
174
190
.filter (tag -> tagNamesToMatch .contains (tag .getName ()))
175
- .findFirst ( );
191
+ .min ( byCategoryCommonnessAsc );
176
192
}
177
193
178
194
RestAction <Void > changeChannelCategory (ThreadChannel channel , String category ) {
@@ -183,8 +199,8 @@ RestAction<Void> changeChannelActivity(ThreadChannel channel, ThreadActivity act
183
199
return changeMatchingTagOfChannel (activity .getTagName (), threadActivityTagNames , channel );
184
200
}
185
201
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 ) {
188
204
List <ForumTag > tags = new ArrayList <>(channel .getAppliedTags ());
189
205
190
206
Optional <ForumTag > currentTag = getFirstMatchingTagOfChannel (tagNamesToMatch , channel );
0 commit comments