Using the Reflection API, you can do something like this (note the 0name of the method):
public static Collection<WrapperFoo> wrapCollection0(Collection<Foo> src)
{
try
{
Class<? extends Collection> clazz = src.getClass();
Collection dst = clazz.newInstance();
for (Foo foo : src)
{
dst.add(new WrapperFoo(foo));
}
return dst;
} catch(Exception e)
{
e.printStackTrace();
return null;
}
}
Now we implement the overloading methods of a whole series with one layer, using the method described above (note 0in the calls):
public static ArrayList<WrapperFoo> wrapCollection(ArrayList<Foo> src)
{
return (ArrayList<WrapperFoo>) wrapCollection0(src);
}
public static Vector<WrapperFoo> wrapCollection(Vector<Foo> src)
{
return (Vector<WrapperFoo>) wrapCollection0(src);
}
...
source
share