How to pass List<Identifier<Foo>> to method with List<Identifier<?>> parameter?

60 Views Asked by At

I have a class Identifier<T> which essentially is a type-safe wrapper around a UUID (so class Foo contains an Identifier<Foo>).

The FooStore class has a method List<Identifier<Foo>> bulkReadIdentifiers().

In another class, I need to turn this List<Identifier<Foo>> into a List<UUID>. Implementing a method that does this for Foo is trivial, but how do I declare the method so that it works for arbitrary classes, i.e. Bar instead of Foo?

The method List<UUID> extractUuids(List<Identifier<?>> identifiers) causes compile errors because the ? does not match Foo. Can this be solved without using a hack that involves raw lists and/or @SuppressWarnings?

1

There are 1 best solutions below

0
On BEST ANSWER

You can make the method generic:

<T> List<UUID> extractUuids(List<Identifier<T>> identifiers)