英文:
Java Consumer MethodReference for nonstatic methods
问题
class Scratch {
Map<ActionType, BiConsumer<DocumentPublisher, String[]>> consumerMapping = Map.of(
ActionType.REJECT, DocumentPublisher::rejectDocument,
ActionType.ACCEPT, DocumentPublisher::acceptDocument,
ActionType.DELETE, DocumentPublisher::deleteDocument);
private void runProcess(DocumentAction action) {
DocumentPublisher documentPublisher = DocumentPublisherFactory.getDocumentPublisher(action.getType);
BiConsumer<DocumentPublisher, String[]> consumer = consumerMapping.get(action.getType());
consumer.accept(documentPublisher, new String[]{"documentName", "testId1"});
}
private interface DocumentPublisher {
void rejectDocument(String name, String textId);
void acceptDocument(String name, String textId);
void deleteDocument(String name, String textId);
}
}
你可以使用 BiConsumer<DocumentPublisher, String[]>
代替 SomeConsumer<DocumentPublisher, String, String>
,主要问题在于你需要在运行时传递对象,而不是静态字段。我已经将代码做了相应的修改,现在代码中使用了 BiConsumer
,你可以在 runProcess
方法中调用 consumer.accept
来执行相应的操作。
英文:
Code snippet:
class Scratch {
Map<ActionType, SomeConsumer<DocumentPublisher, String, String>> consumerMapping = Map.of(
ActionType.REJECT, DocumentPublisher::rejectDocument,
ActionType.ACCEPT, DocumentPublisher::acceptDocument,
ActionType.DELETE, DocumentPublisher::deleteDocument);
private void runProcess(DocumentAction action) {
DocumentPublisher documentPublisher = DocumentPublisherFactory.getDocumentPublisher(action.getType);
SomeConsumer<DocumentPublisher, String, String> consumer = consumerMapping.get(action.getType());
consumer.apply(documentPublisher, "documentName", "testId1");
}
private interface DocumentPublisher {
void rejectDocument(String name, String textId);
void acceptDocument(String name, String textId);
void deleteDocument(String name, String textId);
}
}
Which type of functionalInterface can I use instead SomeConsumer? The main issue here is that it is not static field, and the object I will only know in runtime.
I tried to use BiConsumer, however it tells me that I can not refer to non static method in this way.
答案1
得分: 2
根据您在此处的用法:
consumer.apply(documentPublisher, "documentName", "testId1");
很明显,消费者会消耗三个参数,因此它不是一个 BiConsumer
。您需要一个 TriConsumer
,但标准库中没有提供这样的接口。
您可以自己编写这样的函数式接口:
interface TriConsumer<T1, T2, T3> {
void accept(T1 a, T2 b, T3 c);
}
如果您将给它的唯一泛型参数始终设为 <DocumentPublisher, String, String>
,我认为您应该将其命名为与您的应用程序相关的名称,例如 DocumentPublisherAction
:
interface DocumentPublisherAction {
void perform(DocumentPublisher publisher, String name, String textId);
}
Map<ActionType, DocumentPublisherAction> consumerMapping = Map.of(
ActionType.REJECT, DocumentPublisher::rejectDocument,
ActionType.ACCEPT, DocumentPublisher::acceptDocument,
ActionType.DELETE, DocumentPublisher::deleteDocument);
private void runProcess(DocumentAction action) {
DocumentPublisher documentPublisher = DocumentPublisherFactory.getDocumentPublisher(action.getType);
DocumentPublisherAction consumer = consumerMapping.get(action.getType());
consumer.perform(documentPublisher, "documentName", "testId1");
}
英文:
From your usage here:
consumer.apply(documentPublisher, "documentName", "testId1");
It is quite clear that the consumer consumes 3 things, so it's not a BiConsumer
. You'd need a TriConsumer
, which isn't available in the standard library.
You can write such a functional interface yourself though:
interface TriConsumer<T1, T2, T3> {
void accept(T1 a, T2 b, T3 c);
}
If the only generic parameters that you are ever going to give it is <DocumentPublisher, String, String>
, I think you should name it something specific to your application, such as DocumentPublisherAction
:
interface DocumentPublisherAction {
void perform(DocumentPublisher publisher, String name, String textId);
}
Map<ActionType, DocumentPublisherAction> consumerMapping = Map.of(
ActionType.REJECT, DocumentPublisher::rejectDocument,
ActionType.ACCEPT, DocumentPublisher::acceptDocument,
ActionType.DELETE, DocumentPublisher::deleteDocument);
private void runProcess(DocumentAction action) {
DocumentPublisher documentPublisher = DocumentPublisherFactory.getDocumentPublisher(action.getType);
DocumentPublisherAction consumer = consumerMapping.get(action.getType());
consumer.perform(documentPublisher, "documentName", "testId1");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论