I haven't programmed in Java for a long time, the question can be stupid (But the advantages are hard) There is a simple Map
based Rule Engine:
public class TaskSuspenderActions { private static final Map<String, Supplier<TaskSuspenderAction>> actions; // TaskSuspenderAction - просто интерфейс, реализация в данном случае не важна static { final Map<String, Supplier<TaskSuspenderAction>> actions_t = new HashMap<>(); actions_t.put("SUCCESS", TaskSuspenderSuccessAction::new); actions_t.put("SETTING_UP", TaskSuspenderSettingUpAction::new); actions_t.put("NOT_FOUND", TaskSuspenderNotFountAction::new); actions_t.put("INTERNAL_ERROR", TaskSuspenderInternalError::new); actions = Collections.unmodifiableMap(actions_t); } @NotNull public static TaskSuspenderAction get(@NotNull final String internalStatus) { return actions.getOrDefault(internalStatus, TaskSuspenderDefault::new).get(); } }
The catch is that when I want to unify this class, to create a new engine (Suppose: TaskSuspenderActions<SomeInterface>
), firstly: I cannot substitute the template parameter in Supplier<SomeInterface>
because, if I understood correctly, erasure etc. Secondly: how can I pass a link to new
in Supplier, taking into account the type erasure, to add a new action?