Categories: Java

A Practical Guide to Project Lombok

A Practical Guide to Project Lombok

Java is a powerful language but often criticized for being too verbose (aka boilerplate – term used to describe code that is repeated in many parts of an application with little alteration). Project Lombok came to help the lazy coder in you. It basically generates code for you. But instead of adding code to your non-compiled classes, like an IDE generally does, Lombok works during the build process by modifying the Java bytecode of your class files.

It’s very simple to configure it, their project page has all you need to help you to add it in your projects.

Lombok Annotations

1. @Getter and @Setter

The @Getter and @Setter annotations are used to generate accessor methods for fields, @Getter annotation respects the convention if the field is a boolean property. Both @Getter and @Setter take an optional parameter to specify the access level for the generated method:

Lombok code:

Equivalent Java native code:

Categories: Java

7 Useful Examples of the Java 8 Stream API

7 Useful Examples Java 8 Stream API

You probably already heard about the Java 8 Stream API (If not, you can find more information about it here). It’s basically an abstraction that allows us to process data in a simple and declarative way. Besides that, streams can leverage multi-core architectures without you having to write a single line of multi-thread code.

In this article we won’t describe each of the available methods on the Java 8 Stream API. Instead, we will focus on practical examples of how to consume this new API.

First of all, let’s present our Athlete model class:

Now, let’s initialize a List of Athletes with 6 objects…

Based on the previously presented List of Athletes, let’s now see how to easily manipulate this data and extract the information we want.

1. Getting the quantity of female athletes (filter + count)

2. Retrieving the list of American athlete names (filter + map + collect)

Categories: Java, Miscellaneous

Automatically Mapping Java Objects

Automatically Mapping Java Objects

Automatically Mapping Java ObjectsIn this article we aim to clarify why sometimes it’s important to map two different classes and how it’s possible to do it by building the simplest possible mapping manager step-by-step. Design choices / constraints often produce a considerable amount of mechanical work which we will try to minimize by automatically mapping Java objects.

By “mapping objects” we mean copy one object state to another object. Sometimes the mapping is transparent, considering that both objects have the same attributes, but often it happens between two objects with different attributes. Those who are familiar with Web API development probably already faced a situation where your resource representation is similar but not identical to the domain model or even when you don’t want to expose your domain objects to any other layer within your system. In these cases we generally create a component to abstract this mapping task, which also allow us to reuse it in other parts of the system.

An advantage of having a mapping component (e.g. to map your domain to DTO and vice versa) is not as apparent when you are only supporting a single mapping, but as the number of mappings increases, having that code isolated from the domain helps to keep the domain simpler and leaner. You won’t be cluttering your domain with a lot of extra weight.

Hands On

To clarify the mapping idea, let’s start creating two domain classes (Customer and Address) and one DTO class (CustomerDTO).

Tutorial: Building the Simplest JS Notification Library

Tutorial: Building the Simplest JS Notification Library

Few days ago I was asked to create a solution to work with JS Notification for a web app in the same way apps like WhatsApp web does. So, I went to the docs, read it and got quite surprised how easy it is to use the native API. For a little while I thought my work was already done and, even better, without any dependences from frameworks, only pure Javascript.


Before I talk about the issues on my Notifications project, let’s see how use it.

That’s simplest example of notification:

Try it yourself

Pretty cool, eh?

In the example above we are only printing the attribute title of the notification. There are other attributes we can declare to have a better experience with our notification and, yay, those are almost working in the same way at the main browsers (except for IE family, of course. But we are talking about real browsers, aren’t we? But don’t worry, we’ll create a way to give a fallback to them).

How to Integrate Elasticsearch with MongoDB

How to integrate Elasticsearch with MongoDBRecently, I faced an unusual requirement during the implementation of a project. My task was to implement a web crawler to index the content of a few websites and save those data into an Elasticsearch index for further analysis. The pitfall decision in this case lay in the fact that I had no strong reason to keep the extracted data in anywhere else, once that all user interaction with this data would be done using an web application that connects directly to Elasticsearch. But, if the Elasticsearch index mapping changes at anytime in the future, I would be forced to re-index part or all of the data, which means extract the same data from web sites again.

Adopting a relational database to address this need seemed to me an unjustified implementation effort. It would drastically increase the time, cost and complexity to implement and maintain the project, just to avoid a future risk of changes in my index mapping. Deal with database modeling, choose a persistence framework, implement extra tests, … I feel tired just to think about it. So, talking with my friend Paulo about this problem, he told me about the elasticsearch-river-mongodb project, an Elasticsearch plugin that propagates changes data from a MongoDB collection to an Elasticsearch index.

Use MongoDB seemed to be a good idea. The data extracted from website are not well structured and it is highly probable to suffer frequently changes. A schema-free / document oriented database fits well in this case, once that it’s flexible enough to accommodate changes on data structure with a minimum impact.

But, How to integrate Elasticsearch with MongoDB?

Despite the fact that elasticsearch-river-mongodb project seems to be awesome, offering filter and transformation capabilities, it is deprecated, having Elasticsearch 1.7.3 and MongoDB 3.0.0 as most recently supported versions. You can find more information about the deprecation decision on the article “Deprecating Rivers”.

It is a shame, but all is not lost. The MongoDB team offers mongodb-connector project which creates a pipeline to target systems and has a document manager for Elasticsearch. Great! And I’m so happy with the final result of this solution that I want to share my experience with you. My intention along of this post is to show what I found useful, what was tricky and what limitations I found during the implementation of this solution.