Apr/1011
Google collections made easier
Due to legal/ethical objections around trademark violation the project presented here has been renamed to compacted-collections.
I spent a few hours over the last two weeks working on a little project that aims to make google collections even easier to use. I set out to make a thin wrapper that mimics the way the ruby programming language does collections.
The result is compacted-collections and this is what you can do with it...
Sorting:
import static com.compactcode.FluentList.fluent; fluent(2, 1, 3, 4).sort(); fluent(1, 2, 3, 4).reverse();
Filtering:
import static com.compactcode.FluentList.fluent; import static org.hamcrest.number.OrderingComparisons.greaterThan; import static org.hamcrest.text.StringStartsWith.startsWith; import static com.google.common.base.Predicates.equalTo; fluent("foo", "bar", "baz").filter(startsWith("ba")); fluent(1, 2, 3, 4).filter(greaterThan(2)); fluent(1, 2, 3, 4).find(equalTo(3)); fluent(1, 2, 3, 4).first(); fluent(1, 2, 3, 4).last();
In order to provide a bunch of out of the box predicates the project has been tightly integrated with the Hamcrest project. You can use hamcrest matchers interchangeably with predicates.
Transformation:
import static com.compactcode.FluentList.fluent; import static com.compactcode.Functions.toInt; import static com.compactcode.Functions.sum; fluent("1", "2", "3", "4").map(toInt()); fluent(1, 2, 3, 4).reduce(sum());
Like google collections everything statically typed, so you will get instant feedback about your mistakes. The really nice thing though is that the interface is fluent so you can chain together operations in a nice readable way.
Transformation + Filtering + Sorting:
import static com.compactcode.FluentList.fluent; import static com.compactcode.Functions.toInt; import static org.hamcrest.number.OrderingComparisons.greaterThan; fluent( "4", "1", "2").map(toInt()).filter(greaterThan(1)).sort();
Ignoring the imports it doesn't really read much different from a pure ruby implementation...
["4", "1", "2"].map{|it| it.to_i}.select{|it| it > 1}.sort()
If you're interested in having a look the code it's all available on github. You can clone it, download it or fork it to your hearts content. The project is built using maven so it should be easy to get it working in your favourite IDE.
I'll be posting more about the features of the library over the next weeks so if you like what you see and you'd like something explained or improved, let me know.
Enjoy!
April 20th, 2010
I just saw op4j (http://www.op4j.org/) earlier today. Looks very similiar.
April 20th, 2010
Why not call the method “list”, rather than “fluent”? The word “fluent” doesn’t evoke list creation to me at all.
April 20th, 2010
I agree with John Watson
April 20th, 2010
@ Vadim
I’m not familiar with op4j but I will check it out. lamdaj (http://code.google.com/p/lambdaj/) is another one worth checking out. There are quite a few similar projects around. I created fluent-google-collections because I wanted something really simple for working with lists that leveraged existing high quality libraries.
@John
I’m glad you mentioned the method name fluent. I’ve been thinking about changing this for a while but hadn’t settled on a decision. I’m thinking listOf as it reads slightly better than list (imo) when using static imports.
Thanks for the feedback
April 20th, 2010
Why is this project called “fluent-google-collections” and not “fluent-collections” or “fluent-rubylike-collections”? What does Google have to do with it, other than you’ve chosen to have a dependency on two or three (non-collection-based) Google classes?
April 20th, 2010
All the underlying transformation, filtering, sorting and joining is being performed by the google collections api. You can see the source code and relevant imports here. I’ll take it as a compliment that the library does a good job hiding this complexity
April 21st, 2010
@Shanon: Your code may be great, and you may be well-intentioned, giving props to Google for doing the heavy lifting…but (disclaimer: IANAL) the legally/ethically right thing for you to do is to thank Google in your dependencies, not by appropriating their trademark to use, without their permission, on your library.
Why didn’t you call your library fluent-oracle-collections? Surely your code leverages much more Oracle code (via acquisition) than Google. Perhaps you could rename your library to something you have a reasonable claim to use, like “fluent-compactcode-collections” or “fluent-shanon-collections”?
(If you actually got Google’s permission/blessing, my apologies, and I completely retract this comment.)
April 21st, 2010
The main reason for the name is to help communicate the intent. The project is specifically designed to be used on top of google gollections and cannot be used otherwise.
You do make a good point though, does anyone else think I should change the name?
April 21st, 2010
I think that you have a point for naming your project with the current name to be able to communicate your intentions. But I think also that Mike is right.
- One solution could be that you change your project’s name to something else, but keep your project description to include what ever you want to say. Even if it would be a simple sentence that stresses your intentions of the project.
- Other one is that you contact Google and take their permission..
- A better and last option, may be instead of taking permission to name your project, why don’t you contact them and try to add you code to the original project’s code base
April 21st, 2010
Hehe I really like your last suggestion
I’d love to see the code integrated into the core google collections/guava project. Definately preferable to being sued for trademark violation!
May 26th, 2010
This is awesome. Google Common Collections has totally revolutionized my coding. This project will take it even further. Thanks for the good work.