Example usage for org.springframework.statemachine.guard Guard Guard

List of usage examples for org.springframework.statemachine.guard Guard Guard

Introduction

In this page you can find the example usage for org.springframework.statemachine.guard Guard Guard.

Prototype

Guard

Source Link

Usage

From source file:org.springframework.statemachine.recipes.tasks.TasksHandler.java

/**
 * Tasks choice guard. This {@link Guard} will check if related
 * extended state variables contains negative values for related
 * tasks id's and returns true if so, else false.
 *
 * @return the guard//from  w w w.  java  2 s  .  c  om
 */
private Guard<String, String> tasksChoiceGuard() {
    return new Guard<String, String>() {

        @Override
        public boolean evaluate(StateContext<String, String> context) {
            Map<Object, Object> variables = context.getExtendedState().getVariables();
            for (Entry<Object, Object> entry : variables.entrySet()) {
                if (entry.getKey() instanceof String
                        && ((String) entry.getKey()).startsWith(STATE_TASKS_PREFIX)) {
                    if (entry.getValue() instanceof Integer) {
                        Integer value = (Integer) entry.getValue();
                        if (value < 0) {
                            if (log.isDebugEnabled()) {
                                log.debug("Task id=[" + entry.getKey()
                                        + "] has negative execution value, tasksChoiceGuard returns true");
                            }
                            listener.onTasksError();
                            return true;
                        }
                    }
                }
            }
            listener.onTasksSuccess();
            return false;
        }
    };
}