1- # JState (v2.1 ) [ ![ Build Status] ( https://travis-ci.org/UnquietCode/JState.png?branch=master )] ( https://travis-ci.org/UnquietCode/JState )
1+ # JState (v3.0 ) [ ![ Build Status] ( https://travis-ci.org/UnquietCode/JState.png?branch=master )] ( https://travis-ci.org/UnquietCode/JState )
22A core Java tool which provides state machine semantics using enums, strings, or anything else you
33want to represent the various states. States have transitions which can move them to other states.
44Callbacks are provided for transitions, and for each state when entering or exiting. It is also
@@ -11,15 +11,17 @@ generally better to construct your state machine up front and not modify it ther
1111EnumStateMachine and StringStateMachine in particular can be serialized to and from their string
1212representations.
1313
14+ As of version 3.0, the minimum version of Java required is JDK 8.
15+
1416# Installation
1517The project is built using Maven, and the artifacts are available from Maven Central. (If you
16- are a current user of the tool, note that the group name has changed to accommodate Sonatype's
17- repository hosting requirements.)
18+ are a current user of the tool, note that the group name has been recently changed to
19+ accommodate Sonatype's repository hosting requirements.)
1820```
1921<dependency>
2022 <groupId>com.unquietcode.tools.jstate</groupId>
2123 <artifactId>jstate</artifactId>
22- <version>2.1 </version>
24+ <version>3.0 </version>
2325</dependency>
2426```
2527
@@ -38,7 +40,7 @@ enum State {
3840
3941...
4042
41- EnumStateMachine<State > esm = new EnumStateMachine<State > (State . Ready );
43+ EnumStateMachine<State > esm = new EnumStateMachine<> (State . Ready );
4244esm. addTransitions(State . Ready , State . Running , State . Finished );
4345esm. addTransitions(State . Running , State . Paused , State . Stopping );
4446esm. addTransitions(State . Paused , State . Running , State . Stopping );
@@ -54,9 +56,8 @@ method supports mapping from 1..n states. In the example above, we see that some
5456one other states. The ` null ` state is also a possibility, depending on your preference.
5557
5658Callbacks can be added as transitions are defined, and fire during transition between states:
57-
5859``` java
59- TransitionHandler<State > cb = new TransitionHandler<State > () {
60+ TransitionHandler<State > cb = new TransitionHandler<> () {
6061 public void onTransition (State from , State to ) {
6162 // ....
6263 }
@@ -81,6 +82,7 @@ esm.onExiting(State.Running, new StateHandler<State>() {
8182```
8283
8384` StateRouters ` allow you to 'deflect' or 'redirect' a transition based on your own custom logic.
85+ There are several pre-defined routers available which provide round-robin and randomized routing.
8486``` java
8587esm. routeBeforeEntering(TestStates . Three , new StateRouter<TestStates > () {
8688 public TestStates route (TestStates current , TestStates next ) {
@@ -92,11 +94,27 @@ esm.routeBeforeEntering(TestStates.Three, new StateRouter<TestStates>() {
9294` SequenceHandlers ` are callbacks which are triggered whenever the specified sequence of states
9395occurs in the state machine.
9496``` java
95- List<Color > _pattern = Arrays . asList(Color . Blue , Color . Green , Color . Orange );
97+ final List<Color > _pattern = Arrays . asList(Color . Blue , Color . Green , Color . Orange );
98+
99+ sm. onSequence(_pattern, new SequenceHandler<Color > () {
100+ public void onMatch (List<Color > pattern ) {
101+ // pattern equals [Blue, Green, Orange]
102+ }
103+ });
104+ ```
105+
106+ There is also support for wildcard matching in sequences, available through the use
107+ of the ` PatternBuilder ` class.
108+ ``` java
109+ final Pattern<Color > _pattern = PatternBuilder . < Color > create()
110+ .add(Color . Red , Color . Blue )
111+ .addWildcard()
112+ .add(Color . Green )
113+ .build();
96114
97115sm. onSequence(_pattern, new SequenceHandler<Color > () {
98116 public void onMatch (List<Color > pattern ) {
99- assertEquals(_pattern, pattern);
117+ // pattern equals [Red, Blue, Purple, Green]
100118 }
101119});
102120```
@@ -145,7 +163,7 @@ sm.transition(null);
145163```
146164
147165
148- See the [ tests] ( src/test/java/unquietcode/tools/esm/EnumStateMachine_T.java )
166+ See the [ tests] ( src/test/java/unquietcode/tools/esm )
149167for more usage examples, as well as the provided javadocs.
150168
151169# License
0 commit comments