Showing posts with label Guava. Show all posts
Showing posts with label Guava. Show all posts

Wednesday, January 29, 2014

Learning Guava -- Calibrating time using Stopwatch

Many of our day-to-day applications would need calibrating time taken between 2 points. In Java world we either depend on System.currentTimeMillis() or System.nanoTime(). But the pain here is, we have to do the required computations of getting to a proper granularity to understand the time taken.Would n't it be great it to have such an utility class which will give the required information in the granularity we need with minimum amount of boilerplate code?

Stopwatch is one such small and wonderful utility class in Guava which helps in calibrating elapsed time / duration between any 2 points in the logic. The advantage of using Guava's Stopwatch is you can get the elapsed time in any measure i.e. right from nanoseconds to days. This is possible because you can pass an enum argument type of TimeUnit class to get the elapsed time in the desired granularity.

Code snippet for the usage of the Stopwatch class:

Few caveats for using Stopwatch are you should not start an already started Stopwatch. One has to check if the Stopwatch is already running by invoking isRunning() method.
Stopwatch documentation says the following on the same:
Stopwatch methods are not idempotent; it is an error to start or stop a stopwatch that is already in the desired state.

Also, once I got burned down by StopWatch class of Apache Commons Lang. As I was working in an IDE on a Maven project, I could not relate to the difference between Stopwatch of Guava and StopWatch of Commons Lang as the class got auto imported into the code and then spent some 20 minutes trying to check my classpath, IDE setup, etc. Yes how stupid of me, right? So, please be careful in choosing the correct class.

Update on 05th April, 2015: After a fair bit of time here, I have moved on to GitHub hosted Octopress blogs. Please find me on http://P7h.org henceforth for all new updates.

Sunday, January 26, 2014

Learning Guava -- Load properties file using Guava

Guava code snippet for loading a properties file from classpath.

For more info, please check Resources class of Guava.

Update on 05th April, 2015: After a fair bit of time here, I have moved on to GitHub hosted Octopress blogs. Please find me on http://P7h.org henceforth for all new updates.

Wednesday, November 27, 2013

Learning Guava -- Google Guava blog series

​I have been a huge fan of Google Guava from the time I came across it 3 years back.
For starters, Guava is a project which contains many Google's core libraries like collections, caching, math, primitives, concurrency, networking, common annotations, string processing, I/O, refelction and many others.
It is very well designed API. Guava is designed, implemented and maintained by Google Engineers like Kevin Bourrillion and Kurt Alfred Kluever, etc.

Guava follows almost all the excellent patterns and practices mentioned in Effective Java book written by Joshua Bloch, who has designed the impeccable Java Collections API while he was at Sun. Later he joined Google. Under his mentor-ship, Google Guava got wings and became a very well designed and effective API, useful for many situations and scenarios with an ever-growing feature list. I ensure I add Guava dependency as the first thing to my Gradle or Maven build script. Guava makes Java code a lot more readable, clean, simple and elegant. It utilises the Java generics very well.

Consider the following example which I tweeted few months back.
Google Guava sample code

Which of the above versions looks fine? Obviously the second option, aint it?
There are many such examples where Guava wins by a margin compared to normal Java code and or other libraries like commons, etc.

Guava also helps for [in a way] functional programming too. There are few options which are really helpful there as well. Having said that, Guava creators implore the developers not to litter code with too much functional programming which might lead to unreadable code.

I will start with writing few posts on Google Guava with the tag, "LearningGuava". I have been using Guava extensively in almost every project of mine since few years. This will not only help some one else looking for info or starting on Google Guava, but as well as for me also so that I will remember in future if I need any quick snippet on something specific with Guava usage. That being the motivation, I hope it will be of good experience for you and me as well.

This post will have list of all the posts written for Google Guava. This post kinda serves as an Index and quick reference of my Google Guava posts.

  1. Load properties file using Guava
  2. Calibrating time using Stopwatch 

Update on 05th April, 2015: After a fair bit of time here, I have moved on to GitHub hosted Octopress blogs. Please find me on http://P7h.org henceforth for all new updates.

Saturday, December 22, 2012

Guava toString() method generator for IntelliJ IDEA

IntelliJ IDEA comes bundled with toString() plugin. If you check the plugins in Settings, you should find GenerateToString plugin, which generates toString() method with options for String concat, StringBuffer, StringBuilder and ToStringBuilder (of Apache Commons) for generating this method.

Google Guava has even better helper method for generating toString() method for a JavaBean or POJO, which is both clutter free and has a very consistent format; in the form of Objects.toStringHelper(this). It looks more elegant than any of the above solutions, actually. Also, as I make it a point to use Google Guava in all my projects, I have defined the following template in my toString() method generator settings in IntelliJ IDEA.

Follow the steps mentioned below for adding this template to your IntelliJ IDEA [picked up from SO]:
  • Go inside a Java Class in an editor in IntelliJ IDEA
  • Hit Alt + Insert to popup the "Generate" menu
  • Choose toString()
  • Click the "Settings" button
  • Go to the "Templates" tab
  • Create a new template named "Guava toString gen" (or any name you prefer)
  • Add the above code to the template
Yes, its that simple!!  After configuring the above template, whenever you want to generate a toString() method for a Class, IntelliJ IDEA generates toString() using Guava template you have added just now.

Now, go ahead and give this a try and also check the other method omitNullValues() which might be useful in some scenarios.


Also, as a bonus tip, Guava's Objects class also has 2 awesome helper methods which aide in writing readable equals() and hashcode() methods. You should try them too.


Update on 05th April, 2015: After a fair bit of time here, I have moved on to GitHub hosted Octopress blogs. Please find me on http://P7h.org henceforth for all new updates.