I have a method.
It accepts a param, it can either be an object of the Provider or String class.
Now if I tell that method to accept Object then I can pass it Provider or String object, but I want to make it type safe using generics and pass only either Provider or String. How do I do that, is it possible?
I want to achieve something like this:
public <T can be String or Provider> void myMethod(T value)
I had a look at this question, but it would not work in my case because none of Provider or String is an interface.
You should not (and can not) use generics for this. Instead simply provide two overloaded method:
Since both need to have some different handling anyway it’s actually simpler this way.