DaveAKing / guava-libraries

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

Maps.asMap for immutables #1418

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
It would be nice to create a view of an ImmutableSet and a Function, but have 
it return an ImmutableMap.

It would mean that I could happily do the following without worrying about 
unnecessary copying and as a bonus the function will be `apply`d lazily:

    ImmutableMap.copyOf(Maps.asMap((ImmutableSet)keySet, function); //assume cast is safe

This principle could be applied across all the view generating methods to 
actually make copyOf smarter than I think...

My present situation is such that I have an object that accepts an 
ImmutableMap, which then uses it to perform some calculation. My input values 
actually has a well defined relationship with its keys which the calculation 
doesn't care about. So, it makes sense to construct a map from my function but 
it doesn't make sense to be actually pushing around all those instances.

Original issue reported on code.google.com by corin.la...@gmail.com on 18 May 2013 at 5:46

GoogleCodeExporter commented 9 years ago
Do you propose that the computed values are cached/stored upon first 
computation? If not, passing in a non-pure function would allow one to create a 
"mutable ImmutableMap".

(But even then, lazy computation and storage of values i.c.w. a non-pure 
function could make the ImmutableMap's final content dependent on the order in 
which keys are first accessed...)

Perhaps I misunderstood the request, though. For example, in your scenario, if 
the object to which you pass the ImmutableMap view will access all key/value 
pairs anyway, then you might as well copy eagerly. (Unless of course it's 
better that this work is amortized or performed in another thread.)

Side node: from an architectural point of view, shouldn't consumers generally 
ask for a Map and produce the ImmutableMap themselves, using #copyOf()? 
Performance-wise that's unlikely to make a difference, while providing more 
flexibility to API users. Anyway, I digress.

Original comment by stephan...@gmail.com on 18 May 2013 at 7:40

GoogleCodeExporter commented 9 years ago
I had made the assumption that Function was one-to-one and not a multivalued 
function... I think you are right, my proposal is not really worthy unless you 
can somehow guarantee that you have an ImmutableFunction (whatever that means).

Also, regarding to your last point, I was starting to think the along the same 
lines... The API should be using the Map *interface* not ImmutableMap so my use 
case is kinda out the window.

I'm happy to see this marked as WontFix :)

Original comment by corin.la...@gmail.com on 18 May 2013 at 12:48

GoogleCodeExporter commented 9 years ago

Original comment by cpov...@google.com on 18 May 2013 at 1:27

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

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

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

GoogleCodeExporter commented 9 years ago

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