In the following code sniplet, I want to specify that:
attachmentandhandlershare a generic type `<A>'s type only needs to be specified whennotify()is invoked- Invoking
notify()is optional.
I do not want to force users to specify <A> at class construction time because they might never end up invoking notify().
/**
* Builder pattern for some asynchronous operation.
*/
public class OperationBuilder
{
private A attachment = null;
private CompletionHandler<Integer, A> handler = null;
public <A> OperationBuilder notify(A attachment, CompletionHandler<Integer, A> handler)
{
this.attachment = attachment;
this.handler = handler;
return this;
}
public abstract build();
}
Is this possible under Java? If not, what would you recommend I do?
UPDATE: I don't need to specify that <A> associated with attachment and handler must be the same <A> associated with notify(). All I'm trying to specify is that attachment and handler must use the same type <A>.