dimanche 26 janvier 2020

Generic object with builder pattern

I have a Producer<?, ?> producer field in my class, which implementation depends on a given state using a builder pattern, for example:

private void changeImplementation(int state) {
    switch (state) {
        case 0:
            producer = builder
                           .setKey(Long.class)
                           .setValue(String.class)
                           .setOtherStuff(...)
                           .build() // return the object with correct key and value
        break;
        case 1:
            ...
}

But whenever I call a method on producer (for example with types Producer<Long, String>), this error is given (Eclipse EE):

The method method(Record<capture#9-of ?,capture#10-of ?>) in the type Producer<capture#9-of ?,capture#10-of ?> is not applicable for the arguments (Record<Long,String>)

Making a cast before build() or inside the method call didn't help. The build pattern works perfectly elsewhere in the project.

Aucun commentaire:

Enregistrer un commentaire