In my nano project, I decided to replace big nasty switch statement with map of commands. Now I'm in the process of tiding up my code and i want to create provider which provides me populated map.
I wonder, what will be best practice for that. Initially i was thinking about something like this:
@Provides
ActionResolver provideActionResolver(Dependency dep1,Dependency dep2) {
Map<SomeEnum,Action> map = new HashMap<>();
map.put(SomeEnum.A1,new Action1(dep1);
map.put(SomeEnum.A2,new Action2(dep2);
return new ActionResolver(map);
}
I wonder is it right way to go, and what is the best practice for this.
With Map Multibindings, if I'm correct, you could do the following:
@MapKey
@interface SomeEnumKey {
SomeEnum value();
}
@Provides
@IntoMap
@SomeEnumKey(SomeEnum.A1)
Action action1(@Named("dep1") Dependency dep1) {
return new Action1(dep1);
}
@Provides
@IntoMap
@SomeEnumKey(SomeEnum.A2)
Action action2(@Named("dep2") Dependency dep2) {
return new Action2(dep2);
}
@Provides
ActionResolver provideActionResolver(Map<SomeEnum, Action> actions) {
return new ActionResolver(actions);
}