Nishant Lakhara
Nishant Lakhara

Reputation: 2455

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?

Upvotes: 4

Views: 550

Answers (2)

MikeFHay
MikeFHay

Reputation: 9023

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, e.g.:

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

Upvotes: 0

biziclop
biziclop

Reputation: 49794

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

P.s.: setter is a BiConsumer, because you use a static method reference (e.g. NodeStatus::setErrorCode) on an instance method, so the first argument of BiConsumer has to be the NodeStatus instance setErrorCode() will be called on.

P.p.s: 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.

Upvotes: 4

Related Questions