Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package org.togetherjava.tjbot.config;

import com.fasterxml.jackson.annotation.JsonCreator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Set;
import java.util.function.Function;
import java.util.stream.Stream;

/**
* Blacklist of features, use {@link FeatureBlacklist#isEnabled(T)} to test if a feature is enabled.
Expand All @@ -12,6 +16,7 @@
*/
public class FeatureBlacklist<T> {
private final Set<T> featureIdentifierBlacklist;
private static final Logger logger = LoggerFactory.getLogger(FeatureBlacklist.class);

/**
* Creates a feature blacklist
Expand All @@ -32,4 +37,15 @@ public FeatureBlacklist(Set<T> featureIdentifierBlacklist) {
public boolean isEnabled(T featureId) {
return !featureIdentifierBlacklist.contains(featureId);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add Java doc here.

public <F> Stream<F> disableMatching(Stream<F> features, Function<F, T> idExtractor) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use Stream<? extends F> and Function<? super F, ? extends T> as it allows more flexibility

return features.filter(f -> {
T id = idExtractor.apply(f);
if (!this.isEnabled(id)) {
logger.info("Feature {} is disabled", id);
return false;
}
return true;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,6 @@ public static Collection<Feature> createFeatures(JDA jda, Database database, Con
features.add(new JShellCommand(jshellEval));

FeatureBlacklist<Class<?>> blacklist = blacklistConfig.normal();
return features.stream().filter(f -> blacklist.isEnabled(f.getClass())).toList();
return blacklist.disableMatching(features.stream(), Feature::getClass).toList();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this should be made Object::getClass

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ public final class CodeMessageHandler extends MessageReceiverAdapter implements
public CodeMessageHandler(FeatureBlacklist<String> blacklist, JShellEval jshellEval) {
componentIdInteractor = new ComponentIdInteractor(getInteractionType(), getName());

List<CodeAction> codeActions =
Stream.of(new FormatCodeCommand(), new EvalCodeCommand(jshellEval))
.filter(a -> blacklist.isEnabled(a.getClass().getSimpleName()))
.toList();
List<CodeAction> codeActions = blacklist
.disableMatching(Stream.of(new FormatCodeCommand(), new EvalCodeCommand(jshellEval)),
e -> e.getClass().getName())
.toList();


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