Balzanka / guava-libraries

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

Allow ImmutableXXXMultimap to wrap existing immutable collections #1341

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Example:

Sometimes I want to create an ImmutableSetMultimap<K,V> given a key K and an 
existing ImmutableSet<V> of values (to adhere to an interface requiring a 
Multimap).

There's no way to do this without unnecessarily making a copy of the values.

More generally, there should be a way to create an ImmutableSetMultimap from an 
existing map ImmutableMap<K, ImmutableSet<V>> without copying any values--it's 
safe to simply wrap the existing immutable collections.

* ImmutableSetMultimap.of(K, ImmutableSet<V>) would work for the singleton 
case. 

* ImmutableSetMultimap.wrap( ImmutableMap<K,ImmutableSet<V>> ) would work for 
all others (shouldn't name it copyOf because it doesn't technically copy the 
input).

The same exact approach could be applied to ImmutableMultimap and 
ImmutableListMultimap as well.

Original issue reported on code.google.com by ori.schw...@gmail.com on 15 Mar 2013 at 3:45

GoogleCodeExporter commented 9 years ago
In general, you *cannot* guarantee that an ImmutableMap<K, ImmutableSet<V>> can 
be safely wrapped as an ImmutableSetMultimap, due to the specific case of empty 
value sets.

What might be doable -- though it would convolute the implementation 
significantly -- would be to make e.g. ImmutableSetMultimap.Builder smarter 
about recognizing in put(K, Iterable<? extends V>) when the value collection is 
already immutable and no other values are added for that key.

Original comment by wasserman.louis on 16 Mar 2013 at 3:52

GoogleCodeExporter commented 9 years ago
That said, do you have empirical data suggesting that this is actually a 
performance bottleneck for your application?

Original comment by wasserman.louis on 16 Mar 2013 at 3:53

GoogleCodeExporter commented 9 years ago
No, this is definitely not a performance bottleneck. I only opened the issue 
because I thought it would be a quick fix but as your comment indicates there 
is more to it than simply wrapping existing immutable collections. (I'm also 
now seeing that the size of the Multimap would have to be calculated by 
iterating over the wrapped Map's entries.)

I don't believe this is worth fixing if it complicates the implementation.

Thanks for the reply, the Guava APIs are a delight to use!

Original comment by ori.schw...@gmail.com on 16 Mar 2013 at 3:17

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 8 Apr 2013 at 7:00

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 1 Nov 2014 at 4:18

GoogleCodeExporter commented 9 years ago

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