yf0994 / guava-libraries

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

Improve Sets.immutableEnumSet(Iterable<E> elements) to avoid copying when possible #1148

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I use Sets.immutableEnumSet(Iterable) when dealing with enums collections, but 
it's not as smart as ImmutableSet.copyOf(Iterable): it doesn't avoid copying 
when provided Iterable is instance of ImmutableEnumSet. 

If I define a method which accepts regular Set of enum values and want to do a 
defensive copy, I can either use ImmutableSet.copyOf(Set) which won't use 
EnumSet as delegate or use Sets.immutableEnumSet(Set) which will unnecessary 
copy the argument if it was ImmutableEnumSet instance.

I can't think of any reason against adding simple check at the beginning:

if (elements instanceof ImmutableEnumSet) {
  return (ImmutableEnumSet<E>) elements;
}

Since ImmutableEnumSet#isPartialView is always false there is no fear that 
underlying EnumSet will change and thus no copying is required. 

In current version Sets.immutableEnumSet does linear copy if its argument is 
not empty or EnumSet instance.

Original issue reported on code.google.com by xaerx...@gmail.com on 13 Sep 2012 at 4:31

GoogleCodeExporter commented 9 years ago
Makes sense to me.

Original comment by cpov...@google.com on 13 Sep 2012 at 4:40

GoogleCodeExporter commented 9 years ago

Original comment by lowas...@google.com on 13 Sep 2012 at 8:17

GoogleCodeExporter commented 9 years ago
Fix implemented and checked in internally; will be mirrored out in the near 
future.

Original comment by lowas...@google.com on 13 Sep 2012 at 10:06

GoogleCodeExporter commented 9 years ago

Original comment by lowas...@google.com on 26 Dec 2012 at 5:04

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:13

GoogleCodeExporter commented 9 years ago

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