I browsed some JAVA code made by Google, and I found the ImmutableSet: http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/ImmutableSet.html
They implemented the of() method with several other ways:
public static <E> ImmutableSet<E> of(E e1, E e2);
public static <E> ImmutableSet<E> of(E e1, E e2, E e3);
public static <E> ImmutableSet<E> of(E e1, E e2, E e3, E e4);
public static <E> ImmutableSet<E> of(E e1, E e2, E e3, E e4, E e5);
public static <E> ImmutableSet<E> of(E... elements);
I checked the implementation which is here:https://code.google.com/p/google-collections/source/browse/trunk/src/com/google/common/collect/ImmutableSet.java
There's a create method wiith the following signature:
private static <E> ImmutableSet<E> create(E... elements)
which wraps the
private static <E> ImmutableSet<E> create(Iterable<? extends E> iterable, int count);
method. The public methods just passes the parameters to the create(E... elements) signatured method which finally calls the other create method.
I guess that the public of methods with fixed count of parameter are unnecessary since we have the of(E... elements) method.
My question is that why did they do that like this? Performance? Or it's a pattern?
Thanks.
Method overloading increases the readability of the program. This provides flexibility to programmers so that they can call the same method for different types of data. This makes the code look clean. This reduces the execution time because the binding is done in compilation time itself.
Overloading is a powerful feature, but you should use it only as needed. Use it when you actually do need multiple methods with different parameters, but the methods do the same thing. That is, don't use overloading if the multiple methods perform different tasks.
Advantages of Method Overriding Method overriding helps in writing a generic code based on the parent class. It provides multiple implementations of the same method and can be used to invoke parent class overridden methods using super keyword. It defines what behavior a class can have.
It can't be related to performance, actually: All the methods delegate to the same creation method, which expects an array anyhow.
My guess is that it is related to warnings. Consider the following, minimal snippet:
import java.util.List;
class ImmutableSet<T>
{
}
public class ParametersTest
{
public static void main(String[] args)
{
List<String> list0 = null;
List<String> list1 = null;
of(list0, list1);
}
@SuppressWarnings("unchecked")
public static <E> ImmutableSet<E> of(E e1, E e2) {
return create(e1, e2);
}
public static <E> ImmutableSet<E> of(E... elements) {
return create(elements);
}
private static <E> ImmutableSet<E> create(E... elements)
{
return null;
}
}
The call to of
in the main method is fine: It matches the 2-args-version of the of
method. Now comment out the 2-args-version of the of
-method. Then the call is still OK, but will directly invoke the varags version. This will cause a generic array to be created, and cause a warning. (This warning is suppressed in the 2-args-version, obviously).
So to summarize, I assume that this is in order to avoid warnings for clients of the library who want to invoke the of
method with several objects of a generic type.
Fortunately, things like this will no longer be necessary in the future, thanks to http://docs.oracle.com/javase/7/docs/api/java/lang/SafeVarargs.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With