Annotation library which create a decorator for interfaces that delegate any actions with a executor
With android gradle plugin:
provided 'fr.sagix:executordecorator-annotations:2.3'
annotationProcessor 'fr.sagix:executordecorator-compiler:2.3'Annotate a method that return a interface and a *Decorator will be generated
interface MyInterface {
void method();
}@ImmutableExecutorDecorator public MyInterface provideMyInterface() {
...
}Will generate:
public final class MyInterfaceDecorator implements MyInterface {
private final Executor executor;
private final MyInterface decorated;
public MyInterfaceDecorator(Executor executor, MyInterface decorated) {
this.executor = executor;
this.decorated = decorated;
}
@Override
public void method() {
executor.execute(new Runnable() {
@Override()
public void run() {
decorated.method();
}
});
}
}If you are using Dagger, then you could decorate your MyInterface using Dagger Module:
class ImmutableModule {
@ImmutableExecutorDecorator
MyInterface provideMyInterface(Executor executor, MyInterfaceImpl impl) {
return new MyInterfaceDecorator(executor, impl);
}
}For more example, look at the unit tests and the sample project.
The decorator can be mutable: @MutableExecutorDecorator and with a WeakReference with @WeakExecutorDecorator
ExecutorDecorator has been removed and must be replaced:
@ExecutorDecoratoror@ExecutorDecorator(mutable = false)should be replaced by@ImmutableExecutorDecorator@ExecutorDecorator(mutable = true)should be replaced by@MutableExecutorDecorator
If a project uses Kotlin with Dagger, a module can not provide generated classes.
So now, MutableDecorator<T> can be provided and a TDecorator class will be generated, implementing both T and MutableDecorator<T> interfaces.
A cast of MutableDecorator<T> to T can be done with method T asDecorated()