jiweigang1 / google-collections

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

2-, 3-, 4-, ... element of() methods for ImmutableSet #199

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Collection factory methods like Lists.immutableList(E...) generate 
"unchecked" type warnings when callers invoke them with a generic type 
argument.

It would be great if you'd add helper methods that suppress the warnings 
for small collections (the most common cases, e.g. size <= 10), so that 
client code wouldn't have to. For example, something like this:

  public static <E> List<E> immutableList(E element) {
    return Collections.singletonList(element);
  }

  @SuppressWarnings("unchecked")
  public static <E> List<E> immutableList(E e1, E e2) {
    return immutableList(Arrays.asList(e1, e2));
  }

  @SuppressWarnings("unchecked")
  public static <E> List<E> immutableList(E e1, E e2, E e3) {
    return immutableList(Arrays.asList(e1, e2, e3));
  }

  ...

This would be useful for all of the varargs factory methods in Lists in 
Sets.

Original issue reported on code.google.com by jaredjac...@gmail.com on 23 Jun 2009 at 4:40

GoogleCodeExporter commented 9 years ago
These methods were removed in revision 47, which came before the 20080818 
release. 
ImmutableList, ImmutableSet, etc. now feature the overloads you've requested.

Original comment by cpov...@google.com on 23 Jun 2009 at 4:54

GoogleCodeExporter commented 9 years ago
Thanks for the response, but I don't see the methods that I've requested in 
ImmutableList or ImmutableSet. They only have the singleton case and the 
varargs case, 
not the N in 2..10 cases. Please re-open, or close on different grounds. Thanks.

Original comment by jaredjac...@gmail.com on 23 Jun 2009 at 5:48

GoogleCodeExporter commented 9 years ago
I believe the 'of(...)' methods do what you want:

http://code.google.com/p/google-collections/source/browse/trunk/src/com/google/
common/collect/ImmutableList.java

See methods:

public static <E> ImmutableList<E> of(E e1, E e2)
public static <E> ImmutableList<E> of(E e1, E e2, E e3)
...

and similar in ImmutableSet.

Original comment by cresw...@gmail.com on 23 Jun 2009 at 5:59

GoogleCodeExporter commented 9 years ago
Revisions 57, also part of the 20080818 release, added the methods to 
ImmutableList.
 I see that I'm wrong about ImmutableSet, and now that you mention it, I don't think
we ever decided whether to supply these methods for all the Immutable* classes 
or how
many arguments to support.

Original comment by cpov...@google.com on 23 Jun 2009 at 6:05

GoogleCodeExporter commented 9 years ago

Original comment by cpov...@google.com on 23 Jun 2009 at 6:06

GoogleCodeExporter commented 9 years ago
> or how many arguments to support.
Please support more than now - at least 20 if you ask me.
It's very annoying to reformat the code to use the builder pattern if you add 
the 7th
constant to a list. 
I was wondering whether or not to make this a separate issue.

Original comment by zeitlin...@gmail.com on 1 Jul 2009 at 8:14

GoogleCodeExporter commented 9 years ago
You don't _have_ to reformat the code; you could just prepend

  @SuppressWarnings("unchecked") // stupid varargs warning

No matter where we cut off these overloads, someone will always be just over 
the line 
and will wonder why we don't add just a few more; to stave off all that 
insanity we 
decided to keep it to five.

We will probably make ImmutableList.of() the lone exception, and equip it with 
numerous overloads (to a point), so that it can be your go-to "turn varargs 
into a 
collection warning-free" method.

Original comment by kevin...@gmail.com on 1 Jul 2009 at 5:05

GoogleCodeExporter commented 9 years ago
In leaving my last comment, I forgot to notice that indeed, we HAVE forgotten 
to add 
the of(E, E)-style overloads going up to 5.  I argued against going further 
than 5, 
but I thought we had gone up to 5 already. We will fix this.

Original comment by kevin...@gmail.com on 7 Aug 2009 at 6:26

GoogleCodeExporter commented 9 years ago
Fixed for next RC.

Original comment by kevin...@gmail.com on 12 Aug 2009 at 5:03

GoogleCodeExporter commented 9 years ago
Great! Thanks Kevin.

Original comment by jaredjac...@gmail.com on 14 Aug 2009 at 7:06