A Tumblr Theme

it cannot be

Towards A More “Functional” Java (The Sweet Nectar of Guava)

Recently, on a client project, our development team completed a significant refactoring of a fairly large Java EE web application. Since this required updating many disparate parts of our codebase, some of which I was not as familiar with, it also served as an informal code review.

Once underway, I started noticing a fairly common pattern emerging - one that I have seen in many other applications. It goes like this: Given an existing collection of homogeneous objects, we wish to find the ones that match specific criteria(s). In other words, we wish to filter the collection.

Consider a trivial example, where, given a list of integers, we want to find all of the even numbers. Here is a naive Java implementation:

http://gist.github.com/817904

This could probably be optimized some more, but you get the idea.

If you are familiar with functional languages, you will recognize what we are doing is akin to a filter or select. For example, it’s very simple to achieve the identical functionality in Ruby:

http://gist.github.com/818049

But we know that Java lacks the syntactic sugar to be able to express this as simply as we can in Ruby and other functional languages. However, we can get part of the way there using the Guava project from Google. It greatly enhances the existing Collections API and adds some pseudo-functional features.

Here is another Java implementation, this time using some features from the Guava library.

http://gist.github.com/817936

Admittedly, this is overkill for such a trivial example. Also, it’s not immediately obvious what the Predicate interface and the apply() method we are implementing are actually doing. However, the power will start to shine through once we have more complex business logic and filter criteria.

Think about searching for a pair of new shoes from an online store. Almost all of today’s websites offer some sort of filter functionality to narrow down your results to display only those that match a particular criteria. This could be brand, availability, size, color, etc.

The following example demonstrates how we could filter a list of results to only return items that qualify for free shipping. For the sake of this example, an item qualifies for free shipping if it’s price is equal to or greater than $25 and it is available in inventory.

http://gist.github.com/818148

There is something in the light that I see.


Amen. Amen. Amen. This show is better than anything you are watching right now.

lonelysandwich:

“Louie” Season 1 Episode 8, the Pot Smoking Scene

with Josh Hamilton as Jeff the Neighbor and Audrey Gelber as his wife, Piss Shit Fart.

You should know that this scene is better than most scenes in most television shows. You should know that, and if you don’t then ‘Louie’ will be soon cancelled and you will not have cared.


mrgan:

I just had to get this out of my system, and I blame all of you for not making it first. Back in my day, we used to drain every droplet of fun out of memes before we discarded them like greasy burrito wrappers.

mrgan:

I just had to get this out of my system, and I blame all of you for not making it first. Back in my day, we used to drain every droplet of fun out of memes before we discarded them like greasy burrito wrappers.


Between the questionable headbands, the pink bow-ties, and the creeper kid in the front row, I’m not sure what I like the best. Between the questionable headbands, the pink bow-ties, and the creeper kid in the front row, I’m not sure what I like the best.

Between the questionable headbands, the pink bow-ties, and the creeper kid in the front row, I’m not sure what I like the best.


coketalk:

Ever since Elena Kagan was nominated, I’ve been scratching my head. She looked so damned familiar. It was driving me nuts until I suddenly remembered The Princess Bride. From now on, every time I see her in robes I won’t be able to get the Pit of Despair out of my head.

coketalk:

Ever since Elena Kagan was nominated, I’ve been scratching my head. She looked so damned familiar. It was driving me nuts until I suddenly remembered The Princess Bride. From now on, every time I see her in robes I won’t be able to get the Pit of Despair out of my head.