Java 非静态方法的 Consumer MethodReference
Java Consumer MethodReference for nonstatic methods
代码片段:
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);
}
}
我可以使用哪种类型的功能接口来代替 SomeConsumer?这里的主要问题是它不是静态字段,而且我只会在运行时知道的对象。
我尝试使用 BiConsumer,但它告诉我不能以这种方式引用非静态方法。
根据您在此处的用法:
consumer.apply(documentPublisher, "documentName", "testId1");
很明显消费者消费了 3 样东西,所以它不是 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");
}
代码片段:
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);
}
}
我可以使用哪种类型的功能接口来代替 SomeConsumer?这里的主要问题是它不是静态字段,而且我只会在运行时知道的对象。
我尝试使用 BiConsumer,但它告诉我不能以这种方式引用非静态方法。
根据您在此处的用法:
consumer.apply(documentPublisher, "documentName", "testId1");
很明显消费者消费了 3 样东西,所以它不是 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");
}