I have 3 methods:
//1 -- check one item
public static <T> void containsAtLeast(String message,
T expectedItem,
Collection<? extends T> found) {
if (!found.contains(expectedItem))
Assert.fail("...");
}
//2 -- check several items
public static <T> void containsAtLeast(String message,
Collection<? extends T> expectedItems,
Collection<T> found) {
for (T exptetedItem : expectedItems)
containsAtLeast(message, exptetedItem, found);
}
//3 -- check several items, without message parameter
public static <T> void containsAtLeast(Collection<? extends T> expectedItems,
Collection<? extends T> found) {
containsAtLeast(null, expectedItems, found);
}
I would expect that the method //3
invoke //2
but it does not, it invoke method //1
. Is there a mistake in what I expect?
*I use sdk 1.7.0_25 and Eclipse 4.3*
Your second method expects the generic type of expectedItems
(? extends T
) to be a subtype of the generic type of found
(T
).
In your third method, there is no subtype relationship between the two generic types. They both extend T
but could be siblings for example.
So the second method can't be called.
Example: imagine you call the third method with those types:
containsAtLeast(Collection<Integer> e, Collection<String> f)
So the T
in your third method is Object
. And your first method is called with T = Object
too.
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