Skip to content

2016

The 'Now' Service

Quite many times, in our applications, we need to make use of the current time in milliseconds. Most of us follow the easy way and rely on java's System.currentTimeMillis().

The problem with arises when some unit tests need to be written that rely on that functionality. Additionally, a few times the developer needs to assert a specific time in his/her unit test. In order to avoid such situations someone could hide that logic behind a simple interface, with its solely purpose to return a long number indicating the number of milliseconds. Then the user can have one or multiple implementations of that, according to his/her needs. The main advantage with that is that the implementation becomes irrelevant. Hence, the user can use mocks when testing, or a dependency injection framework to supply the preferred implementation without having to touch the source code.

A sample implementation could be the following really simple functional interface.

An implementation which uses the beloved System.currentTimeMillis() could be the following:

The source code could be found at GitHub, along with some unit tests.

Design Patterns, Singleton

Singleton, is probably, the most controversial object oriented design pattern. Many people even consider it an antipattern.

It is used when one and only one instance of a class should exist per JVM (or classloader).

I personally believe it is a useful pattern and with some care it can definitely solve more problems than it can introduce ( http://www.javaworld.com/article/2074979/java-concurrency/double-checked-locking--clever--but-broken.html ).

There are quite a few implementations of the Singleton pattern, most of which are designed around JVM's and Java's intricacies. My favourite one is the lazily initialized one, using the holder idiom. The benefits of this implementation are that the class will only be initialized if and when needed and the thread safety is guaranteed by the language itself.

A sample implementation is the following:

For the complete example along with a unit test please see: Design Pattern Examples

The Apache Commons CLI, Command Line Parsing

Quite a few times when writing a Java application, there is the need of passing command line arguments to the program itself. Usually, the application would have to validate those arguments. For example, making sure that the user passed in a numeric value, or a boolean one. Sometimes validate that the user has passed in all the mandatory parameters etc. This process is usually very tedious.

Fortunately enough, there are a few libraries that can do that for us. My preferred one is the Apache Commons CLI. It's a great library, with an extremely straightforward to use API. It also provides multiple parser styles. Naming a few:

  • GNU like options (i.e --key ~/.ssh/key.pem)
  • POSIX like options (i.e -xvfz)
  • Short and long options (i.e -k ~/.ssh/key.pem or -key ~/.ssh/key.pem)
  • Java like property options (i.e -Dkeystore=~/.ssh/key.pem)

The library can automatically parse the arguments to their correct types (i.e Integer, String, Boolean etc), throwing appropriate exceptions when the user has passed a wrong argument type. Additionally, it validates those arguments and it ensures that all the mandatory arguments have been passed to the application. The programmer would only have to retrieve the values that the user has passed, without having to worry about anything else.

The Maven dependency is:

[xml] commons-cli commons-cli 1.2 [/xml]

As i mentioned above its API is simple self-explaining and straight forward. At the very least there are two things that a developer needs to create. The command line Options and a CommandLineParser.

The Options can be created like below

An example of creating the command line parser is below.

The entire example, along with some unit tests, can be found on Github.

IntelliJ Live Templates

I am a huge fan of IDEs. I truly believe that a powerful IDE can boost a programmer's productivity by orders of magnitude. Additionally, i also believe that each developer should treat his/her IDE as a tool in his/her toolbox and try to get the most out of it. Having said that, making ourselves familiar with the IDE's features (i.e debugging, profiling, refactoring utilities) and shortcut keys is a must. Everybody should invest time in those things. In the beginning it may be tedious, but in the long run it really worths it.

My personal choice of IDE is IntelliJ IDEA from Jetbrains. The Jetbrains guys have done an amazing job in creating one of the best technological products out there, according to myself. Their offerings are not only Java related for those who are interested in checking their products.

IntelliJ IDEA is such a powerful IDE that a developer can do almost everything from the keyboard. When i started using IntelliJ i forced myself to learn every shortcut key that i was using and just use that. In some cases i disconnected my mouse so that i would be forced to just type my way in achieving things. And i found out that i could work on it as comfortably with or without a mouse. I also found out that just by using the keyboard i was more productive, as my 'muscle memory' was kicking in every time and i did not have to take my hand off the keyboard to reach the mouse and think of the action that i wanted to perform, being in danger of loosing my momentum.

One of the features that i love and i use is their "Live Templates". The live templates is a mechanism where the developer can use already existing or create his/her own code snippets and add those to code by just typing an abbreviation followed by the 'tab' key. Someone can create a new live template or a new live template group by going to Preferences -> Editor -> Live Templates. It definitely makes sense to have the most common patters as live templates. Three of my favourite that i use almost always when i program are the following.

Unit test case: intelliJTestLiveTemplate

Main method: intelliJMainMethodLiveTemplate

SLF4J Logger: intelliJSLF4JLiveTemplate intelliJSLF4JLiveTemplateII

There are lots of others built into the IDE, like for-loops, System.out.println() statements etc, that are commonly used.  I believe that there is no limitation on any live template that someone want to create according to his/her needs.

But whatever tool you use in your everyday life, i would strongly recommend spending some time in learning that tool inside-out, as the benefits of that are tremendous.