I have a MyService interface with MyServiceImpl basic implementation.
I wrap MyServiceImpl into a chain of MyService decorators.
For that i’ve created a MyServiceDelegate which is an abstract class taking a MyService instance and delegating all the service calls to the provided MyService instance.
But in my unit tests, for a specific need, i need to find back the first real implementation of the MyService, which is MyServiceImpl, and i only have a MyService which refers to a decorator.
I just wonder if it is possible with Guava, to make a recursion in a functional way that will return me the “original service” that has been decorated with a lot of layers and is not a delegate service.
I think of using a function like Function that returns the delegate service, or null if it’s the original service, but don’t know were to go with it.
I know i can easily do it with a while loop, and i’m not looking for an alternative solution. Just want to know if Guava can solve these kind of problems.
You don’t even need a while loop or Guava. Just add the following method in your interface:
And then the following implementation in the delegate:
And the following implementation in the “real” implementation:
If the interface can’t be modified, do it externally: