I have a slightly odd issue with Java generics. Actually it might not be an issue at all and it might just me that I am trying to use generics incorrectly in design. I will not give the entire details of the application (it is irrelevant and boring).
I have created a small snippet that captures the essence of the problem.
There is a Producer interface which looks like this.
public interface Producer<P> {
public P produce();
}
There is an implementation of this interface which looks like this
public class IntegerProducer<P> implements Producer<P> {
@Override
public P produce() {
return (P)new Integer(10);
}
}
What I really need to do in the produce method is something like
if (P instanceOf Integer) return (P)new Integer(10);
else throw new Exception("Something weird happened in the client app");
Now of course the P instanceOf Integer won’t work. But if you have got the essence of what I am trying to do, would you please share a solution.
Thanks for helping.
Clarification 1:
The issue is not that I want to return Integer only. The issue is that in the function produce(), I need to check the type of generics used by the client program and change the behaviour of the function based on that. It is not that I want to restrict the type of generics used to a particular type of Object (where I could have used wildcards) but I need the function produce() to behave slightly differently based on the kind of Object used in the generics by the client code.
How about:
Since your
IntegerProducerdeals withIntegers only, you don’t need it to be generic. You can then write:EDIT
Following your comment, I think the only way would be to pass a class parameter:
Alternatively you can pass it in the constructor (strategy used for example by EnumSets and EnumMaps):
That clearly clutters the code and I would personally reconsider the design to avoid that kind of situation.