dherik / state-machine

A state machine write in Java using states, transitions and conditions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

State Machine

A state machine write in Java using states, transitions and conditions. This code was inspired by the following answer in Stack OverFlow: http://stackoverflow.com/a/2064170/2387977.

Build

Just:

gradle build

Run

The repository have two tests:

  • The StateMachineTest has the example from Stack OverFlow.

  • The StateMachineAccountTest is a state machine using the same mechanism, but using an entreprise example for an Account state machine.

The example below is a user account state diagram represented by the code in StateMachineAccountTest class. This diagram can be visualized here too.

                      +----------+
                      |          |
+---------------------+   new    |  [isAccountDourmant()] suspend/
|                     |          +-----------------------------------------+
|                     +---+------+                                         |
|                         |                                                |
|           [isVerified() |                                                |
|           isUniqueId()] |                                                |
|               activate/ |                                                |
|                         |                                                |
|                     +---v------+ [isSuspendedRequested()] suspend/ +-----v-----+
|                     |          +----------------------------------->           |
|                     |          | [isPasswordAlert()] lock/         |           |
|                     |          +----------------------------------->           |
|[isCancelRequested()]|          | [isAccountDormant()] suspend/     |           |
|cancel/              |  Active  +-----------------------------------> Suspended |
|                     |          |                                   |           |
|                     |          |      [isResumeRequested()] resume/|           |
|                     |          <-----------------------------------+           |
|                     |          |          [isLockExpired()] unlock/|           |
|                     |          <-----------------------------------+           |
|                     +-+------+-+                                   +--+------+-+
|                       |      |                                        |      |
| [isCancelRequested()] |      |  [isPolicyViolated()]                  |      |
|               cancel/ |      |  cancel/                               |      |
|                       |      |                                        |      |
|                       |      |                                        |      |
|                       |      |                                        |      |
|                       |      |                                        |      |
|                     +-v------v-+                                      |      |
|                     |          |                                      |      |
|                     |          |  [isCancelRequested()] cancel/       |      |
|                     |          <--------------------------------------+      |
|                     |  Closed  |                                             |
+--------------------->          |                                             |
                      |          |   [isPolicyViolated()] cancel/              |
                      |          <---------------------------------------------+
                      |          |
                      +----------+


About

A state machine write in Java using states, transitions and conditions


Languages

Language:Java 100.0%