Merge branch 'release/2.2.1'

This commit is contained in:
Vyacheslav Boyko 2022-07-14 17:34:47 +03:00
commit 43b98e80c0
6 changed files with 145 additions and 1 deletions

View File

@ -7,7 +7,7 @@
<groupId>me.bvn13.fsm</groupId>
<artifactId>fsm</artifactId>
<version>2.1.9</version>
<version>2.2.1</version>
<packaging>jar</packaging>

View File

@ -50,6 +50,31 @@ Simple way to use it - to construct an inherited class specified with the type o
.create();
```
or since 2.2.1
```java
SimpleFsm<String> simpleFsm = Fsm
.<SimpleFsm<String>, String>from(SimpleFsm::new)
.withStates()
.from("init")
.withBeforeHandler(fsm -> initBefore.set(true))
.withAfterHandler(fsm -> initAfter.set(true))
.withProcessor((fsm, event) -> initProcess.set(true))
.withTransition()
.to("finish")
.checking((fsm, event) -> true)
.endTransition()
.end()
.finish("finish")
.withBeforeHandler(fsm -> finishBefore.set(true))
.withAfterHandler(fsm -> finishAfter.set(true))
.withProcessor((fsm, event) -> finishProcess.set(true))
.end()
.create();
}
```
## Releasing
Creating a new release involves the following steps:

View File

@ -3,12 +3,20 @@ package me.bvn13.fsm;
public class ConditionBuilder<T extends Fsm, E> {
private final FsmBuilder<T,E> fsmBuilder;
private final StateBuilder<T, E> stateBuilder;
private String from;
private String to;
private Condition<T,E> condition;
ConditionBuilder(FsmBuilder<T,E> fsmBuilder) {
this.fsmBuilder = fsmBuilder;
this.stateBuilder = null;
}
ConditionBuilder(FsmBuilder<T,E> fsmBuilder, StateBuilder<T, E> stateBuilder, String from) {
this.fsmBuilder = fsmBuilder;
this.stateBuilder = stateBuilder;
this.from = from;
}
public ConditionBuilder<T,E> from(String from) {
@ -31,4 +39,12 @@ public class ConditionBuilder<T extends Fsm, E> {
return fsmBuilder;
}
public StateBuilder<T, E> endTransition() {
if (stateBuilder == null) {
throw new IllegalStateException("Use '.end()' instead");
}
end();
return stateBuilder;
}
}

View File

@ -52,6 +52,8 @@ import static java.lang.String.format;
* Simple way to use it - to construct an inherited class specified with the type of events to be processed
* during transitions.
*
* since 2.1.5
*
* <pre>
* {@code
* SimpleFsm<String> simpleFsm = Fsm
@ -76,6 +78,31 @@ import static java.lang.String.format;
* }
* </pre>
*
* or since 2.2.1
*
* <pre>
* {@code
* SimpleFsm<String> simpleFsm = Fsm
* .<SimpleFsm<String>, String>from(SimpleFsm::new)
* .withStates()
* .from("init")
* .withBeforeHandler(fsm -> initBefore.set(true))
* .withAfterHandler(fsm -> initAfter.set(true))
* .withProcessor((fsm, event) -> initProcess.set(true))
* .withTransition()
* .to("finish")
* .checking((fsm, event) -> true)
* .endTransition()
* .end()
* .finish("finish")
* .withBeforeHandler(fsm -> finishBefore.set(true))
* .withAfterHandler(fsm -> finishAfter.set(true))
* .withProcessor((fsm, event) -> finishProcess.set(true))
* .end()
* .create();
* }
* </pre>
*
* <p>
* Otherwise you are able to use Old syntax:
*

View File

@ -56,4 +56,8 @@ public class StateBuilder<T extends Fsm, E> {
return this;
}
public ConditionBuilder<T,E> withTransition() {
return new ConditionBuilder<>(fsmBuilder, this, name);
}
}

View File

@ -262,6 +262,78 @@ public class FsmTest {
}
@Test
public void newSyntaxOfTransitions() {
Counter initCounter = new Counter();
Counter firstIntermediateCounter = new Counter();
Counter secondIntermediateCounter = new Counter();
Counter finishCounter = new Counter();
// @formatter:off
SimpleFsm<String> simpleFsm = Fsm
.<SimpleFsm<String>, String>from(SimpleFsm::new)
.withStates()
.from("init")
.withBeforeHandler(fsm -> initCounter.before.incrementAndGet())
.withProcessor((fsm, event) -> initCounter.process.incrementAndGet())
.withAfterHandler(fsm -> initCounter.after.incrementAndGet())
.withTransition()
.to("intermediate-1")
.checking((fsm, event) -> true)
.endTransition()
.end()
.state("intermediate-1")
.withBeforeHandler(fsm -> firstIntermediateCounter.before.incrementAndGet())
.withProcessor((fsm, event) -> firstIntermediateCounter.process.incrementAndGet())
.withAfterHandler(fsm -> firstIntermediateCounter.after.incrementAndGet())
.withTransition()
.from("intermediate-1")
.to("intermediate-2")
.checking((fsm, event) -> true)
.endTransition()
.end()
.state("intermediate-2")
.withBeforeHandler(fsm -> secondIntermediateCounter.before.incrementAndGet())
.withProcessor((fsm, event) -> secondIntermediateCounter.process.incrementAndGet())
.withAfterHandler(fsm -> secondIntermediateCounter.after.incrementAndGet())
.end()
.finish("finish")
.withBeforeHandler(fsm -> finishCounter.before.incrementAndGet())
.withProcessor((fsm, event) -> finishCounter.process.incrementAndGet())
.withAfterHandler(fsm -> finishCounter.after.incrementAndGet())
.withTransition()
.from("intermediate-2")
.to("finish")
.checking((fsm, event) -> true)
.endTransition()
.end()
.create()
;
// @formatter:on
simpleFsm.process("");
simpleFsm.process("");
simpleFsm.process("");
Assert.assertEquals("finish", simpleFsm.getCurrentState().getName());
Assert.assertEquals(1, initCounter.before.get());
Assert.assertEquals(1, initCounter.after.get());
Assert.assertEquals(1, initCounter.process.get());
Assert.assertEquals(1, firstIntermediateCounter.before.get());
Assert.assertEquals(1, firstIntermediateCounter.after.get());
Assert.assertEquals(1, firstIntermediateCounter.process.get());
Assert.assertEquals(1, secondIntermediateCounter.before.get());
Assert.assertEquals(1, secondIntermediateCounter.after.get());
Assert.assertEquals(1, secondIntermediateCounter.process.get());
Assert.assertEquals(1, finishCounter.before.get());
Assert.assertEquals(0, finishCounter.after.get());
Assert.assertEquals(0, finishCounter.process.get());
}
static class Counter {
final AtomicInteger before = new AtomicInteger(0);
final AtomicInteger after = new AtomicInteger(0);