kucci / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

calling a member function / getter on each object of a collection #415

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I apologize if this is a duplicate or already existing functionality. Basically 
I'm trying to do the equivalent of the following in python:

    ",".join([ x.foo() for x in mycollection ])

where I'm currently struggling with the x.foo() part. As far as I understood 
would I need a class implementing com.google.common.base.Function (I think what 
I'm looking for is the equivalent of InvokerTransformer in Apache Commons 
Collections.)

A simple version of such a member accessing Function would be restricted to 
getters, one could imagine the following use case:

   Joiner.on(',').join(
     Collections2.transform(
       mycollection, 
         Functions.forGetter("foo")));

which then would call getFoo() on each member of mycollection.

[ A more generic case would be to produce a object implementing Function which 
binds all arguments except the one to be taken from the input collection. ]

I had a look into this a little and it turns out that getting the Class object 
(which is needed to get a member function given its name in a string) from a 
generic type F (the type of elements contained in the input follection) is not 
straightforward but probably doable.

Original issue reported on code.google.com by andre.ho...@gmail.com on 12 Sep 2010 at 8:25

GoogleCodeExporter commented 9 years ago
So basically you're looking for a Guava equivalent of Python's 
`operator.methodcaller` (which would have to implement `Function`)?

For the sake of completeness, `operator.attrgetter` and `operator.itemgetter` 
also belong to the team.

However, I'm (generally) a bit worried about using reflection as that breaks 
some links that allow secure refactoring through IDEs and stuff.

On the other hand: In the end, it all comes down to Java's verbosity for 
defining anonymous functions, and JDK 7 (or, as it seems, 8) is quite far away 
again.

Original comment by j...@nwsnet.de on 13 Sep 2010 at 8:25

GoogleCodeExporter commented 9 years ago
I think breaking refactoring is plenty reason enough to not do this. Something 
I've been doing occasionally is defining a Function for a getter as a public 
static final field in the class the method is defined in. Then you can 
reference the getter like "Bar.GET_FOO" which looks reasonably nice.

And yeah, it's really unfortunate that lambda expressions won't be in Java til 
mid-2012 at the soonest... when that's in, you'll be able to use method 
references like "Bar#getFoo()" as Functions.

Original comment by cgdec...@gmail.com on 13 Sep 2010 at 1:26

GoogleCodeExporter commented 9 years ago
Broken refactoring does seem like a big a penalty to incur for the benefit.  
I've seen the "Bar.GET_FOO" used a lot too, sometimes named "Bar.EXTRACT_FOO".

Original comment by boppenh...@google.com on 23 Sep 2010 at 4:43

GoogleCodeExporter commented 9 years ago
In fact, I started using Bar.GET_FOO as well, as I don't use too many getters 
anyway and initializing Bar.GET_FOO in Bar.java doesn't make the join statement 
more obscure to read (as an in-place definition of an anonymous class would do).

I guess I'm looking forward to JDK 7 or 8 then...

Original comment by andre.ho...@gmail.com on 23 Sep 2010 at 5:55

GoogleCodeExporter commented 9 years ago

Original comment by fry@google.com on 28 Jan 2011 at 8:27

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:15

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:09