Java Developer since version 1.5. Her main expertise are web applications. She’s also familiar with Groovy. Recently she’s been involved in the role of a backend developer deploying REST APIs for SPA application, but she doesn’t ignore Android too. Clean code apostle; she’s keen on reading someone else’s code like a book as well as singing and learning new foreign languages. She’s mastered telling politically incorrect jokes with clients. Anna has more interests than time to spare because she’s raising her husband and two sons at the same time.
Our Java team has recently prepared a hands-on workshop on functional programming in Java 8. The participants solved our coding tasks, trying out the Java 8’s features in separation, and now it’s time we show off how we employ the full power of Java 8 in our real-life projects.
This is a common problem of converting pairs of similar objects one to another (in our case – domain classes to DTOs, which are then sent to frontend as JSON objects) and the other way round. In this case, what we need is a one-shot conversion, just once in the object’s lifetime. What we do not want is coupling between the two kinds of objects: DTO classes shouldn’t “know” about domain classes and the other way round.
Converter class in Java
As changes in the source object do not have to affect the destination object, the classic solution would be to create a single “Mapper” (or “Converter”) class for each pair. There are also tools which are able to map fields of similar classes, based on field names. But how about “producing” whole collections of our source object? How to produce collections of destination objects, without too much boilerplate code?
What addresses our needs, is Java 8 and its three core features:
default method implementation in interfaces
lambdas (here in the form of a method reference)
Default method implementation is what saves us from boilerplate code, creating collections of objects. Streams and lambdas build a beautiful code transforming our collections. Let’s have a look at the final class hierarchy and the code itself:
Having implemented the default method that converts a collection of data transfer objects (D) into entities (E), as well as another one that does the opposite, we don’t need to implement this in concrete implementations of the converter any more. Creating a converter for a single DTO/domain class is as simple as that:
So there we have a complete Converter structure using all the Java 8 goodness. Adding a new Converter for another entity-DTO pair (like User, Address, etc.) needs just creating a new UserConverterImpl class, implementing its own UserConverter, which in turn should implement GenericConverter. This way the new class will be capable of converting collections of objects out-of-the box. This is possible thanks to the default method implementations in GenericConverter interface, which is a very handy Java 8’s feature.