Tag: Guava
-
Groovy + GPars: Handling Concurrency
PROBLEM Let’s assume given a list of user IDs (ex: 1, 2, 3, 4, and 5), we need to query 2 data sources to get the names and the addresses before returning a list of Employee objects. The Employee class looks like this:- We are going to explore multiple solutions to implement lookup(..) to run… Read More…
-
IntelliJ IDEA 14.1: Better equals(), hashCode() and toString()
PROBLEM Let’s assume we want to create the default equals(), hashCode() and toString() with the following bean:- Most IDEs, including older version of IntelliJ, have code generation features that would create something similar to this:- While it works, the generated code is usually crazy horrendous. SOLUTION With IntelliJ 14.x, it allows us to select templates… Read More…
-
Guava: Testing equals(..) and hashcode(..)
PROBLEM Let’s assume we want to test the following equals(..):- A correctly implemented equals(..) must be reflexive, symmetric, transitive, consistent and handles null comparison. In another word, you have to write test cases to pass at least these 5 rules. Anything less is pure bullshit. SOLUTION You can write these tests yourself… or you can… Read More…
-
Guava: Reducing Cyclomatic Complexity with Objects.firstNonNull(…)
PROBLEM Ever written code like this? While it works, it has several minor problems:- SOLUTION Guava provides a much cleaner solution to address these problems:- Read More…
-
Guava: FluentIterable vs Collections2
PROBLEM Guava’s Collections2 is great when dealing with collections, but it quickly becomes rather clumsy and messy when we try to combine multiple actions together. For example, say we have userIds, which is a collection of user IDs. For each user ID, we want to retrieve the Employee object and add it into an immutable… Read More…
-
Guava: Elegant Caching Mechanisms
There are many different ways to implement a caching mechanism. Google’s Guava provides very simple and elegant solutions to do so. OPTION 1: Caching using Supplier If you want to cache just one thing, Supplier should satisfy that requirement. In the above example, we cache all employees for a day. OPTION 2: Caching using LoadingCache… Read More…