Shreyas arya

Generation

generate functionTue, 24 Jan 2023

Currently, WorkflowExecutionValidator performs validation for different use cases. As a result, it is quite difficult to track positive or negative scenarios. The aim is to simplify code. We need to create validators for each status. The validator will check if transition from current state is allowed or not. All validators are injected into WorkflowExecutionValidator. private List<Validator> validators WorkflowExecutionValidator will pick up an appropriate validator from a list and check if transition is allowed. Validators are implemented for each workflow execution status Validators are applied in production code

public class WorkflowExecutionValidator { private final List<Validator<WorkflowExecution>> validators = new ArrayList<>(); public void addValidator(Validator<WorkflowExecution> validator) { validators.add(validator); } public boolean validate(WorkflowExecution execution, WorkflowExecutionStatus newStatus) { final Validator<WorkflowExecution> validator = findValidator(execution); return validator.validate(execution, newStatus); } private Validator<WorkflowExecution> findValidator(WorkflowExecution execution) { return validators.stream() .filter(validator -> validator.canHandle(execution)) .findFirst() .orElseThrow(() -> new IllegalStateException("Can't find validator for workflow execution " + execution)); } } public interface Validator<T> { boolean validate(T execution, WorkflowExecutionStatus newStatus); boolean canHandle(T execution); }

Questions about programming?Chat with your personal AI assistant