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

I have recently started implementing different distributed system protocols to get some understanding how they work. I think that using Akka Actors to simulate hosts is a good choice because they are easy to set up. What is more you can kill actors on demand to test some failure scenarios.

First protocol I implemented is Paxos - probably the most important consensus protocol, which guarantees storng consistecy, full transactions, read/write failover and prevents from data loss. This comes with the cost of higher latency and lower throughput. In terms of CAP theorem, Paxos is used to make your system CP. I used this sweet article as a reference.

Implementation can be found on my github repository. Go to the Test.scala and run some simulations. 
0

Add a comment

Popular Posts
Popular Posts
About Me
About Me
Labels
Blog Archive
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.