Embracing the Messiness in Search of Epic Solutions

Tag: Java

  • 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…

  • Guava: Elegant Collection Ordering

    PROBLEM Java provides a built-in API to sort a list, but it is flat out ugly and prone to error especially when dealing with Comparator. Here’s an example:- Number of puppies killed = 3 … from the if-else statement. SOLUTION A better way to do this is to use Ordering from Guava:- </request,> In this… Read More…

  • Java: Invoking Secured Web Service with JSESSIONID

    PROBLEM I wrote a JSP custom tag that invokes a secured web service within the same application to perform some evaluation. This custom tag is only used in the secured views where the user has successfully authenticated against Spring Security, and they have access to these views. The secured web service is also guarded by… Read More…

  • Java: Performing 2-Key Lookup using HashBasedTable

    PROBLEM HashMap is great to perform a simple lookup, for example:- However, what if we need 2 keys to lookup a value? For example, we need “key1” + “key2” in order to lookup “mike”. SOLUTION 1: Create combo key You can combine the keys to create a unique key:- Assuming if the keys are just… Read More…

  • How to Unit Test Spring MVC Controller

    SCENARIO Let’s assume we have the following controller that needs to be tested:- SOLUTION 1: “Works but It Won’t Get You the Promotion” This working solution relies on:- While this solution works, but it has a few problems. This test case strictly tests the actual controller API, but it completely disregards the URI and request… Read More…

  • Hibernate: Migrating from XML-Based Configuration to Annotation-Based Configuration

    Overview At some point of time, as your project scope grows, the Hibernate mapping XML files are going to get to a point where it becomes very difficult to maintain. This is where the annotation-based configuration comes in. It took me a few years to convince myself that annotation-based configuration is the way to go.… Read More…