简体   繁体   English

Java 8函数式编程 - 需要在类上编写泛型函数

[英]Java 8 Functional Programming - Need to write a generic function on class

I want to create a method that accepts something like this 我想创建一个接受这样的东西的方法

set(nodeStatus, status, NodeStatus::setStatus, Status::valueOf);
set(nodeStatus, errorCode, NodeStatus::setErrorCode, ErrorCode::valueOf);

Status and ErrorCode are enums in java. Status和ErrorCode是java中的枚举。

Signature and pseudocode 签名和伪代码

set(NodeStatus nodeStatus, byte[] status, ?nodeStatusOperator , ?ValueTransformer) {
   1. convert byte[] status to appropriate value as per ValueTransformer
   2. nodeStatusOperator sets this transformed value according to the lambda passed.
}

I want to know what method signature should be used to accompalish this in java and why. 我想知道在java中使用什么方法签名来配合这个以及为什么。 I tried various Consumers, BiConsumers etc but couldnt do this. 我尝试了各种消费者,BiConsumers等,但无法做到这一点。 Can anyone please help? 有人可以帮忙吗?

As far as I can tell, what you need is this: 据我所知,你需要的是:

public <T> void set (NodeStatus nodeStatus, 
                     byte [] value, 
                     BiConsumer<NodeStatus,T> setter, 
                     Function<byte[],T> transformer) {
    T transformedValue = transformer.apply(value);
    setter.accept(nodeStatus, transformedValue);
}

(If value can be something other than byte[] , you can replace it with another type parameter.) (如果value可以是byte[]以外的value则可以用其他类型参数替换它。)

Ps: setter is a BiConsumer , because you use a static method reference (eg NodeStatus::setErrorCode ) on an instance method, so the first argument of BiConsumer has to be the NodeStatus instance setErrorCode() will be called on. Ps: setter是一个BiConsumer ,因为你在实例方法上使用静态方法引用(例如NodeStatus::setErrorCode ),所以BiConsumer的第一个参数必须是NodeStatus实例, setErrorCode()将被调用。

Pps: As pointed out by glglgl , you can potentially simplify your code to this: Pps:正如glglgl所指出的,您可以将代码简化为:

public <T> void set (byte [] value, 
                     Consumer<T> setter, 
                     Function<byte[],T> transformer) {
    T transformedValue = transformer.apply(value);
    setter.accept(transformedValue);
}

And call it like this: 并称之为:

set(status, nodeStatus::setStatus, Status::valueOf);

...where nodeStatus is the instance of NodeStatus you want to manipulate. ...其中nodeStatus是您要操作的NodeStatus的实例。

It's a little unclear what you're trying to achieve. 有点不清楚你想要实现的目标。 Why pass the NodeStatus and the function when you could just pass a function that works on that specific NodeStatus instance, eg: 为什么在只传递一个适用于该特定NodeStatus实例的函数时传递NodeStatus和函数,例如:

static <T> void set(byte[] status, Consumer<T> nodeStatusOperator, Function<String, T> transformer) {
    nodeStatusOperator.accept(transformer.apply(new String(status)));
}

public static void main(String[] args) {
    NodeStatus nodeStatus = new NodeStatus();
    byte[] status = new byte[0];
    set(status, nodeStatus::setStatus, Status::valueOf);
    set(status, nodeStatus::setErrorCode, ErrorCode::valueOf);
}

And what does that genericity buy you over a more straightforward approach? 那种通用性会以更简单的方式为您带来什么?

nodeStatus.setStatus(Status.valueOf(new String(status)));
nodeStatus.setErrorCode(ErrorCode.valueOf(new String(status)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM