Friday, June 18, 2010

Property interfaces and Guava functional programming

Something that I’ve been thinking about lately is how I can make it easier to use Guava’s methods that operate on Functions, Predicates, etc.

One simple use for Functions is to get properties of objects, allowing you to (among other things) create a transformed view of a collection containing a certain property from each element. For example, transforming a list of Person objects to a String collection of names. Any time you want to do something with a collection of objects based on a certain property of those objects, this is useful.

However, it's rather ugly and awkward to have to create an anonymous inner class inline when calling a method that uses a Function. While you can create static final instances of such functions and methods that return instances of functions, with many classes you could end up with many such instances and methods. One good way to reduce the number of Function objects you need to create and make it easy to work with certain common properties (such as IDs, names, etc.) on many objects is to create interfaces that each expose a single such property:

Not only does this help add consistency to your classes, it allows you to collect behavior that can work on any type of object that has the property the interface exposes. I'd probably name such classes as the plural of the property name ("Ids" and "Names", for example).

This makes working with the IDs of a list of things with IDs as easy as possible:

The GET function is exposed to allow its use for anything you might want a function for, not just tranform. For example, with a HasName interface and Names class similar to the code for IDs, I could do something like this:

This would of course filter a list of people to an iterable that only includes people with the name Bob. It uses static imports (because they make this stuff read a lot nicer), and the methods are:

I think this approach can make working with the properties of objects using Functions a lot easier and cleaner, while also encouraging more consistency between classes in general, which is nice.

3 comments:

Henryk Konsek said...

Goods stuff. Makes plain old static Java looks more like dynamic language.

balapacitto said...

What casino games are there that can be played online? - Dr
Some games 의왕 출장마사지 are available on other platforms. In the list below you'll 계룡 출장안마 see 화성 출장마사지 one such game as Slots Empire. Some 서산 출장샵 of them can be 세종특별자치 출장샵 found on NetEnt.

Unknown said...

What is the difference between Slot machines and casino
The difference is that slot 메이저벳 machines have different odds. A slot machine has odds 사설 토토 운영 넷마블 and 토토365 the bigger the payout, the better the odds of winning. What 토토 분석 is the difference between 먹튀 사이트 조회 slot

Post a Comment