DaveAKing / guava-libraries

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

ImmutableSet.toArray() redundant null checking #1500

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
ImmutableSet.toArray() does additional null checking for toArray() and 
toArray(T[]) calls:

at com.google.common.collect.ImmutableList.construct(ImmutableList.java:319)
at 
com.google.common.collect.ImmutableList.asImmutableList(ImmutableList.java:308)
at 
com.google.common.collect.RegularImmutableAsList.<init>(RegularImmutableAsList.j
ava:39)
at 
com.google.common.collect.ImmutableSet$ArrayImmutableSet.createAsList(ImmutableS
et.java:476)
at 
com.google.common.collect.ImmutableCollection.asList(ImmutableCollection.java:17
0)
at 
com.google.common.collect.ImmutableSet$ArrayImmutableSet.toArray(ImmutableSet.ja
va:446)

  /** {@code elements} has to be internally created array. */
  private static <E> ImmutableList<E> construct(Object... elements) {
    for (int i = 0; i < elements.length; i++) {
      ObjectArrays.checkElementNotNull(elements[i], i);
    }
    return new RegularImmutableList<E>(elements);
  }

Why not do the same as ImmutableList.toArray() - just copy elements to the 
freshly allocated array?

Original issue reported on code.google.com by Ash2kk@gmail.com on 8 Aug 2013 at 3:33

GoogleCodeExporter commented 9 years ago
What version of Guava are you using?  ArrayImmutableSet went away in January.

https://code.google.com/p/guava-libraries/source/detail?r=a072275f7ec6cd71835bae
f23020a766ba0497e6&path=/guava/src/com/google/common/collect/ImmutableSet.java

Original comment by lowas...@google.com on 8 Aug 2013 at 10:36

GoogleCodeExporter commented 9 years ago
I'm using 14.0.1 and it's there [1]. But not in HEAD. In HEAD it's 
RegularImmutableSet [2] and it uses ImmutableCollection.toArray() which uses 
copyIntoArray() which is overriden to use System.arrayCopy() [3]. So everything 
is great, this issue can be closed. 

[1]: 
https://code.google.com/p/guava-libraries/source/browse/guava/src/com/google/com
mon/collect/ImmutableSet.java?name=v14.0.1
[2]: 
https://code.google.com/p/guava-libraries/source/browse/guava/src/com/google/com
mon/collect/RegularImmutableSet.java
[3]: 
https://code.google.com/p/guava-libraries/source/browse/guava/src/com/google/com
mon/collect/RegularImmutableSet.java#72

Original comment by Ash2kk@gmail.com on 9 Aug 2013 at 3:14

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 9 Aug 2013 at 3:45

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