Java消费者方法引用用于非静态方法。

huangapple go评论78阅读模式
英文:

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&lt;ActionType, SomeConsumer&lt;DocumentPublisher, String, String&gt;&gt; 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&lt;DocumentPublisher, String, String&gt; consumer = consumerMapping.get(action.getType());
    consumer.apply(documentPublisher, &quot;documentName&quot;, &quot;testId1&quot;);
}

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, &quot;documentName&quot;, &quot;testId1&quot;);

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&lt;T1, T2, T3&gt; {
    void accept(T1 a, T2 b, T3 c);
}

If the only generic parameters that you are ever going to give it is &lt;DocumentPublisher, String, String&gt;, 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&lt;ActionType, DocumentPublisherAction&gt; 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, &quot;documentName&quot;, &quot;testId1&quot;);
}

huangapple
  • 本文由 发表于 2020年8月26日 10:11:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63589522.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定