No null collections from AbstractState

- Fix to create empty collections in
  favour of returning nulls.
- Fixes #271
This commit is contained in:
Janne Valkealahti
2016-11-12 14:38:41 +00:00
parent 58326fc948
commit 8dea842830
4 changed files with 31 additions and 45 deletions

View File

@@ -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<S, E> extends LifecycleObjectSupport impleme
Collection<? extends Action<S, E>> exitActions, Collection<? extends Action<S, E>> stateActions,
PseudoState<S, E> pseudoState, Collection<Region<S, E>> regions, StateMachine<S, E> submachine) {
this.id = id;
this.deferred = deferred;
this.entryActions = entryActions;
this.exitActions = exitActions;
this.stateActions = stateActions;
this.deferred = deferred != null ? deferred : Collections.<E>emptySet();
this.entryActions = entryActions != null ? entryActions : Collections.<Action<S, E>>emptySet();
this.exitActions = exitActions != null ? exitActions : Collections.<Action<S, E>>emptySet();
this.stateActions = stateActions != null ? stateActions : Collections.<Action<S, E>>emptySet();
this.pseudoState = pseudoState;
// use of private ctor should prevent user to
@@ -190,7 +191,7 @@ public abstract class AbstractState<S, E> extends LifecycleObjectSupport impleme
@Override
public boolean shouldDefer(Message<E> event) {
return deferred != null && deferred.contains(event.getPayload());
return deferred.contains(event.getPayload());
}
@Override
@@ -315,7 +316,11 @@ public abstract class AbstractState<S, E> extends LifecycleObjectSupport impleme
* @param triggers the triggers
*/
public void setTriggers(List<Trigger<S, E>> triggers) {
this.triggers = triggers;
if (triggers != null) {
this.triggers = triggers;
} else {
this.triggers.clear();
}
}
/**
@@ -344,9 +349,6 @@ public abstract class AbstractState<S, E> extends LifecycleObjectSupport impleme
* @param context the context
*/
protected void scheduleStateActions(StateContext<S, E> context) {
if (stateActions == null) {
return;
}
for (Action<S, E> action : stateActions) {
ScheduledFuture<?> future = scheduleAction(action, context);
if (future != null) {

View File

@@ -142,14 +142,11 @@ public class ObjectState<S, E> extends AbstractSimpleState<S, E> {
@Override
public void exit(StateContext<S, E> context) {
super.exit(context);
Collection<? extends Action<S, E>> actions = getExitActions();
if (actions != null) {
for (Action<S, E> action : actions) {
try {
executeAction(action, context);
} catch (Exception e) {
log.error("Action execution resulted error", e);
}
for (Action<S, E> action : getExitActions()) {
try {
executeAction(action, context);
} catch (Exception e) {
log.error("Action execution resulted error", e);
}
}
}
@@ -157,14 +154,11 @@ public class ObjectState<S, E> extends AbstractSimpleState<S, E> {
@Override
public void entry(StateContext<S, E> context) {
super.entry(context);
Collection<? extends Action<S, E>> actions = getEntryActions();
if (actions != null) {
for (Action<S, E> action : actions) {
try {
executeAction(action, context);
} catch (Exception e) {
log.error("Action execution resulted error", e);
}
for (Action<S, E> action : getEntryActions()) {
try {
executeAction(action, context);
} catch (Exception e) {
log.error("Action execution resulted error", e);
}
}
}

View File

@@ -113,8 +113,7 @@ public class RegionState<S, E> extends AbstractState<S, E> {
for (Region<S, E> r : getRegions()) {
State<S, E> state = r.getState();
if (state != null) {
Collection<E> 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<S, E> extends AbstractState<S, E> {
}
region.stop();
}
Collection<? extends Action<S, E>> actions = getExitActions();
if (actions != null) {
for (Action<S, E> action : actions) {
executeAction(action, context);
}
for (Action<S, E> action : getExitActions()) {
executeAction(action, context);
}
}
@Override
public void entry(StateContext<S, E> context) {
super.entry(context);
Collection<? extends Action<S, E>> actions = getEntryActions();
if (actions != null) {
for (Action<S, E> action : actions) {
executeAction(action, context);
}
for (Action<S, E> action : getEntryActions()) {
executeAction(action, context);
}
if (getPseudoState() != null && getPseudoState().getKind() == PseudoStateKind.INITIAL) {

View File

@@ -147,9 +147,8 @@ public class StateMachineState<S, E> extends AbstractState<S, E> {
.getSource())) {
getSubmachine().stop();
}
Collection<? extends Action<S, E>> actions = getExitActions();
if (actions != null && !isLocal(context)) {
for (Action<S, E> action : actions) {
if (!isLocal(context)) {
for (Action<S, E> action : getExitActions()) {
executeAction(action, context);
}
}
@@ -158,9 +157,8 @@ public class StateMachineState<S, E> extends AbstractState<S, E> {
@Override
public void entry(final StateContext<S, E> context) {
super.entry(context);
Collection<? extends Action<S, E>> actions = getEntryActions();
if (actions != null && !isLocal(context)) {
for (Action<S, E> action : actions) {
if (!isLocal(context)) {
for (Action<S, E> action : getEntryActions()) {
executeAction(action, context);
}
}
@@ -258,8 +256,7 @@ public class StateMachineState<S, E> extends AbstractState<S, E> {
if (machine != null) {
State<S, E> state = machine.getState();
if (state != null) {
Collection<E> deferredEvents = state.getDeferredEvents();
if (deferredEvents != null && deferredEvents.contains(event.getPayload())) {
if (state.getDeferredEvents().contains(event.getPayload())) {
return true;
}
}