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.
When I started working as a Java Developer, me and my teammate got a first task to repair all broken tests (great task for new starters!) in some old project. Replacing some old configuration and upgrading a few libraries helped making the tests status green but there was another problem.
In this article I am going to share some cool features I stumbled upon while coding with Intellij. These are not the most popular/productivity improving ones - for these you should watch this video. 

1.
I have recently pushed very simple Map Reduce concept implementation on my Github account (click). My idea was to focus on the concept and mock the rest.
In this article I will try to map methods of Java’s Optional to Kotlin’ssimilar scattered language features and built-in functions. The code in the examples is written in Kotlin, because the language has all the JDK classes available.

Representation

Let’s start with the representation.
Have you ever scrolled someone’s code and bumped into this weird method called flatMap, not knowing what it actually does from the context? Or maybe you compared it with method map but didn’t really see much difference? If that is the case then this article is for you.
Functional Programming in Java

Stream and Optional classes - added to Java 8 - allow you to have some fun with functional programming. The problem is Java still misses quite a lot to be taken as a serious FP language.
In this article, I am going to present you a simple trick that will make using java.util.function.Function.andThen() more useful.

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

Checked exceptions & Java 8

Defining custom exceptions (both checked and unchecked) is a common approach to handling errors in Java applications. It usually leads to creating a new class for every different type of error, marking methods with throws keyword or wrapping code with try-catch blocks.
Popular Posts
Popular Posts
About Me
About Me
Labels
Blog Archive
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.