Skip to content

Instantly share code, notes, and snippets.

@sebastienvermeille
Created June 12, 2018 07:13
Show Gist options
  • Save sebastienvermeille/701ae50782f547ca6f0e49bcbc14fa25 to your computer and use it in GitHub Desktop.
Save sebastienvermeille/701ae50782f547ca6f0e49bcbc14fa25 to your computer and use it in GitHub Desktop.
public class Buncher extends AbstractFSM<State, Data> {
{
startWith(Idle, Uninitialized);
when(Idle,
matchEvent(SetTarget.class, Uninitialized.class,
(setTarget, uninitialized) ->
stay().using(new Todo(setTarget.getRef(), new LinkedList<>()))));
onTransition(
matchState(Active, Idle, () -> {
// reuse this matcher
final UnitMatch<Data> m = UnitMatch.create(
matchData(Todo.class,
todo -> todo.getTarget().tell(new Batch(todo.getQueue()), getSelf())));
m.match(stateData());
}).
state(Idle, Active, () -> {/* Do something here */}));
when(Active, Duration.ofSeconds(1L),
matchEvent(Arrays.asList(Flush.class, StateTimeout()), Todo.class,
(event, todo) -> goTo(Idle).using(todo.copy(new LinkedList<>()))));
whenUnhandled(
matchEvent(Queue.class, Todo.class,
(queue, todo) -> goTo(Active).using(todo.addElement(queue.getObj()))).
anyEvent((event, state) -> {
log().warning("received unhandled request {} in state {}/{}",
event, stateName(), state);
return stay();
}));
initialize();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment