Skip to content

Commit

Permalink
Fix #443: Introduce composite guard (#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
banterCZ authored Oct 13, 2022
1 parent b6118d4 commit 0fe2a34
Showing 1 changed file with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.guard.Guard;
import org.springframework.statemachine.listener.StateMachineListener;
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
import org.springframework.statemachine.state.State;

import java.util.Arrays;
import java.util.EnumSet;

/**
Expand Down Expand Up @@ -316,18 +318,15 @@ private void configurePresenceCheckTransitions(StateMachineTransitionConfigurer<
.withExternal()
.source(OnboardingState.PRESENCE_CHECK_NOT_INITIALIZED)
.event(OnboardingEvent.PRESENCE_CHECK_INIT)
.guard(
context -> processIdentifierGuard.evaluate(context) && presenceCheckEnabledGuard.evaluate(context)
)
.guard(createCompositeGuard(processIdentifierGuard, presenceCheckEnabledGuard))
.action(presenceCheckInitAction)
.target(OnboardingState.PRESENCE_CHECK_IN_PROGRESS)

.and()
.withExternal()
.source(OnboardingState.PRESENCE_CHECK_IN_PROGRESS)
.event(OnboardingEvent.PRESENCE_CHECK_INIT)
.guard(context ->
processIdentifierGuard.evaluate(context) && presenceCheckEnabledGuard.evaluate(context))
.guard(createCompositeGuard(processIdentifierGuard, presenceCheckEnabledGuard))
.action(presenceCheckInitAction)
.target(OnboardingState.PRESENCE_CHECK_IN_PROGRESS)

Expand All @@ -350,7 +349,7 @@ private void configurePresenceCheckTransitions(StateMachineTransitionConfigurer<
.source(OnboardingState.CHOICE_PRESENCE_CHECK_PROCESSING)
.first(OnboardingState.PRESENCE_CHECK_VERIFICATION_PENDING, statusInProgressGuard)
.then(OnboardingState.OTP_VERIFICATION_PENDING,
context -> otpVerificationEnabledGuard.evaluate(context) && statusAcceptedGuard.evaluate(context),
createCompositeGuard(otpVerificationEnabledGuard, statusAcceptedGuard),
otpVerificationSendAction
)
.then(OnboardingState.CHOICE_VERIFICATION_PROCESSING,
Expand Down Expand Up @@ -379,9 +378,7 @@ private void configureOtpTransitions(StateMachineTransitionConfigurer<Onboarding
.withExternal()
.source(OnboardingState.OTP_VERIFICATION_PENDING)
.event(OnboardingEvent.OTP_VERIFICATION_RESEND)
.guard(
context -> processIdentifierGuard.evaluate(context) && otpVerificationEnabledGuard.evaluate(context)
)
.guard(createCompositeGuard(processIdentifierGuard, otpVerificationEnabledGuard))
.action(otpVerificationResendAction)
.target(OnboardingState.OTP_VERIFICATION_PENDING)

Expand All @@ -398,6 +395,10 @@ private void configureOtpTransitions(StateMachineTransitionConfigurer<Onboarding
.last(OnboardingState.OTP_VERIFICATION_PENDING);
}

private static Guard<OnboardingState, OnboardingEvent> createCompositeGuard(Guard<OnboardingState, OnboardingEvent>... guards) {
return context -> Arrays.stream(guards).allMatch(it -> it.evaluate(context));
}

private void configureCompletedTransition(StateMachineTransitionConfigurer<OnboardingState, OnboardingEvent> transitions) throws Exception {
transitions
.withChoice()
Expand Down

0 comments on commit 0fe2a34

Please sign in to comment.