From 8dea842830a106fb676796217a3097882e8bb17f Mon Sep 17 00:00:00 2001 From: Janne Valkealahti Date: Sat, 12 Nov 2016 14:38:41 +0000 Subject: [PATCH] No null collections from AbstractState - Fix to create empty collections in favour of returning nulls. - Fixes #271 --- .../statemachine/state/AbstractState.java | 20 +++++++------- .../statemachine/state/ObjectState.java | 26 +++++++------------ .../statemachine/state/RegionState.java | 17 ++++-------- .../statemachine/state/StateMachineState.java | 13 ++++------ 4 files changed, 31 insertions(+), 45 deletions(-) diff --git a/spring-statemachine-core/src/main/java/org/springframework/statemachine/state/AbstractState.java b/spring-statemachine-core/src/main/java/org/springframework/statemachine/state/AbstractState.java index 54d2d036..ce53815d 100644 --- a/spring-statemachine-core/src/main/java/org/springframework/statemachine/state/AbstractState.java +++ b/spring-statemachine-core/src/main/java/org/springframework/statemachine/state/AbstractState.java @@ -17,6 +17,7 @@ package org.springframework.statemachine.state; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.Date; import java.util.List; import java.util.concurrent.ScheduledFuture; @@ -169,10 +170,10 @@ public abstract class AbstractState extends LifecycleObjectSupport impleme Collection> exitActions, Collection> stateActions, PseudoState pseudoState, Collection> regions, StateMachine submachine) { this.id = id; - this.deferred = deferred; - this.entryActions = entryActions; - this.exitActions = exitActions; - this.stateActions = stateActions; + this.deferred = deferred != null ? deferred : Collections.emptySet(); + this.entryActions = entryActions != null ? entryActions : Collections.>emptySet(); + this.exitActions = exitActions != null ? exitActions : Collections.>emptySet(); + this.stateActions = stateActions != null ? stateActions : Collections.>emptySet(); this.pseudoState = pseudoState; // use of private ctor should prevent user to @@ -190,7 +191,7 @@ public abstract class AbstractState extends LifecycleObjectSupport impleme @Override public boolean shouldDefer(Message event) { - return deferred != null && deferred.contains(event.getPayload()); + return deferred.contains(event.getPayload()); } @Override @@ -315,7 +316,11 @@ public abstract class AbstractState extends LifecycleObjectSupport impleme * @param triggers the triggers */ public void setTriggers(List> triggers) { - this.triggers = triggers; + if (triggers != null) { + this.triggers = triggers; + } else { + this.triggers.clear(); + } } /** @@ -344,9 +349,6 @@ public abstract class AbstractState extends LifecycleObjectSupport impleme * @param context the context */ protected void scheduleStateActions(StateContext context) { - if (stateActions == null) { - return; - } for (Action action : stateActions) { ScheduledFuture future = scheduleAction(action, context); if (future != null) { diff --git a/spring-statemachine-core/src/main/java/org/springframework/statemachine/state/ObjectState.java b/spring-statemachine-core/src/main/java/org/springframework/statemachine/state/ObjectState.java index 15f4b0bd..c314b7aa 100644 --- a/spring-statemachine-core/src/main/java/org/springframework/statemachine/state/ObjectState.java +++ b/spring-statemachine-core/src/main/java/org/springframework/statemachine/state/ObjectState.java @@ -142,14 +142,11 @@ public class ObjectState extends AbstractSimpleState { @Override public void exit(StateContext context) { super.exit(context); - Collection> actions = getExitActions(); - if (actions != null) { - for (Action action : actions) { - try { - executeAction(action, context); - } catch (Exception e) { - log.error("Action execution resulted error", e); - } + for (Action action : getExitActions()) { + try { + executeAction(action, context); + } catch (Exception e) { + log.error("Action execution resulted error", e); } } } @@ -157,14 +154,11 @@ public class ObjectState extends AbstractSimpleState { @Override public void entry(StateContext context) { super.entry(context); - Collection> actions = getEntryActions(); - if (actions != null) { - for (Action action : actions) { - try { - executeAction(action, context); - } catch (Exception e) { - log.error("Action execution resulted error", e); - } + for (Action action : getEntryActions()) { + try { + executeAction(action, context); + } catch (Exception e) { + log.error("Action execution resulted error", e); } } } diff --git a/spring-statemachine-core/src/main/java/org/springframework/statemachine/state/RegionState.java b/spring-statemachine-core/src/main/java/org/springframework/statemachine/state/RegionState.java index 76d3ac4c..01ced84a 100644 --- a/spring-statemachine-core/src/main/java/org/springframework/statemachine/state/RegionState.java +++ b/spring-statemachine-core/src/main/java/org/springframework/statemachine/state/RegionState.java @@ -113,8 +113,7 @@ public class RegionState extends AbstractState { for (Region r : getRegions()) { State state = r.getState(); if (state != null) { - Collection deferredEvents = state.getDeferredEvents(); - if (deferredEvents != null && deferredEvents.contains(event.getPayload())) { + if (state.getDeferredEvents().contains(event.getPayload())) { defer = defer & true; } else { defer = false; @@ -134,22 +133,16 @@ public class RegionState extends AbstractState { } region.stop(); } - Collection> actions = getExitActions(); - if (actions != null) { - for (Action action : actions) { - executeAction(action, context); - } + for (Action action : getExitActions()) { + executeAction(action, context); } } @Override public void entry(StateContext context) { super.entry(context); - Collection> actions = getEntryActions(); - if (actions != null) { - for (Action action : actions) { - executeAction(action, context); - } + for (Action action : getEntryActions()) { + executeAction(action, context); } if (getPseudoState() != null && getPseudoState().getKind() == PseudoStateKind.INITIAL) { diff --git a/spring-statemachine-core/src/main/java/org/springframework/statemachine/state/StateMachineState.java b/spring-statemachine-core/src/main/java/org/springframework/statemachine/state/StateMachineState.java index 09f128c3..9834f0f3 100644 --- a/spring-statemachine-core/src/main/java/org/springframework/statemachine/state/StateMachineState.java +++ b/spring-statemachine-core/src/main/java/org/springframework/statemachine/state/StateMachineState.java @@ -147,9 +147,8 @@ public class StateMachineState extends AbstractState { .getSource())) { getSubmachine().stop(); } - Collection> actions = getExitActions(); - if (actions != null && !isLocal(context)) { - for (Action action : actions) { + if (!isLocal(context)) { + for (Action action : getExitActions()) { executeAction(action, context); } } @@ -158,9 +157,8 @@ public class StateMachineState extends AbstractState { @Override public void entry(final StateContext context) { super.entry(context); - Collection> actions = getEntryActions(); - if (actions != null && !isLocal(context)) { - for (Action action : actions) { + if (!isLocal(context)) { + for (Action action : getEntryActions()) { executeAction(action, context); } } @@ -258,8 +256,7 @@ public class StateMachineState extends AbstractState { if (machine != null) { State state = machine.getState(); if (state != null) { - Collection deferredEvents = state.getDeferredEvents(); - if (deferredEvents != null && deferredEvents.contains(event.getPayload())) { + if (state.getDeferredEvents().contains(event.getPayload())) { return true; } }