Category Archives: tutorial

Working with nested Maps in JAVA

In this post I will show you a simple and easy way of working with JAVA Maps, more precisely, how to add an element (a.k.a value) to a multi nested maps:

That’s it. Simple, isn’t it? 😉

Source code: github

Builder Design Pattern

Here is the “usual” implementation of this design pattern.


The result is:




Builder Design Pattern (simple way)

This design pattern allows us to create an object with the advantage that the same construction process can create different representations of that object. Here is the simplest implementation of this design pattern.

In a next tutorial we will explain the other way of doing the same thing, with more flexibility but with a more complex implementation.

So, let’s start creating a car!


The final result:


Singleton Design Pattern

With his design patttern we allways get the same instance, as you can see next.

  1. The Singleton class:


2. The main class:


3. The result:




Reverse String/array recursively

Here is an example how to recursively invert the order of a string or an array in Java.

The output is:


Notice that in the string method we have twice the iterations that we have in the array method, because we go through all positions in the string.

If you know other ways of doing it, please feel free to comment it out.


Date Range Validation with Annotations in JAVA

In this post I will show you how to validate date ranges with JAVA annotation.

simple date range validation
simple date range validation

XHTML page:

This is a simple JSF page with two date inputs and a submit button. When we press the button a the validation process will start via the backing bean that call a super class method.

Backing Bean:

Note that in line 3 we have the super class named: AbstractController. In line 16, in the action method, we call a validate method and we pass as a parameter the dto wich has the data itself.

Abstract Controller

In this abstract class we simple call the validate() method in the received DTO.

Data Transfer Object(DTO):

Here is the point where we annotate at the class-level with our special annotation. We simple use the annotation with 2 parameters. This parameters are optional and it’s purpose is only to personalize the error message. In case that your date fields are different from those in this example you must provide the annotation with more parameters, as you will see in the next class.


Here is where the magic happens. I will not discuss how annotations work, but I will cover the essential part: the constraint validation. The inner class DateIntervalValidator is responsible for the validation of the date fields with the help of reflection. We get the get methods of the two date fields and then we verify if endDate is after startDate. If that condition is false, we return false, otherwise we return true.
The message is required if you want personalize your message.
The start is required if your first date field is different from startDate. It indicates the name of the first field.
The end is required if your end date field is different from endDate. It indicates the name of the second date field.
The purpose the startDesc and endDesc is to use as a placeholder in the message parameter.


This class is the trigger of all the validation process. When in the DTO we call the validate method, is this method we are calling, because this class is the superclass of the DTO. It calls the annotation and validates the fields. If there are any erros it will collect them in a set, here called as result. Then we iterate all over the set and add the error messages to faces context.

The next few files demonstrate the same logic applied to 2 or more groups of date validations.

multiple date range validation
multiple date range validation


Backing Bean:


Plase feel free to comment or to suggest new features.
You can read more about this topic here
Download source code.