I have a generics problem I am trying to resolve. I'm doing some manual casting and it feels like I'm doing something wrong. I'm a bit new to using generics, so it's very much possible I am misusing them in some capacity. Some guidance would be very much appreciated.
TLDR:
I have an interface with a generic method that takes a T argument. I'm implementing this interface in a class, but in the implementer I would like to ensure T is of a specific type (lets say Animal so I can extract some fields). How can I do that?
Details:
I have an interface Transformer below, and an implementing class where SpecialTransformer implements Transformer.
Transformer has this definition:
public interface Transformer
public <T> Event transform(T input);
whereas SpecialTransformer has this definition:
public class SpecialTransformer implements Transformer
@Override
public <T> Event transform(T input) {
Animal convertedInput = (Animal) input; // <------- This feels wrong
String color = convertedInput.getColor();
//Other field based logic
}
T extends Animal?