Executor Decorator

Additional

Language
Java
Version
v2.0 (Apr 12, 2017)
Created
Jun 28, 2016
Updated
Jul 22, 2021 (Retired)
Owner
Nicolas (sagix)
Contributors
Vincent Brison (vincentbrison)
Nicolas (sagix)
2
Activity
Badge
Generate
Download
Source code

Advertisement

Executor Decorator

Annotation library which create a decorator for interfaces that delegate any actions with a executor

Installation

With android gradle plugin:

provided 'fr.sagix:executordecorator-annotations:2.3'
annotationProcessor 'fr.sagix:executordecorator-compiler:2.3'

Usage

Annotate a method that return a interface and a *Decorator will be generated

Example

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.

Also

The decorator can be mutable: @MutableExecutorDecorator and with a WeakReference with @WeakExecutorDecorator

Migration from 1.0 to 2.0

ExecutorDecorator has been removed and must be replaced:

  • @ExecutorDecorator or @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()