Creating a fluent interface for Google Collections

Chris wrote about functional programming in Java. As an example he used Google Collections which “is a suite of new collections and collection-related goodness for Java 5.0”.

Looking at his post he gives the following code on how to use Goolge Collections

This is very verbose and hard to read. Mostly due to the static method calls and the seperation of the first “filter” and the “new PagesToFilesTransformation()” parameter. Using a static import we can reduce this to:

Better. I’ll show you how to make this even more readable with a fluent interface. I use the following example for this:

We want to move this code to a fluent interface. Martin Fowler wrote about Fluent Interfaces on his bliki and there is a entry on Wikipedia. They were made popular by JMock as a way to specify mock expectations.

What is a fluent interface though? For me it’s an interface to an API which is more consistent and readable. Which helps you do several steps with an API in a correct way. It’s often implemented with speaking method names and call chaining. These together create the illusion of natural, descriptive language. When the chained methods return the correct type, most often it’s impossible to call the wrong methods or methods in the wrong sequence. Therefor fluent interfaces create better readable code with less bugs. Which distinguishes them from domain specific languages (like in Ruby on Rails), macro programming (Lisp) or plain language constructs (methods which take closures) like “[1..5].each { … }”. The differences is fluent ;-) “5.times.each {…}” could be considered a simple fluent interface.

The most recent fluent interface in Java with some fame is Quaere, a kind of LINQ for Java for querying object graphs. There even is a promising Quaere API for JPA. I really think fluent interfaces are useful, although others think they are stupid. I keep to myself what I think is stupid and we see where this all is going to.

Back to our example. I’ve recently shown how to create a fluent interface for object creation. One for Google Collections might look like this:

This example isn’t optimal, but much more readable than the one given by Chris. The or() part could be improved because it doesn’t look very nice. This will change with closures. Fluent interfaces will boom with closures in Java 7.

In the comment to his blog post, Chris mentions “Making a fluent interface to do this might be a little risky, because I think there’s an expectation* that a method like Iterable.filter() wouldn’t modify the source iterable itself, but rather would return a new, filtered iterable.” We do not need to modify the Iterable, we only hold a reference and create new fluent interfaces on the fly as needed. Our code to enable a fluent interface for Google Collections this is rather simple:

This example has several main points. The with() static method call is descriptive and creates a fluent interface object for us to hold the Iterable object. Second most methods in fluent interfaces return the object itself. We create a new fluent interface object in filter to wrap the filtered iterable and enable chaining. My last example with object creation did hold the same bean and did not create new objects on the fly. This really depends on what should be done. We could have set the new iterable instead of creating a new wrapper like this:

This mostly depends on your style and if you think immutable objects are good or bad. For this I prefer the immutable Fluent Interface object over the mutable. Perhaps this is a pattern.

Conculsion:: A fluent interface for Google Collections would be useful. It’s more readable, less error prone and flows. Although my example works and is usable, there needs to go more work into a fluent interface for Google Collections to make it useful. For example support more Predicates and support transformations.

Thanks for listening.

197 thoughts on “Creating a fluent interface for Google Collections”

  1. Pingback: best
  2. Pingback: fucoxanthin
  3. Pingback: geniux pills
  4. Pingback: intelligex
  5. Pingback: intellux
  6. Pingback: probrain
  7. Pingback: radian c
  8. Pingback: supreme antler
  9. Pingback: testomax
  10. Pingback: TRACKR BRAVO
  11. Pingback: alpha f1
  12. Pingback: alpha fuel
  13. Pingback: alpha man pro
  14. Pingback: alpha peak
  15. Pingback: alpha xtrm
  16. Pingback: caralluma extract
  17. Pingback: anabolic rx24
  18. Pingback: cognifocus
  19. Pingback: garcinia melt
  20. Pingback: nerventrax
  21. Pingback: nutra forskolin
  22. Pingback: premium cleanse
  23. Pingback: testorip
  24. Pingback: testoroar
  25. Pingback: pro forskolin
  26. Pingback: forskolin slim
  27. Pingback: garcinia cleanse
  28. Pingback: garcinia xt
  29. Pingback: forskolin fuel
  30. Pingback: vitapulse
  31. Pingback: Vita pulse
  32. Pingback: vitapulse reviews
  33. Pingback: vitapulse review
  34. Pingback: pure colon detox
  35. Pingback: pure colon detox
  36. Pingback: somnapure
  37. Pingback: perfect biotics
  38. Pingback: reviews vitapulse
  39. Pingback: vitapulse vitamins
  40. Pingback: vitapulse scam
  41. Pingback: vitapulse
  42. Pingback: vitapulse
  43. Pingback: vita pulse
  44. Pingback: probiotic america
  45. Pingback: Panic Away review
  46. Pingback: T90 Xplode Review
  47. Pingback: Maxtropin Reviews
  48. Pingback: Tea tox
  49. Pingback: Tai Cheng
  50. Pingback: MegaDrox Reviews
  51. Pingback: Test X180
  52. Pingback: Test X180 Reviews
  53. Pingback: recapture 360
  54. Pingback: probiotics america
  55. Pingback: Perfect Biotics
  56. Pingback: niagen anti aging
  57. Pingback: check here
  58. Pingback: have a peek here
  59. Pingback: Get More Info
  60. Pingback: news
  61. Pingback: this page
  62. Pingback: check over here
  63. Pingback: Cash Blurbs Review
  64. Pingback: weblink
  65. Pingback: Source
  66. Pingback: this website
  67. Pingback: Half Day Diet Plan
  68. Pingback: anchor
  69. Pingback: here
  70. Pingback: dig this
  71. Pingback: Read More Here
  72. Pingback: his explanation
  73. Pingback: see this here
  74. Pingback: look at this site
  75. Pingback: official site
  76. Pingback: dig this
  77. Pingback: additional hints
  78. Pingback: Visit Website
  79. Pingback: here
  80. Pingback: check it out
  81. Pingback: see this here
  82. Pingback: her latest blog
  83. Pingback: his explanation
  84. Pingback: visit
  85. Pingback: Read More Here
  86. Pingback: official site
  87. Pingback: his explanation
  88. Pingback: click here
  89. Pingback: tires
  90. Pingback: rims
  91. Pingback: wheels
  92. Pingback: click here
  93. Pingback: click here
  94. Pingback: read more
  95. Pingback: read more

Comments are closed.