简体   繁体   中英

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.

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. I tried various Consumers, BiConsumers etc but couldnt do this. 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.)

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.

Pps: As pointed out by glglgl , you can potentially simplify your code to this:

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.

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:

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)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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