I am trying to generically get vehicles by tags. How come I can't use the generic Collection
type to get vehicles? The CarTypeTags
and TruckTypeTags
are hashmaps of CarType
class and TruckType
class which extend from VehicleType
indexed by string (tag). Is there a way I can get the cars or trucks assigned to the types collection and return it?
Bonus: Does clazz.getClass()
return the class of the caller to this method?
public <T> Collection<T> getVehicleByTag(String tag) {
T clazz;
Collection<T> types;
if (clazz.getClass() == Car.class) {
types = (CarTypeTags.get(tag)); // error here: type mismatch
} else if (clazz.getClass() == Truck.class) {
types.addAll(TruckTypeTags.get(tag)); // error here too
}
if (types != null) {
return types;
}
return new ArrayList<T>();
}