In this article, I am going to present you a simple trick that will make using java.util.function.Function.andThen() more useful. 

As an example I will use ExternalSystemGateway class, which job is to call external system along with serializing/mapping the messages:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Request {...}

class Response {...}

class ExternalSystemRequest {...}

class ExternalSystemResponse {...}

public class ExternalSystemGateway {

    public Response invoke(Request request) {
        ExternalSystemRequest externalSystemRequest = mapRequest(request);
        String requestString = marshallRequest(externalSystemRequest);
        String responseString = sendAndReceive(requestString);
        ExternalSystemResponse externalSystemResponse = unmarshallResponse(responseString);
        Response response = mapResponse(externalSystemResponse);
        return response;
    }

    private ExternalSystemRequest mapRequest(Request request) {
        // ..
    }

    private String marshallRequest(ExternalSystemRequest externalSystemRequest) {
        // ..
    }

    private String sendAndReceive(String requestString) {
        //..
    }

    private ExternalSystemResponse unmarshallResponse(String responseString) {
        //..
    }

    private Response mapResponse(ExternalSystemResponse externalSystemResponse) {
        //..
    }

}

You can see that every line of the invoke method does some kind of action, which transforms some input type to another output type. Let's try to get rid of the variables, which are declared only to be used in the next line:


1
2
3
4
5
6
7
    public Response invoke(Request request) {
        return mapResponse(
                unmarshallResponse(
                        sendAndReceive(
                                marshallRequest(
                                        mapRequest(request)))));
    }

Variables are gone but this code looks weird. What is more you need to read it backwards to understand the flow. Let's try to compose invocations using java.util.function.Function.andThen():


1
2
3
4
5
6
7
public Response invoke(Request request) {
        return ((Function<Request, ExternalSystemRequest>) (this::mapRequest))
                .andThen(this::marshallRequest)
                .andThen(this::sendAndReceive)
                .andThen(this::unmarshallResponse)
                .andThen(this::mapResponse)
                .apply(request);

Better, code reads nicer now, but can we get rid of the (Function<Request, ExternalSystemRequest>) cast at the beginning? Typing just this::mapRequest is not representing java.util.function.Function type so the trick is to achieve this using this simple interface:


1
2
3
4
5
6
public interface FunctionUtils {

    static <T, R> Function<T, R> function(Function<T, R> function) {
        return function;
    }
}

Our invoke method now becomes:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import static com.ps.functional.util.FunctionUtils.function;
...

    public Response invoke(Request request) {
        return function(this::mapRequest)
                .andThen(this::marshallRequest)
                .andThen(this::sendAndReceive)
                .andThen(this::unmarshallResponse)
                .andThen(this::mapResponse)
                .apply(request);
    }


Method function is just casting any one argument method reference to a java.util.function.Function type. You can create the same method for two argument functions:  


1
2
3
static <T, U, R> BiFunction<T, U, R> biFunction(BiFunction<T, U, R> biFunction) {
        return biFunction;
    }


You can now easily compose functions in a readable way, without a need to do any casting. Source code for FunctionUtils is available here.




1

View comments

Paxos protocol simulation in Akka
Paxos protocol simulation in Akka
Don't sleep when you test!
Don't sleep when you test!
5 Less Known Cool Intellij Features
5 Less Known Cool Intellij Features
Simple Map Reduce concept implementation
Simple Map Reduce concept implementation
Kotlin null safety for Optional experienced
Kotlin null safety for Optional experienced
Understanding flatMap
Understanding flatMap
Verifying End-to-end Test Code Coverage using Jacoco Agent
Lifting functions to work with monads in Java
Lifting functions to work with monads in Java
Composing functions with java.util.function.Function.andThen() trick
Composing functions with java.util.function.Function.andThen() trick
1
Exception-free code using functional approach
Exception-free code using functional approach
1
Popular Posts
Popular Posts
About Me
About Me
Labels
Blog Archive
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.