Skip to content
Advertisement

Java erasure with generic overloading (not overriding)

I have FinanceRequests and CommisionTransactions in my domain. If I have a list of FinanceRequests each FinanceRequest could contain multiple CommisionTransactions that need to be clawed back. Dont worry how exactly that is done.

The class below (very bottom) makes me feel all fuzzy and warm since its succint and reuses existing code nicely. One problem Type erasure.

JavaScript

They both have the same signature after erasure, ie:

JavaScript

So eclipse complainst that:
Method clawBack(Collection) has the same erasure clawBack(Collection) as another method in type CommissionFacade

Any suggestions to restructure this so that it still an elegant solution that makes good code reuse?


JavaScript

Advertisement

Answer

Either rename the methods, or use polymorphism: use an interface, and then either put the clawback code in the objects themselves, or use double-dispatch (depending on your design paradigm and taste).

With code in objects that would be:

JavaScript

I prefer this approach, since I’m of the belief your domain should contain your logic; but I’m not fully aware of your exact wishes, so I’ll leave it up to you.

With a double dispatch, you would pass the “ClawbackHandler” to the clawback method, and on the handler call the appropriate method depending on the type.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement