Why google-collections or guava contains semantically equal functions?
example:
static
<T> Predicate<T>
and(Predicate<? super T>... components)
static
<T> Predicate<T>
and(Predicate<? super T> first, Predicate<? super T> second)
I.e. all functions that can accept several arguments.
The second question why do defintion of such functions use generic <? super T> instead of <T>?
To answer the first question, the varargs version (
Predicate<? super T>...) will give you a warning about the unchecked creation of a generic array when called with several generic predicates (e.g.Predicate<T>). For the common case of combining two predicates, you don’t get that warning.To answer the second question, taking
Predicate<? super T>means you can pass in aPredicate<Object>(orPredicate<Number>or whatever) when calling the method to create aPredicate<Integer>. For example, ifPredicates.notNull()were aPredicate<Object>(as it should be) and you wanted to combine that and somePredicate<Integer>, it would not be possible if the arguments were required to be of typePredicate<T>.