Search code examples
javalambdafunctional-programmingproducer-consumer

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?


Solution

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