Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do Guava classes provide so many factory methods instead of just one that takes varargs? [duplicate]

Possible Duplicate:
Why does Guava's ImmutableList have so many overloaded of() methods?

Looking at Guava's ImmutableList (and some other classes), you'll find plenty of overloaded of convenience methods ("Returns an immutable list containing the given elements, in order.") that take a different number of parameters:

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

All the way to this one:

public static <E> ImmutableList<E> of(E e1,
                                      E e2,
                                      E e3,
                                      E e4,
                                      E e5,
                                      E e6,
                                      E e7,
                                      E e8,
                                      E e9,
                                      E e10,
                                      E e11,
                                      E e12,
                                      E... others) 

Some colleagues of mine consider this silly, wondering why there's isn't just one method: of(E... elements). They suspect it's an ill-guided performance "optimisation" that falls into the category "do you think you're smarter than compiler", or something like that.

My hunch is that Kevin Bourrillion et al. put these methods in there for a real reason. Can anyone explain (or speculate) what that reason might be?

like image 459
Jonik Avatar asked Nov 30 '10 14:11

Jonik


1 Answers

The comment in the source says:

// These go up to eleven. After that, you just get the varargs form, and
// whatever warnings might come along with it. :(

So, this was done because varargs methods produce a warning with generic arguments.

like image 132
dogbane Avatar answered Oct 01 '22 19:10

dogbane