Skip to content

Commit 6c3cc81

Browse files
authored
Feature/logging-for-disabled-features (#1048)
* Added Logging for disabled features
1 parent 4ff5966 commit 6c3cc81

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

application/src/main/java/org/togetherjava/tjbot/config/FeatureBlacklist.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package org.togetherjava.tjbot.config;
22

33
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
46

57
import java.util.Set;
8+
import java.util.function.Function;
9+
import java.util.stream.Stream;
610

711
/**
812
* Blacklist of features, use {@link FeatureBlacklist#isEnabled(T)} to test if a feature is enabled.
@@ -13,6 +17,8 @@
1317
public class FeatureBlacklist<T> {
1418
private final Set<T> featureIdentifierBlacklist;
1519

20+
private static final Logger logger = LoggerFactory.getLogger(FeatureBlacklist.class);
21+
1622
/**
1723
* Creates a feature blacklist
1824
*
@@ -32,4 +38,25 @@ public FeatureBlacklist(Set<T> featureIdentifierBlacklist) {
3238
public boolean isEnabled(T featureId) {
3339
return !featureIdentifierBlacklist.contains(featureId);
3440
}
41+
42+
/**
43+
* Filters features stream to only having enabled features and logs any disabled features.
44+
*
45+
* @param features the feature stream to be filtered
46+
* @param idExtractor function to get the class name of an object
47+
* @return stream of features that are enabled
48+
*/
49+
public <F> Stream<F> filterStream(Stream<F> features,
50+
Function<? super F, ? extends T> idExtractor) {
51+
return features.filter(f -> {
52+
T id = idExtractor.apply(f);
53+
if (!this.isEnabled(id)) {
54+
logger.info("Feature {} is disabled", id);
55+
return false;
56+
}
57+
return true;
58+
});
59+
}
60+
61+
3562
}

application/src/main/java/org/togetherjava/tjbot/features/Features.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,6 @@ public static Collection<Feature> createFeatures(JDA jda, Database database, Con
168168
features.add(new JShellCommand(jshellEval));
169169

170170
FeatureBlacklist<Class<?>> blacklist = blacklistConfig.normal();
171-
return features.stream().filter(f -> blacklist.isEnabled(f.getClass())).toList();
171+
return blacklist.filterStream(features.stream(), Object::getClass).toList();
172172
}
173173
}

application/src/main/java/org/togetherjava/tjbot/features/code/CodeMessageHandler.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ public final class CodeMessageHandler extends MessageReceiverAdapter implements
7171
public CodeMessageHandler(FeatureBlacklist<String> blacklist, JShellEval jshellEval) {
7272
componentIdInteractor = new ComponentIdInteractor(getInteractionType(), getName());
7373

74-
List<CodeAction> codeActions =
75-
Stream.of(new FormatCodeCommand(), new EvalCodeCommand(jshellEval))
76-
.filter(a -> blacklist.isEnabled(a.getClass().getSimpleName()))
77-
.toList();
74+
List<CodeAction> codeActions = blacklist
75+
.filterStream(Stream.of(new FormatCodeCommand(), new EvalCodeCommand(jshellEval)),
76+
codeAction -> codeAction.getClass().getName())
77+
.toList();
7878

7979
labelToCodeAction = codeActions.stream()
8080
.collect(Collectors.toMap(CodeAction::getLabel, Function.identity(), (x, y) -> y,

0 commit comments

Comments
 (0)