better DSL

develop
Vyacheslav Boyko 2022-07-07 16:07:54 +03:00
parent f49fa7297b
commit a124058a56
4 changed files with 28 additions and 11 deletions

View File

@ -29,8 +29,9 @@ Simple way to use it - to construct an inherited class specified with the type o
during transitions. during transitions.
```java ```java
SimpleFsm<String> simpleFsm = SimpleFsm SimpleFsm<String> simpleFsm = Fsm
.<SimpleFsm<String>, String>withStates(SimpleFsm::new) .<SimpleFsm<String>, String>from(SimpleFsm::new)
.withStates()
.from("init") .from("init")
.withBeforeHandler(fsm -> initBefore.set(true)) .withBeforeHandler(fsm -> initBefore.set(true))
.withAfterHandler(fsm -> initAfter.set(true)) .withAfterHandler(fsm -> initAfter.set(true))

View File

@ -54,8 +54,9 @@ import java.util.function.Supplier;
* during transitions. * during transitions.
* <pre> * <pre>
* {@code * {@code
* SimpleFsm<String> simpleFsm = SimpleFsm * SimpleFsm<String> simpleFsm = Fsm
* .<SimpleFsm<String>, String>withStates(SimpleFsm::new) * .<SimpleFsm<String>, String>from(SimpleFsm::new)
* .withStates()
* .from("init") * .from("init")
* .withBeforeHandler(fsm -> initBefore.set(true)) * .withBeforeHandler(fsm -> initBefore.set(true))
* .withAfterHandler(fsm -> initAfter.set(true)) * .withAfterHandler(fsm -> initAfter.set(true))
@ -94,8 +95,8 @@ public class Fsm<T extends Fsm, E> {
* @param <E> the class type of Events to be processed * @param <E> the class type of Events to be processed
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <T extends Fsm,E> FsmBuilder<T,E> withStates(Supplier<T> supplier) { public static <T extends Fsm,E> FsmBuilderInitializer<T,E> from(Supplier<T> supplier) {
return new FsmBuilder<>(supplier); return new FsmBuilderInitializer<>(supplier);
} }
/** /**

View File

@ -0,0 +1,17 @@
package me.bvn13.fsm;
import java.util.function.Supplier;
public class FsmBuilderInitializer<T extends Fsm, E> {
final Supplier<T> supplier;
FsmBuilderInitializer(Supplier<T> supplier) {
this.supplier = supplier;
}
public FsmBuilder<T,E> withStates() {
return new FsmBuilder<>(supplier);
}
}

View File

@ -1,11 +1,8 @@
package me.bvn13.fsm.tests; package me.bvn13.fsm.tests;
import me.bvn13.fsm.ConditionBuilder;
import me.bvn13.fsm.Fsm; import me.bvn13.fsm.Fsm;
import me.bvn13.fsm.FsmBuilder;
import me.bvn13.fsm.SimpleFsm; import me.bvn13.fsm.SimpleFsm;
import me.bvn13.fsm.State; import me.bvn13.fsm.State;
import me.bvn13.fsm.StateBuilder;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -86,8 +83,9 @@ public class FsmTest {
// @formatter:off // @formatter:off
SimpleFsm<String> simpleFsm = SimpleFsm SimpleFsm<String> simpleFsm = Fsm
.<SimpleFsm<String>, String>withStates(SimpleFsm::new) .<SimpleFsm<String>, String>from(SimpleFsm::new)
.withStates()
.from("init") .from("init")
.withBeforeHandler(fsm -> initBefore.set(true)) .withBeforeHandler(fsm -> initBefore.set(true))
.withAfterHandler(fsm -> initAfter.set(true)) .withAfterHandler(fsm -> initAfter.set(true))