Question

Today I wrote the following article in my blog about how Google Guavas Predicates can be used to increase the extensibility of your design. This made me ask myself the following question: "Is the need for this solution caused by a deeper design flaw?". Is there another "cleaner" way of achieving this type of extensibility? Am I just thinking too deep about this?

Was it helpful?

Solution

Google Guava adds several things to Java that should have been there but weren't (many of which are included in v7).

IMO Guava tends to be very clean and fast. Compare Guava's ComparisonChain with (for instance) Apache Commons CompareToBuilder classes. Both do essentially the same thing. While Apache's is very simple and easy enough for newbies to understand, Guava's is the faster/cleaner/less-resource-intensive (better?) approach.

As far as Predicates go, they serve a function and they do it well, especially when you combine it with all of the other Guava stuff like Iterators.filter.

The Guava tools do need to be understood before they are used though. For instance, if you are going to loop through the filtered list multiple times and your Predicate has a non-negligible cost (CPU/memory/time), then you may be better off looping through it once and storing the values in a Collection first, as some of these methods just wrap the given Iterable into a class whose next() function just calls next() on the unfiltered Iterable until it finds an entry that passes the filter and returns that.

So, yes, Guava is very useful. Including Predicates and Iterables and Collections2 filtering, etc. However, it always helps to know what is going on under the hood and what gotchas or performance implications you may need to deal with if you find that your code needs optimization.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top